大致步驟:在伺服器端產生一對密鑰,然後公開金鑰儲存在伺服器使用者目錄.ssh下的authorized_keys2,是個隱藏的目錄。私密金鑰必須將id_dsa下載到用戶端,然後通過puttygen.exe產生.ppk檔案。當使用者登入時,伺服器會根據公開金鑰來比較私密金鑰,如果相符就允許登陸,否則拒絕。私密金鑰只儲存在用戶端中,及時有人知道root密碼,沒有私密金鑰也不能遠程登入到伺服器上。
Centos 5.4 ssh key 配置:
1. Server:
1).shell# vi /etc/ssh/sshd_config 修改以下配置:
Port 20000
Protocol 2
PasswordAuthentication no
2).#為了安全起見,ssh key方式用普通使用者登入,例如smallfish
[smallfish@tingso ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/smallfish/.ssh/id_dsa):
Created directory '/home/smallfish/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/smallfish/.ssh/id_dsa.
Your public key has been saved in /home/smallfish/.ssh/id_dsa.pub.
The key fingerprint is:
68:e2:be:31:5a:d0:3c:0f:6d:48:31:69:13:9c:58:f3 smallfish@tingso.com
3).shell$ chmmod o+x /home/smallfish
[smallfish@tingso .ssh]$ ls
id_dsa id_dsa.pub
4).mv id_dsa.pub authorized_keys2
5).download id_dsa to windows #一般為了方便下載到putty目錄下,用於產生ppk檔案
6).shell# service sshd restart
2. Client:
#產生ppk檔案
1).open puttygen.exe--Conversions--import key--id_dsa--Enter passpharase for key--Save private key--儲存為*.ppk(例如server.ppk)
#啟動用戶端測試
2).open putty.exe--Category-SSH-Auth-Private key file for authentication #匯入ppk檔案
#輸入之前在server上設定的passphrase登入
3).login as: smallfish
Authenticating with public key "imported-openssh-key"
Passphrase for key "imported-openssh-key":
Ok, 配置完成。
問題:
1.用戶端用key登陸的時候提示:
Disconnected: No supported authentication methods available
解決:是由於在用戶端puttygen產生ppk檔案的時候重新用了Generate命令,直接匯入id_dsa檔案然後save private key即可。