有没有办法让 ssh 提供更多密钥来自动登录?

有没有办法让 ssh 提供更多密钥来自动登录?

我从网上的大量资源中了解到(大致而言),要想不使用密码登录远程服务器:生成一个 ssh 密钥,将 pub 版本放在远程系统的 authorized_keys 中;将私有版本放在本地 ~/.ssh/ 目录中;将其 chmod 设置为 0600,然后就可以登录了。虽然这基本上是正确的,但我发现密钥(对)必须命名为 id_rsa(id_rsa.pub)或 id_dsa(id_dsa.pub),SSH 才能将其提供给远程服务器。

让我稍微回顾一下。我SurnameG在本地 Mac 上有一个登录名。我在远程系统 otherserver 上有一个 SurnameG 帐户。

我将内容复制~/.ssh/surnameg.pub到了该系统的 /home/surnameg/.ssh/authorized_keys。我已使用-iMac 上的 ssh 选项进行了测试,一切正常。

我有一个~/.ssh/id_rsa(我生成用于github.com)。

当然,我还有~/.ssh/surnameg一些其他的密钥,当我尝试通过以下方式登录时,这些密钥并没有被“尝试”otherserver.com

ssh 1.2.3.4

在这里,我尝试使用SurnameG(当前本地登录用户)登录我的SurnameG帐户其他服务器。我希望 openssh~/.ssh/surnameg在尝试连接时提供,但它没有 - 让我们使用详细选项仔细看看:

BOX:~ SurnameG$ ssh -v 1.2.3.4
OpenSSH_5.9p1, OpenSSL 0.9.8y 5 Feb 2013
debug1: Reading configuration data /Users/SurnameG/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to 1.2.3.4 [50.112.132.124] port 22.
debug1: Connection established.
debug1: identity file /Users/SurnameG/.ssh/id_rsa type 1
debug1: identity file /Users/SurnameG/.ssh/id_rsa-cert type -1
debug1: identity file /Users/SurnameG/.ssh/id_dsa type -1
debug1: identity file /Users/SurnameG/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA ax:64:3e:4a:e3:2c:e4:30:dd:36:a4:a0:9x:fa:ba:6b
debug1: Host '1.2.3.4' is known and matches the RSA host key.
debug1: Found key in /Users/SurnameG/.ssh/known_hosts:88
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/SurnameG/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Trying private key: /Users/SurnameG/.ssh/id_dsa
debug1: No more authentication methods to try.

奇怪的是,openssh 只提供 ~/.ssh/id_rsa,但这还不是全部。它还会“尝试” ~/.ssh/id_dsa?我不确定这两者(提供与尝试)之间的区别。无论如何,如果我没有看错的话,openssh 永远不会尝试我的任何其他私钥~/.ssh/*

好的,我知道我可以~/.ssh/config用类似这样的内容明确描述每个服务器

Host otherserver
   HostName 1.2.3.4
   User surnameg
   IdentityFile ~/.ssh/surnameg

然后登录

ssh otherserver

这很好,而且效果很好。但实际上,我的 ~/.ssh/config 变得笨重了。遗憾的是我cd ~/.ssh && git init很久以前没有这样做过。但我离题了。

我的问题是:是否有更简单、更快速、更自动化的方法让 ssh 在尝试登录时动态地尝试 ~/.ssh 目录中的更多密钥,或者编辑每个需要连接的服务器的 ~/.ssh/config 是配置 ssh 的唯一方法?我是否误解了上述有关 SSH 应该如何工作的内容?

答案1

如果要向所有主机提供相同的密钥,请使用 将它们加载到 SSH 代理中ssh-add。许多 Linux 发行版会自动启动一个代理 - 尝试ssh-add -l检查它是否正在运行,然后加载您的密钥:

ssh-add ~/.ssh/id_rsa ~/.ssh/surnameg etc.

如果代理未自动启动,请将以下内容输入到您的~/.profile

agent_running() {
    [ "$SSH_AUTH_SOCK" ] && { ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]; }
}

env=~/.ssh/agent.env
if ! agent_running && [ -s "$env" ]; then
    . "$env" >/dev/null
fi
if ! agent_running; then
    ssh-agent >"$env"
    . "$env" >/dev/null
    ssh-add ~/.ssh/id_*
fi    
unset env

答案2

还有另一种可能的方法。您可以在IdentityFile选项中使用占位符ssh_config。根据man ssh_config

  • %d—本地用户的主目录
  • %u—本地用户名
  • %l—本地主机名
  • %h—远程主机名
  • %r—远程用户名

我这样使用它(在全局ssh_config文件中):

IdentityFile ~/.ssh/%r@%h

这意味着我的私钥文件命名为[email protected]

相关内容