为什么SSH可以从命令行工作,而不能从SSH配置文件工作



我发现在系统上从命令行运行ssh与从~/.ssh/config文件运行不同。但我不知道如何修复它,也不知道这是否是程序本身的问题。

我有一个服务器(blueberry.local)和一个客户端(xps.local)。两者都有一个名为bob的用户。两者都可以通过任一框中的host命令来解析对方。

服务器正在使用以下配置运行sshd(/etc/ssh/sshd_config):

UsePAM yes
Banner none
AddressFamily any
Port 22
X11Forwarding no
PermitRootLogin no
GatewayPorts no
PasswordAuthentication no
KbdInteractiveAuthentication no
PrintMotd no
AuthorizedKeysFile %h/.ssh/authorized_keys %h/.ssh/authorized_keys2 /etc/ssh/authorized_keys.d/%u
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

KexAlgorithms sntrup761x25519-sha512@openssh.com,curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,umac-128@openssh.com
LogLevel INFO
UseDNS no

在我的客户端上,我使用以下配置运行ssh(~/.ssh/config):

Host blueberry.stark.local
Port 22
HostName blueberry.local
IdentityFile ~/.ssh/blueberry_rsa
IdentitiesOnly yes

当从如下命令行运行ssh时:

ssh bob@blueberry.local -i ~/.ssh/blueberry_rsa

该命令有效,我可以通过ssh成功连接到服务器。

但是,当使用如下客户端配置从命令行运行ssh时:

ssh bob@blueberry.local

我收到一个身份验证错误:

bob@blueberry.local: Permission denied (publickey).

这是怎么回事?我试过删除配置属性之类的东西,但从来没有用过。

更奇怪的是,我有另一个类似的客户端配置,它可以毫无问题地工作。。。

问题可能由两个因素引起:

  1. 根据您的示例命令,您的HostHostName值是混合的:
Host <this should be what you type on the CLI>
...
HostName <The real hostname of the server>
...

这意味着ssh实际上不会使用您提供的任何配置。进行以下更改应该有效。

Host blueberry.local
Port 22
HostName blueberry.stark.local
IdentityFile ~/.ssh/blueberry_rsa
IdentitiesOnly yes

如果以下命令适用于您发布的配置,则最有可能发生这种情况:

ssh bob@blueberry.starlink.local
  1. 如果您希望ssh只尝试您的所有私钥,直到找到正确的私钥(~/.ssh/blueberry_rsa),那么很可能您还没有将其添加到ssh-agent中(您可以通过运行ssh-add -L并检查输出进行确认)
by default ssh will check these paths, then any additional keys in the agent:
~/.ssh/id_rsa 
~/.ssh/id_ecdsa 
~/.ssh/id_ecdsa_sk
~/.ssh/id_ed25519
~/.ssh/id_ed25519_sk
~/.ssh/id_xmss
~/.ssh/id_dsa

您的代理中可能只有~/.ssh/id_rssa,这就是抛出遇到麻烦时,运行ssh -vvv <rest of your command>查看引擎盖下发生了什么总是很有帮助的。