第一步:生成密鑰
ssh-keygen -t rsa
執行 ssh-keygen 時,纔會在我的home目錄底下的 .ssh/ 這個目錄裏面產生所需要的兩把 Keys ,分別是私鑰 (id_rsa) 與公鑰 (id_rsa.pub)。
[root@localhost ~]# ssh-keygen -t rsa
//先運行這個命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //直接回車
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase)://輸入密鑰密碼
Enter same passphrase again: //重複密鑰密碼
Your identification has been saved in /root/.ssh/id_rsa. //提示公鑰和私鑰已經存放在/root/.ssh/目錄
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
16:71:6b:47:c3:8c:f4:c5:8f:1f:07:e2:15:a9:7b:0f [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| ..o+o.oo |
| o.++o= |
| . o.o+ + |
| o .o ..o|
| S . .o|
| . . E .|
| . o |
| .|
| |
+-----------------+
第二步:
[root@localhost ~]# mv /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
[root@localhost ~]# chmod 600 /root/.ssh/authorized_keys
第三步:修改ssh配置
修改/etc/ssh/sshd_config 文件,將RSAAuthentication 和 PubkeyAuthentication 後面的值都改成yes ,保存。
vim /etc/ssh/sshd_config
...
RSAAuthentication yes
PubkeyAuthentication yes
...
第四步:重啓ssh服務
service sshd restart
第五步:測試
填寫主機和端口號
填寫用戶名 與 用戶密鑰(id_rsa)
保存 測試連接
第六步: 安全配置
1、 修改/etc/ssh/sshd_config 文件
StrictModes no
#StrictModes no #修改爲no,默認爲yes.如果不修改用key登陸是出現server refused our key(如果StrictModes爲yes必需保證存放公鑰的文件夾的擁有與登陸用戶名是相同的.“StrictModes”設置ssh在接收登錄請求之前是否檢查用戶家目錄和rhosts文件的權限和所有權。這通常是必要的,因爲新手經常會把自己的目錄和文件設成任何人都有寫權限。)(來源http://matt-u.iteye.com/blog/851158)
#有了證書登錄了,就禁用密碼登錄吧,安全要緊
PasswordAuthentication no
#建議改變默認ssh端口
Prot 22
#禁用root賬戶登錄,非必要,但爲了安全性,請配置
PermitRootLogin no
2、 重啓sshd服務
service sshd restart