無法透過 ssh 以 root 身分連線到伺服器

無法透過 ssh 以 root 身分連線到伺服器

我想在 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 身分登入。

相關內容