![無法透過 ssh 以 root 身分連線到伺服器](https://rvso.com/image/1692519/%E7%84%A1%E6%B3%95%E9%80%8F%E9%81%8E%20ssh%20%E4%BB%A5%20root%20%E8%BA%AB%E5%88%86%E9%80%A3%E7%B7%9A%E5%88%B0%E4%BC%BA%E6%9C%8D%E5%99%A8.png)
我想在 Windows 上的 VS Code 中使用遠端資源管理器從 Ubuntu 存取伺服器。一直輸入密碼很煩人,所以我嘗試使用 ssh-keygen 產生 ssh 金鑰。該金鑰對於普通用戶來說工作正常,但我無法以 root 身份登入。
來自 cmd 的錯誤(ssh root@<server_ip>):
root@<server_ip>: Permission denied (publickey,password).
錯誤在/var/log/auth.log
:
sshd[1012327]: Connection reset by authenticating user root <client_ip> port 59021 [preauth]
我見過的所有解決方案都提到了 sshd_config,但其中啟用了 root 登入。 sshd_配置:
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
PubkeyAuthentication yes
# Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
我已將公鑰添加到/home/user/.ssh/authorized_keys
文件中。在我看來,這個金鑰僅供用戶使用,但從指南來看,我似乎也可以將它用於root。
那為什麼我不能以 root 身分連線呢?
答案1
在我看來,這個金鑰僅供用戶使用,但從指南來看,我似乎也可以將它用於root。
導遊誤導了你。
每個使用者都有他或她自己的主目錄,因此也有他或她自己的authorized_keys
. Root 的主目錄通常是/root/
.因此以 root 身分登入的相關文件是/root/.ssh/authorized_keys
.與 無關/home/user/.ssh/authorized_keys
。
想一想。/home/user/.ssh/authorized_keys
屬於user
普通用戶的用戶,並且可能或者可能不會具有 sudo 存取權限。用戶可以在那裡註冊任何密鑰。如果 root 使用相同的文件,那麼使用者只需授權某個金鑰就可以以 root 身分登入。