ssh 免密登入 linux 伺服器詳細步驟。Linux 免登入失敗解決方案,驗證方法。如何更高效的登入遠端伺服器。
1. 生成 rsa 密鑰(本機)
ssh-keygen -t rsa
2. 拷貝公鑰到伺服器(本機)
- 以下指令将生成的 RSA 公鑰拷貝到伺服器的 ~/.ssh/authorized_keys 檔案末;
ssh-copy-id -i ~/.ssh/aaron [email protected]
3. 登入伺服器修改檔案權限(伺服器)
ssh [email protected]
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
4. 修改 ssh 配置(伺服器)
sudo vi /etc/ssh/sshd_config
增加如下内容(如果有不需要新增):
# 允許RSA公鑰登入
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
5. 重新開機 ssh 服務(伺服器)
sudo systemctl restart sshd
6. 驗證是否設定成功(本機)
ssh -i ~/.ssh/aaron [email protected]
如果不需要密碼即可登入說明配置成功,如果不成功關注QA問題清單
7. 修改 ssh config(本機)
vi ~/.ssh/config
添加如下内容:
host my_linux
hostname 192.168.1.168
user Aaron
port 22
identityfile ~/.ssh/aaron
8. 解決每次都需要輸入 ssh-key 密碼(本機)
ssh-add ~/.ssh/aaron
9. 驗證簡化後的指令(本機)
ssh my_linux
10. QA
10.1 自動登入失敗如何檢視錯誤日志?
sudo vi /var/log/secure
10.2 Authentication refused: bad ownership or modes for directory 錯誤如何解決?
- 通常這類錯誤主要針對 ~/.ssh 目錄和 ~/.ssh/authorized_keys 檔案權限太寬松導緻,系統判斷有安全隐患;
- 按照第3步設定相關權限即可解決該問題。