ssh_config 主机 * 覆盖之前的主机

und*_*ned 5 osx ssh ssh-config

我试图让我的 .ssh/config 支持同一主机的不同 ssh 密钥,以便我可以作为我的个人或工作用户提交到 bitbucket,而其他 ssh 内容仍然使用我的工作用户。

\n

我的配置文件如下所示:

\n
Host bitbucket-personal\n HostName bitbucket.com\n User git\n IdentityFile ~/.ssh/personal\n IdentitiesOnly yes\n\nHost *\n AddKeysToAgent yes\n UseKeychain yes\n IdentityFile ~/.ssh/work\n
Run Code Online (Sandbox Code Playgroud)\n

当我ssh bitbucket-personal使用的~/.ssh/work密钥不是我所期望的(参见下面的文档,它应该使用第一个匹配的 IdentityFile)。但所有其他参数都被正确引用(例如git@bitbucket.com)。如果我删除 Host * 部分,它将使用正确的密钥。

\n

我究竟做错了什么?我猜我误解了这里的优先级是如何工作的。

\n
\n

对于每个参数,将使用第一个获得的值。配置文件包含由 \xe2\x80\x9cHost\xe2\x80\x9d 规范分隔的部分,并且该部分仅适用于与规范中给出的模式之一匹配的主机。匹配的主机名是命令行上给出的主机名。

\n

由于使用每个参数的第一个获得的值,因此应在文件开头附近给出更多特定于主机的声明,并在末尾给出一般默认值。

\n
\n

下面是完整详细的跟踪:

\n
luke$ ssh -v bitbucket-personal\nOpenSSH_7.8p1, LibreSSL 2.7.3\ndebug1: Reading configuration data /Users/luke/.ssh/config\ndebug1: /Users/luke/.ssh/config line 1: Applying options for bitbucket-personal\ndebug1: /Users/luke/.ssh/config line 7: Applying options for *\ndebug1: Reading configuration data /etc/ssh/ssh_config\ndebug1: /etc/ssh/ssh_config line 48: Applying options for *\ndebug1: Connecting to bitbucket.com port 22.\ndebug1: Connection established.\ndebug1: identity file /Users/luke/.ssh/luke type 0\ndebug1: identity file /Users/luke/.ssh/luke-cert type -1\ndebug1: identity file /Users/luke/.ssh/luke-cx type 0\ndebug1: identity file /Users/luke/.ssh/luke-cx-cert type -1\ndebug1: Local version string SSH-2.0-OpenSSH_7.8\ndebug1: Remote protocol version 2.0, remote software version conker_1.1.15-49a70a8 app-154\ndebug1: no match: conker_1.1.15-49a70a8 app-154\ndebug1: Authenticating to bitbucket.com:22 as 'git'\ndebug1: SSH2_MSG_KEXINIT sent\ndebug1: SSH2_MSG_KEXINIT received\ndebug1: kex: algorithm: curve25519-sha256@libssh.org\ndebug1: kex: host key algorithm: ssh-rsa\ndebug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none\ndebug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none\ndebug1: expecting SSH2_MSG_KEX_ECDH_REPLY\ndebug1: Server host key: ssh-rsa SHA256:qqq\ndebug1: Host 'bitbucket.com' is known and matches the RSA host key.\ndebug1: Found key in /Users/luke/.ssh/known_hosts:52\ndebug1: rekey after 134217728 blocks\ndebug1: SSH2_MSG_NEWKEYS sent\ndebug1: expecting SSH2_MSG_NEWKEYS\ndebug1: SSH2_MSG_NEWKEYS received\ndebug1: rekey after 134217728 blocks\ndebug1: SSH2_MSG_SERVICE_ACCEPT received\ndebug1: Authentications that can continue: publickey\ndebug1: Next authentication method: publickey\ndebug1: Offering public key: RSA SHA256:qqqq /Users/luke/.ssh/luke-cx\ndebug1: Server accepts key: pkalg ssh-rsa blen 535\ndebug1: Authentication succeeded (publickey).\nAuthenticated to bitbucket.com ([18.205.93.3]:22).\ndebug1: channel 0: new [client-session]\ndebug1: Entering interactive session.\ndebug1: pledge: network\ndebug1: Sending environment.\ndebug1: Sending env LANG = en_NZ.UTF-8\nPTY allocation request failed on channel 0\ndebug1: client_input_channel_req: channel 0 rtype exit-status reply 0\nlogged in as lukemcgregor-x.\n
Run Code Online (Sandbox Code Playgroud)\n

Jak*_*uje 1

我假设您正在使用 ssh-agent 或类似的东西来缓存解密的私钥,这可能会改变提供密钥的顺序。这在更详细的日志级别(具有多个)中可见-v

此外,该IdentitiesOnly选项的含义与您想象的略有不同 - 它将限制将提供给服务器的密钥,但不会将其限制为该选项之前的密钥。它主要用于避免在自动加载的默认位置提供身份 ( ~/.ssh/id_{rsa,dsa,ecdsa,ed25519})。

IdentitiyFile选项也与您假设的略有不同。允许多次指定(请参阅手册页),因此也Match *将使用第 1 节中的那个。

如果您想默认使用某个密钥,而其他密钥仅用于该特定主机,请将“默认”密钥移至默认位置~/.ssh/id_rsa并将其从配置文件中删除。它应该可以解决你的问题。