指定 SSH KexAlgorithms 在 CLI 中有效,但不能通过 ssh_config

3 ssh openssh

默认情况下,我的 SSH 客户端不允许使用diffie-hellman-group-exchange-sha256密钥交换算法。但是,我需要访问 10.0.0.1 上需要使用该算法的服务器。

这在命令行上工作正常:

$ ssh -o KexAlgorithms=diffie-hellman-group-exchange-sha256 user@10.0.0.1
Password:
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试在 末尾添加以下内容,则会失败/etc/ssh/ssh_config

Host 10.0.0.1
    KexAlgorithms diffie-hellman-group-exchange-sha256
Run Code Online (Sandbox Code Playgroud)

这是相关的输出:

$ ssh -vvv user@10.0.0.1
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug3: kex names ok: [curve25519-sha256@libssh.org]
...
debug1: /etc/ssh/ssh_config line 72: Applying options for 10.0.0.1
debug3: kex names ok: [diffie-hellman-group-exchange-sha256]
...
debug1: Connecting to 10.0.0.1 [10.0.0.1] port 22.
debug1: Connection established.
...
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: curve25519-sha256@libssh.org
...
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: kex_parse_kexinit: diffie-hellman-group-exchange-sha256
...
debug2: kex_parse_kexinit: first_kex_follows 0 
debug2: kex_parse_kexinit: reserved 0 
debug2: mac_setup: setup hmac-ripemd160
debug1: kex: server->client aes256-ctr hmac-ripemd160 none
debug2: mac_setup: setup hmac-ripemd160
debug1: kex: client->server aes256-ctr hmac-ripemd160 none
Unable to negotiate a key exchange method
Run Code Online (Sandbox Code Playgroud)

我对此感到困惑的是,SSH 显然正在阅读相关行/etc/ssh/ssh_config并且似乎对此感到满意。但随后它尝试使用curve25519-sha256@libssh.org而不是与服务器协商密钥交换diffie-hellman-group-exchange-sha256,这当然失败了。

为什么会这样,我该如何纠正?

Jak*_*uje 5

乍一看,OpenSSH 选项的行为可能有些奇怪。但是手册页的ssh_config文档很好:

对于每个参数,将使用第一个获得的值。配置文件包含由“主机”规范分隔的部分,该部分仅适用于与规范中给出的模式之一匹配的主机。匹配的主机名通常是命令行上给出的主机名(有关例外情况,请参阅 CanonicalizeHostname 选项。)

你可以像这样重写你的配置来实现你所需要的(明星*匹配应该在最后):

Host 10.0.0.1
    KexAlgorithms diffie-hellman-group-exchange-sha256
#[...]
Host *
    KexAlgorithms curve25519-sha256@libssh.org
Run Code Online (Sandbox Code Playgroud)

从我的重复答案

并解释为什么命令行选项有效,同样来自相同的手册页ssh_config

  1. 命令行选项
  2. 用户的配置文件 (~/.ssh/config)
  3. 系统范围的配置文件 (/etc/ssh/ssh_config)