1. 程式人生 > 其它 >Windows SSH免密連線Linux伺服器

Windows SSH免密連線Linux伺服器

Windows SSH免密連線Linux伺服器

我們在自己電腦上需要遠端連線Linux伺服器時,每次都需要輸入 ssh @username ip -p port,還需要輸入登入密碼,過程複雜,因此準備在Windows上配置SSH免密連線Linux伺服器

前提:電腦上已經存在一對SSH-Key,準備新建一對SSH-Key

目錄

1 建立SSH-Key

# 1.生產金鑰 以rsa演算法
C:\Users\Administrator\.ssh> ssh-keygen -t rsa -C "linux" 
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\Administrator/.ssh/id_rsa): C:\Users\Administrator/.ssh/id_rsa_linux #2.填寫新的地址
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\Administrator/.ssh/id_rsa_linux.
Your public key has been saved in C:\Users\Administrator/.ssh/id_rsa_linux.pub.
The key fingerprint is:
SHA256:QeTc5iTO6rhOTW1a5IfgcOw3rMPjhdCYpcrz2VPpbMg linux
The key's randomart image is:
+---[RSA 2048]----+
|       .o        |
|     . + .       |
|    . = * +      |
|     @ O B       |
|    = = S.o      |
| . . = Ooo       |
|  + ..X=.        |
|   + *E++        |
|   .*.+o         |
+----[SHA256]-----+
PS C:\Users\Administrator\.ssh>
 

2 將公鑰拷貝到Linux伺服器上(拷貝或上傳)

位置:/root/.ssh

如果不存在 authorized_keys,則新建一個檔案,將生成的公鑰複製追加到authorized_keys中

還可以通過上傳的方式,將公鑰上傳到伺服器上:

  1. 使用 Git Bash ,在Windows ssh資料夾下右擊開啟 Git Bash 輸入 ssh-copy-id root@ip -p prot

  2. 輸入密碼,後即可上傳

3 Linux伺服器修改SSH配置

位置:/etc/ssh/sshd_config

PermitRootLogin yes
RSAAuthentication yes
PubkeyAuthentication yes

注意:修改玩後重啟SSH服務:

  1. service ssh restart // centos 6的命令
  2. systemctl restart sshd // centos 7的命令

4.在Windows ssh檔案下新建config檔案

內容如下:

Host name ## 自定義名稱
  HostName ip  ## ip地址
  Port port ## 埠號
  User user ## 登入使用者名稱
  IdentityFile ~/.ssh/id_rsa_linux ##使用的私鑰

5.測試使用

命令列中輸入:ssh 自定義名稱 (與上述 Host 後面定義的名稱一致)