1. 制作密鑰對(duì)
首先在服務(wù)器上制作密鑰對(duì)
[root@host ~]$ ssh-keygen <== 建立密鑰對(duì)
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. <== 公鑰
The key fingerprint is:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host
密鑰鎖碼在使用私鑰時(shí)必須輸入咪奖,這樣就可以保護(hù)私鑰不被盜用。當(dāng)然酱床,也可以留空羊赵,實(shí)現(xiàn)無密碼登錄。
現(xiàn)在斤葱,在 root 用戶的家目錄中生成了一個(gè) .ssh 的隱藏目錄慷垮,內(nèi)含兩個(gè)密鑰文件。id_rsa 為私鑰揍堕,id_rsa.pub 為公鑰料身。
2. 在服務(wù)器上安裝公鑰
鍵入以下命令,在服務(wù)器上安裝公鑰:
[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
如此便完成了公鑰的安裝衩茸。為了確保連接成功芹血,請(qǐng)保證以下文件權(quán)限正確:
[root@host .ssh]$ chmod 600 authorized_keys & chmod 700 ~/.ssh
3. 設(shè)置 SSH,打開密鑰登錄功能
編輯 /etc/ssh/sshd_config 文件楞慈,進(jìn)行如下設(shè)置:
RSAAuthentication yes
PubkeyAuthentication yes
PermitRootLogin yes
PasswordAuthentication no
最后幔烛,重啟 SSH 服務(wù):
[root@host .ssh]$ service sshd restart
4、保存私鑰
cd /root/.ssh
cat /root/.ssh/id_rsa
將打印出的結(jié)果保存成id_rsa文件囊蓝,供ssh客戶端登陸使用
轉(zhuǎn)載自設(shè)置 SSH 通過密鑰登錄 | 菜鳥教程 (runoob.com)