原因:由於採用密碼登錄很容易被攻擊,各種異地登錄,所以改爲祕鑰登錄。
生成祕鑰
在你的電腦上生成祕鑰
生成命令(之後可以一路回車)
ssh-keygen -t rsa
存放的目錄(直接回車):
Enter file in which to save the key (/root/.ssh/id_rsa):
輸入2次密碼( 直接回車 ):
Enter passphrase (empty for no passphrase): Enter same passphrase
again:
+—[RSA 2048]—-+
| =o… .oo.o|
| E = O. . .o=|
| . X o .o=|
| . * . o..+|
| . = S . + .+=|
| . o . +.+B|
| o .o=|
| oooo|
| o+.|
+—-[SHA256]—–+
查看祕鑰
ls -l ~/.ssh/
查看到有3個文件
id_rsa :這個是私鑰
id_rsa.pub :這個是公鑰
複製公鑰的內容
查看公鑰內容
cat ~/.ssh/id_rsa.pub
複製公鑰的內容(鼠標執行)
進入服務器配置(在哪個用戶目錄下配置就是鏈接的哪個用戶)
在ssh目錄下創建文件authorized_keys
vim ~/.ssh/authorized_keys
將上面複製的公鑰內容粘貼到文件中
保存並退出 wq
修改文件權限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
登錄測試
在你的電腦上輸入命令(這裏的用戶名是你在服務器下配置的用戶名)
ssh 用戶名@ip地址
不輸入密碼直接登錄成功 !完成
在服務器上關閉密碼登錄
vi /etc/ssh/sshd_config
找到
PasswordAuthentication yes
改爲
PasswordAuthentication no