Linux中設置SSH免密碼(密鑰)登錄的具體步驟
1. 基本原理
SSH 免密碼登錄通過公鑰認證實現(xiàn):
- 在客戶端生成一對密鑰(公鑰和私鑰)。
- 將公鑰添加到目標服務器的
~/.ssh/authorized_keys
文件中。 - 客戶端使用私鑰登錄,無需輸入密碼。
2. 具體步驟
2.1 在客戶端生成 SSH 密鑰對
檢查是否已有密鑰
在客戶端(你的本地機器或跳板機)上,檢查是否已有 SSH 密鑰:
ls -l ~/.ssh/
如果存在 id_rsa
(私鑰)和 id_rsa.pub
(公鑰),可以跳到步驟 2.2。
生成密鑰對
使用 ssh-keygen
生成 RSA 或 ED25519 密鑰(推薦 ED25519,安全性更高):
ssh-keygen -t ed25519 -C "your_email@example.com"
或使用 RSA(舊系統(tǒng)可能不支持 ED25519):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- 按 Enter 接受默認文件路徑(
~/.ssh/id_ed25519
或~/.ssh/id_rsa
)。 - 可設置密碼保護私鑰(為空則無密碼,推薦為空以實現(xiàn)完全免密)。
- 執(zhí)行后會生成:
~/.ssh/id_ed25519
或~/.ssh/id_rsa
(私鑰)~/.ssh/id_ed25519.pub
或~/.ssh/id_rsa.pub
(公鑰)
2.2 將公鑰復制到目標服務器
將客戶端的公鑰添加到目標服務器的 ~/.ssh/authorized_keys
文件中。
使用 ssh-copy-id
(推薦,簡單)
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host
- 替換
user
為目標服務器的用戶名,remote_host
為服務器 IP 或域名。 - 輸入目標服務器密碼,公鑰會自動添加到
~/.ssh/authorized_keys
。
手動復制(如果 ssh-copy-id
不可用)
- 在客戶端查看公鑰內容:
cat ~/.ssh/id_ed25519.pub
- 復制輸出內容(類似
ssh-ed25519 AAAAC3... your_email@example.com
)。 - 登錄目標服務器,編輯
authorized_keys
:
ssh user@remote_host mkdir -p ~/.ssh echo "your_public_key" >> ~/.ssh/authorized_keys
- 確保權限正確:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
2.3 測試免密碼登錄
在客戶端嘗試登錄:
ssh user@remote_host
如果配置正確,將無需輸入密碼直接登錄。
2.4 (可選)配置 SSH 客戶端
為方便管理,可以在客戶端的 ~/.ssh/config
文件中添加配置:
# 編輯配置文件 nano ~/.ssh/config
添加以下內容:
Host alias_name HostName remote_host User user IdentityFile ~/.ssh/id_ed25519
alias_name
:自定義別名,如myserver
。- 保存后,使用
ssh alias_name
即可登錄。
3. 防火墻與 SSH 配置
確保目標服務器的防火墻允許 SSH 連接(默認端口 22):
使用 firewalld:
sudo firewall-cmd --zone=public --add-service=ssh --permanent sudo firewall-cmd --reload
使用 iptables:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT sudo iptables-save > /etc/sysconfig/iptables
使用 ufw(Ubuntu):
sudo ufw allow ssh
4. 常見問題排查
無法免密登錄
檢查服務器端 SSH 配置(/etc/ssh/sshd_config
):
sudo nano /etc/ssh/sshd_config
確保以下配置啟用:
PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
重啟 SSH 服務:
sudo systemctl restart sshd
檢查 ~/.ssh/authorized_keys
文件權限(必須為 600):
ls -l ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
檢查客戶端私鑰權限:
chmod 600 ~/.ssh/id_ed25519
SELinux 限制(CentOS/RHEL)
如果 SELinux 啟用,可能阻止 SSH 登錄:
sudo setsebool -P ssh_keysign on sudo restorecon -R -v ~/.ssh
連接被拒絕
- 確認服務器 SSH 服務運行:
sudo systemctl status sshd
- 檢查防火墻是否開放 22 端口:
sudo firewall-cmd --list-ports
- 測試端口連通性:
nc -zv remote_host 22
公鑰格式錯誤
確保公鑰是一行完整內容,復制時不要引入換行符。
5. AI 開發(fā)相關場景
在 AI 開發(fā)中,免密碼登錄常用于:
- 遠程訪問 GPU 服務器:配置 Jupyter Notebook 或 TensorFlow Serving 的遠程訪問。
- 自動化腳本:如批量部署模型訓練任務,需在多臺機器間免密傳輸文件(
scp
或rsync
)。 - Docker 集群:在多節(jié)點集群中配置 SSH 免密登錄以便管理。
示例:在客戶端配置 scp
免密傳輸:
scp dataset.tar.gz user@remote_host:/path/to/destination
6. 安全注意事項
- 保護私鑰:不要泄露
~/.ssh/id_rsa
或~/.ssh/id_ed25519
,確保文件權限為 600。
限制公鑰訪問:在 authorized_keys
中可添加限制,如:
from="192.168.1.100" ssh-ed25519 AAAAC3... your_email@example.com
僅允許特定 IP 使用該公鑰登錄。
禁用密碼登錄:為提高安全性,可在 /etc/ssh/sshd_config
中設置:
PasswordAuthentication no
然后重啟 sshd
:
sudo systemctl restart sshd
以上就是Linux中設置SSH免密碼(密鑰)登錄的具體步驟的詳細內容,更多關于Linux SSH免密碼登錄的資料請關注腳本之家其它相關文章!
相關文章
Centos7 利用LVM實現(xiàn)動態(tài)擴容的方法
本篇文章主要介紹了Centos 7 利用LVM實現(xiàn)動態(tài)擴容的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02在 Linux 命令行中使用 tcpdump 抓包的一些功能
tcpdump 是一款靈活、功能強大的抓包工具,能有效地幫助排查網(wǎng)絡故障問題。接下來通過本文給大家介紹在 Linux 命令行中使用 tcpdump 抓包的一些常用功能,需要的朋友參考下吧2018-11-11