本指南涵蓋 OpenSSH 服務端的安裝、配置密碼/公鑰/多因素認證,以及連接測試方法。
適用系統:Ubuntu/Debian、CentOS/RHEL 等主流 Linux 發行版。
1. 安裝 SSH 服務端
Ubuntu/Debian
# 更新軟件包索引
sudo apt update# 安裝 OpenSSH 服務端
sudo apt install openssh-server -y# 啟動服務并設置開機自啟
sudo systemctl enable --now ssh
CentOS/RHEL
# 安裝 OpenSSH 服務端
sudo yum install openssh-server -y# 啟動服務并設置開機自啟
sudo systemctl enable --now sshd
驗證安裝
# 檢查服務狀態
sudo systemctl status ssh # Ubuntu/Debian
sudo systemctl status sshd # CentOS/RHEL# 查看 SSH 端口監聽
ss -tnlp | grep ssh
2. 基礎配置 SSH 服務
編輯配置文件
sudo nano /etc/ssh/sshd_config
關鍵配置項(按需修改)
# 修改默認端口(可選,避免暴力破解)
Port 2222# 禁用 root 登錄(安全建議)
PermitRootLogin no# 允許用戶列表(為空表示允許所有用戶)
AllowUsers alice bob# 禁用密碼認證(推薦使用公鑰后關閉)
PasswordAuthentication no# 啟用公鑰認證
PubkeyAuthentication yes# 指定公鑰存儲路徑
AuthorizedKeysFile .ssh/authorized_keys# 配置日志級別
LogLevel VERBOSE
重啟 SSH 服務生效
# Ubuntu/Debian
sudo systemctl restart ssh# CentOS/RHEL
sudo systemctl restart sshd
3. 配置認證方式
3.1 密碼認證
默認已啟用,如需禁用請設置 PasswordAuthentication no
。
3.2 公鑰認證
客戶端生成密鑰對
# 在客戶端機器執行
ssh-keygen -t ed25519 -C "your_email@example.com"
# 默認保存路徑:~/.ssh/id_ed25519(私鑰)和 ~/.ssh/id_ed25519.pub(公鑰)
上傳公鑰到服務端
# 方法 1:使用 ssh-copy-id 自動上傳
ssh-copy-id -p 22 -i ~/.ssh/id_ed25519.pub user@server_ip# 方法 2:手動追加公鑰到 ~/.ssh/authorized_keys
cat ~/.ssh/id_ed25519.pub | ssh user@server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
驗證權限
服務端用戶目錄權限必須為:
~/.ssh
→700
~/.ssh/authorized_keys
→600
# 修復權限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
3.3 多因素認證(MFA)
安裝 Google Authenticator
# Ubuntu/Debian
sudo apt install libpam-google-authenticator -y# CentOS/RHEL
sudo yum install google-authenticator -y
生成 TOTP 密鑰
google-authenticator
# 按提示操作,保存緊急備用碼,選擇基于時間的令牌
配置 PAM 模塊
sudo nano /etc/pam.d/sshd
添加以下行:
auth required pam_google_authenticator.so
修改 SSH 配置
sudo nano /etc/ssh/sshd_config
確保以下配置:
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive
重啟 SSH 服務
sudo systemctl restart ssh # Ubuntu/Debian
sudo systemctl restart sshd # CentOS/RHEL
4. 防火墻配置
開放 SSH 端口
# Ubuntu/Debian (UFW)
sudo ufw allow 22/tcp # 若修改了端口,替換為實際端口號
sudo ufw reload# CentOS/RHEL (Firewalld)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
5. 測試 SSH 連接
基本連接測試
# 使用密碼認證
ssh -p 22 user@server_ip# 使用公鑰認證(指定私鑰路徑)
ssh -p 22 -i ~/.ssh/id_ed25519 user@server_ip# 使用 MFA 認證(先公鑰后 TOTP)
ssh -p 22 user@server_ip
# 連接時會提示輸入驗證碼
調試連接問題
# 客戶端開啟詳細日志
ssh -vvv user@server_ip# 查看服務端日志
sudo tail -f /var/log/auth.log # Ubuntu/Debian
sudo tail -f /var/log/secure # CentOS/RHEL
6. 安全加固建議
-
禁用弱加密算法
在/etc/ssh/sshd_config
中添加:Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
-
限制用戶訪問
AllowUsers alice bob # 僅允許特定用戶 DenyUsers root # 禁止特定用戶
-
啟用 Fail2ban
# 安裝 Fail2ban sudo apt install fail2ban -y # Ubuntu/Debian sudo yum install fail2ban -y # CentOS/RHEL
-
定期更新系統
sudo apt update && sudo apt upgrade -y # Ubuntu/Debian sudo yum update -y # CentOS/RHEL
7. 常見問題解決
問題 1:公鑰認證失敗
- 檢查項:
- 服務端
~/.ssh/authorized_keys
文件權限是否為600
。 - 客戶端私鑰權限是否為
600
。 - SSH 配置中
PubkeyAuthentication
是否為yes
。
- 服務端
問題 2:MFA 認證不生效
- 檢查項:
- PAM 配置
/etc/pam.d/sshd
是否包含pam_google_authenticator.so
。 - SSH 配置中
AuthenticationMethods
是否設置為publickey,keyboard-interactive
。
- PAM 配置
問題 3:連接超時
- 檢查項:
- 防火墻是否開放 SSH 端口。
- 服務端 SSH 服務是否運行。
- 網絡路由是否可達(使用
ping
或traceroute
測試)。
通過本指南,您已掌握 Linux 下 SSH 服務的完整配置與管理方法。根據實際需求靈活選擇認證方式,并遵循安全最佳實踐,可顯著提升系統的遠程訪問安全性。