大网数据广告活动
服务器运维问答

Why does OpenSSH on Macos look for known hosts file twice?

· 52CE 我爱测

Open SSH debug trace shows that ssh is looking for the known_hosts file(s) twice, once before the key exchange and once during, why is that? I understand that as part of the key exchange ssh verifies the key in the known_hosts fi…

问题原文

原帖链接 · Niko P · CC BY-SA 4.0

Open SSH debug trace shows that ssh is looking for the known_hosts file(s) twice, once before the key exchange and once during, why is that?

I understand that as part of the key exchange ssh verifies the key in the known_hosts file, but I don't understand why it is trying to open them twice?

I've put a debug ssh example connection to github below and notice that debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory occurs twice.

ssh -T [email omitted] -v 
OpenSSH_8.6p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/foo_user/.ssh/config
debug1: /Users/foo_user/.ssh/config line 14: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/foo_user/.ssh/id_rsa type 0
debug1: identity file /Users/foo_user/.ssh/id_rsa-cert type -1
debug1: identity file /Users/foo_user/.ssh/id_ed25519 type 3
debug1: identity file /Users/foo_user/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.6
debug1: Remote protocol version 2.0, remote software version babeld-b9c2a189
debug1: compat_banner: no match: babeld-b9c2a189
debug1: Authenticating to github.com:22 as 'git'
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: rsa-sha2-512
debug1: kex: server->client cipher: [email omitted] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email omitted] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-rsa SHA256:mooSHmooSHmooSHmooSHmooSHmooSHmooSHmooSHmoo
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /Users/foo_user/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /Users/foo_user/.ssh/id_rsa RSA SHA256:fooIVfooIVfooIVfooIVfooIVfooIVfooIVfooIVfoo explicit agent
debug1: Will attempt key: /Users/foo_user/.ssh/id_ed25519 ED25519 SHA256:barEDbarEDbarEDbarEDbarEDbarEDbarEDbarEDbar explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<[email omitted],[email omitted],[email omitted],[email omitted],[email omitted],[email omitted],[email omitted],[email omitted],[email omitted],[email omitted],[email omitted],ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/foo_user/.ssh/id_rsa RSA SHA256:fooIVfooIVfooIVfooIVfooIVfooIVfooIVfooIVfoo explicit agent
debug1: Server accepts key: /Users/foo_user/.ssh/id_rsa RSA SHA256:fooIVfooIVfooIVfooIVfooIVfooIVfooIVfooIVfoo explicit agent
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([20.248.137.48]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: filesystem full
debug1: client_input_global_request: rtype [email omitted] want_reply 0
debug1: client_input_hostkeys: searching /Users/foo_user/.ssh/known_hosts for github.com / (none)
debug1: client_input_hostkeys: searching /Users/foo_user/.ssh/known_hosts2 for github.com / (none)
debug1: client_input_hostkeys: host key found matching a different name/address, skipping UserKnownHostsFile update
debug1: Sending environment.
debug1: channel 0: setting env LANG = "en_AU.UTF-8"
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0

已采纳解答

原帖链接 · petee · CC BY-SA 4.0

When UpdateHostKeys is enabled, after user is authenticated, it will be verified if there are host keys to be updated in ~/.ssh/known_hosts.

So for the first time ~/.ssh/known_hosts is read during authentication.
For the second time - to check if there are any host keys to be updated.

In provided output I can see [email omitted] want_reply - starting from this point ~/.ssh/known_hosts is accessed for the second time.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-07-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。