如果我使用非默认密钥文件,SSH 会不断请求我的密码

Gab*_*lla 6 ssh public-key

我有一个名为 的密钥文件KEY,其中包含KEYKEY.pub。我将 pub 上传到authorized_keys,并添加私有

ssh-add /home/user/.ssh/KEY
Run Code Online (Sandbox Code Playgroud)

但是当我尝试与 连接时ssh user@host.com,它一直要求我输入密码。

如果我使用ssh-keygen默认密钥名称生成密钥并保留默认密钥名称,则上传 pub 并加载私有密钥,它不会请求密码。

可能是什么问题?

小智 8

有了ssh -vvv user@host.com你可以得到调试输出,它可能会告诉你,它不能进行身份验证~/.ssh/id_rsa(SSH的默认密钥文件)。答案是告诉 ssh 使用哪个密钥:

ssh -i /home/user/.ssh/KEY user@host.com
Run Code Online (Sandbox Code Playgroud)

您还可以将每个主机的密钥文件添加到您的.ssh/config,然后您只需键入ssh host.com并自动选择用户/密钥。

.ssh/config 的示例条目(有关更多信息,请参阅man ssh_config):

Host mysshserver ssh.host.com
HostName ssh.host.com
User myusername
IdentityFile ~/.ssh/mykeyfile
Run Code Online (Sandbox Code Playgroud)


密钥文件的解释来自man ssh

 -i identity_file
             Selects a file from which the identity (private key) for RSA or
             DSA authentication is read.  The default is ~/.ssh/identity for
             protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for pro?
             tocol version 2.  Identity files may also be specified on a per-
             host basis in the configuration file.  It is possible to have
             multiple -i options (and multiple identities specified in config?
             uration files).
Run Code Online (Sandbox Code Playgroud)