萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux SSH 登入「Access Denied」錯誤完整排查與解決

SSH 連線輸入正確密碼卻提示「Access Denied」,常見原因包括家目錄權限、sshd_config 設定錯誤、AllowUsers 限制等。本文提供系統性排查步驟與最終解決方案。

常見原因與排查順序

1. 家目錄權限問題

若 /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 再移動。

2. sshd_config 關鍵設定檢查

vi /etc/ssh/sshd_config
參數建議值說明
PermitRootLoginyes / prohibit-password允許 root 登入(生產建議 prohibit-password 僅限金鑰)
PubkeyAuthenticationyes啟用公鑰認證
PasswordAuthenticationyes啟用密碼認證
AuthorizedKeysFile.ssh/authorized_keys公鑰檔案路徑
AllowUsersuser1 user2 root白名單機制:僅列表內使用者可登入,未列入則 Access Denied
DenyUsers黑名單機制

3. 真實案例:AllowUsers 遺漏 root

修改 sshd_config 末尾發現:

AllowUsers user1 user2

未包含 root,導致 root 登入被拒。修正:

AllowUsers user1 user2 root
# 或註解掉該行允許所有使用者(視安全政策)
# AllowUsers user1 user2

重啟服務生效:

/etc/init.d/sshd restart
# 或
systemctl restart sshd

4. 其他檢查項目

  • /etc/passwd、/etc/shadow 權限(644/000)
  • SELinux 標籤:restorecon -Rv /home/username/.ssh
  • 帳號是否鎖定:passwd -S username、pam_tally2
  • 磁碟空間滿導致無法寫入登入記錄:df -h

圖示說明

SSH Access Denied 排查示意圖

排查檢查清單

  1. 確認密碼正確(大小寫、特殊字元)
  2. 檢查 /var/log/secure 或 /var/log/auth.log 詳細錯誤訊息
  3. 驗證 sshd_config 關鍵參數(PermitRootLogin、AllowUsers、PasswordAuthentication)
  4. 檢查家目錄與 .ssh 權限
  5. 確認防火牆/SELinux/TCP Wrappers (/etc/hosts.allow, /etc/hosts.deny)
  6. 重啟 sshd 服務
copyright © 萬盛學電腦網 all rights reserved