SSH 連線輸入正確密碼卻提示「Access Denied」,常見原因包括家目錄權限、sshd_config 設定錯誤、AllowUsers 限制等。本文提供系統性排查步驟與最終解決方案。
若 /home 僅 root 可存取,使用者無法讀取 ~/.ssh/authorized_keys。
# 檢查權限
ls -ld /home /home/username
# 應為 755 (drwxr-xr-x)
chmod 755 /home/username
# .ssh 目錄需 700,authorized_keys 需 600
chmod 700 /home/username/.ssh
chmod 600 /home/username/.ssh/authorized_keys
臨時方案:上傳檔案至 /tmp 再移動。
vi /etc/ssh/sshd_config
| 參數 | 建議值 | 說明 |
|---|---|---|
| PermitRootLogin | yes / prohibit-password | 允許 root 登入(生產建議 prohibit-password 僅限金鑰) |
| PubkeyAuthentication | yes | 啟用公鑰認證 |
| PasswordAuthentication | yes | 啟用密碼認證 |
| AuthorizedKeysFile | .ssh/authorized_keys | 公鑰檔案路徑 |
| AllowUsers | user1 user2 root | 白名單機制:僅列表內使用者可登入,未列入則 Access Denied |
| DenyUsers | 黑名單機制 |
修改 sshd_config 末尾發現:
AllowUsers user1 user2
未包含 root,導致 root 登入被拒。修正:
AllowUsers user1 user2 root
# 或註解掉該行允許所有使用者(視安全政策)
# AllowUsers user1 user2
重啟服務生效:
/etc/init.d/sshd restart
# 或
systemctl restart sshd
/etc/passwd、/etc/shadow 權限(644/000)restorecon -Rv /home/username/.sshpasswd -S username、pam_tally2df -h
/var/log/secure 或 /var/log/auth.log 詳細錯誤訊息sshd_config 關鍵參數(PermitRootLogin、AllowUsers、PasswordAuthentication)/etc/hosts.allow, /etc/hosts.deny)