1. 制作密鑰對
首先在服務器上制作密鑰對。登錄到打算使用密鑰登錄的賬戶,然后執行以下命令:
[root@host ~]$ ssh-keygen <== 建立密鑰對 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): <== 輸入密鑰鎖碼,或直接按 Enter 留空 Enter same passphrase again: <== 再輸入一遍密鑰鎖碼 Your identification has been saved in /root/.ssh/id_rsa. <== 私鑰 Your public key has been saved in /root/.ssh/id_rsa.pub. <== 公鑰
密鑰鎖碼在使用私鑰時必須輸入,這樣就可以保護私鑰不被盜用。當然,也可以留空,實現無密碼登錄。
現在,在 root 用戶的家目錄中生成了一個 .ssh 的隱藏目錄,內含兩個密鑰文件。id_rsa 為私鑰,id_rsa.pub 為公鑰。
2. 在服務器上安裝公鑰
鍵入以下命令,在服務器上安裝公鑰:
[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh
3. 設置 SSH,打開密鑰登錄功能
編輯 /etc/ssh/sshd_config 文件,進行如下設置:
RSAAuthentication yes
PubkeyAuthentication yes
另外,請留意 root 用戶能否通過 SSH 登錄:
PermitRootLogin yes
當你完成全部設置,並以密鑰方式登錄成功后,再禁用密碼登錄:
PasswordAuthentication no
最后,重啟 SSH 服務:
[root@host .ssh]$ service sshd restart
4. 將私鑰下載到客戶端,然后轉換為 PuTTY 能使用的格式
使用 WinSCP、SFTP 等工具將私鑰文件 id_rsa 下載到客戶端機器上。然后打開 PuTTYGen,單擊 Actions 中的 Load 按鈕,載入你剛才下載到的私鑰文件。如果你剛才設置了密鑰鎖碼,這時則需要輸入。
載入成功后,PuTTYGen 會顯示密鑰相關的信息。在 Key comment 中鍵入對密鑰的說明信息,然后單擊 Save private key 按鈕即可將私鑰文件存放為 PuTTY 能使用的格式。
今后,當你使用Xshell或 PuTTY 登錄時,可以在左側的 Connection -> SSH -> Auth 中的 Private key file for authentication: 處選擇你的私鑰文件,然后即可登錄了,過程中只需輸入密鑰鎖碼即可。
轉自:http://blog.csdn.net/permike/article/details/52386868
