2019年10月17日

ssh报"no hostkey alg"错误

[2019-10-17 Thu 17:07]

1.1.1.1 ssh 2.2.2.2"no hostkey alg"

[2019-10-17 Thu 16:40] 应该是 2.2.2.2/etc/ssh 中的key的权限不对,只有 ssh_host_ed25519_keyroot:root ,所以只能用ed25519,但是client是5.0的版本,没有ed25519。

先检查了user用户的.ssh权限和.ssh目录下的文件权限是ok的。

$HOME/.ssh 中有没有 id_rsa 都可以。没有的话用 /etc/ssh/ssh_host_rsa_key/etc/ssh/ssh_host_rsa_key.pub ,没办法测试,不知道属主不是root:root会不会有问题。

[user@1.1.1.1 ~]$ ls -ld .ssh
drwxr-xr-x 2 user user     4096 Mar 25  2018 .ssh

[user@1.1.1.1 ~]$ chmod 700 .ssh

[user@1.1.1.1 ~]$ ls -ld .ssh
drwx------ 2 user user     4096 Mar 25  2018 .ssh

在1.1.1.1上 ssh -vvv 2.2.2.2 的报错log

ssh -vvv 2.2.2.2的报错

[user@1.1.1.1 ~]$ ssh -vvv 2.2.2.2
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to 2.2.2.2 [2.2.2.2] port 22.
debug1: Connection established.

debug1: match: OpenSSH_8.0 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug2: fd 3 setting O_NONBLOCK
...
debug2: mac_setup: found hmac-sha1
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug2: mac_setup: found hmac-sha1
debug1: kex: client->server aes128-ctr hmac-sha1 none
no hostkey alg # 这里报错了

ssh -vvv 3.3.3.3 正常的log

[user@1.1.1.1 ~]$ ssh -vvv 3.3.3.3
...
debug2: mac_setup: found hmac-sha1
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug2: mac_setup: found hmac-sha1
debug1: kex: client->server aes128-ctr hmac-sha1 none
# 能看见下面的log,就不会报 "no hostkey alg" 了
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<2048<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug3: Wrote 24 bytes for a total of 1005
debug2: dh_gen_key: priv key bits set: 160/320
...

指定HostKeyAlgorithms用ssh-rsa,报错。

ssh的hostkey虽然可以支持多种算法,但是从debug log看,服务端指定用 ed25519 ,所以报 "no hostkey alg" 错误。

ssh -vvv 的debug log,注意hostkey用的是哪个算法。

ssh指定 hostkey alg 参考: https://blog.qiqitori.com/?p=436

# ssh v5.0不支持ecdsa,具体可以man ssh-keygen
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
ssh -o HostKeyAlgorithms=ecdsa-sha2-nistp256,ssh-rsa 12.34.56.78

在client(1.1.1.1) ssh -vvv 时看见,5.0的版本比较低,5.0不能用 ssh -Q key 查看支持的hostkey算法。8.0可以。

[user@1.1.1.1 ~]$ ssh -o HostKeyAlgorithms=ssh-rsa 2.2.2.2 
...
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: kex_parse_kexinit: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1
debug2: kex_parse_kexinit: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
# 这是报错的,使用了hostkey ssh-ed25519,ssh 5.0是没有这个算法的
debug2: kex_parse_kexinit: ssh-ed25519
...

在一个安装了8.0版本的ssh的机器上, ssh-keygen -t ed25519 生成 id_ed25519和id_ed25519.pub ,然后copy到1.1.1.1上,ssh报错,ssh 5.0不支持ed25519

[user@1.1.1.1 .ssh]$ ssh -o HostKeyAlgorithms=ssh-ed25519 2.2.2.2
command-line line 0: Bad protocol 2 host key algorithms 'ssh-ed25519'.
# 这个vm的ssh是8.0的,可能用不了ed25519,但是可以用rsa和dsa,权限ok
[root@iZi9p0685 ~]# ls -tlr /etc/ssh
total 284
-rw-r--r-- 1 root root       2208 Apr 12  2017 ssh_config
-rw-r--r-- 1 root root     242153 Apr 12  2017 moduli
-rw------- 1 root root       4122 Jul 29 22:59 sshd_config
-rw-r--r-- 1 root root        410 Jul 29 23:01 ssh_host_rsa_key.pub
-rw------- 1 root root       1675 Jul 29 23:01 ssh_host_rsa_key
-rw------- 1 root root        668 Jul 29 23:01 ssh_host_dsa_key
-rw-r--r-- 1 root root        618 Jul 29 23:01 ssh_host_dsa_key.pub
-rw-r--r-- 1 root root        190 Jul 29 23:01 ssh_host_ecdsa_key.pub
-rw------- 1 root root        227 Jul 29 23:01 ssh_host_ecdsa_key
-rw-r--r-- 1 root root         82 Jul 29 23:01 ssh_host_ed25519_key.pub
-rw-r----- 1 root ssh_keys    387 Jul 29 23:01 ssh_host_ed25519_key

确认 ssh 2.2.2.2 用的是ed25519。

[root@iZi9p0685 ~]# cat ~/.ssh/known_hosts
...

2.2.2.2 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHxOfSn4HtkwHW5056lpu0/Dw81IwNjMFXLyeaR6InCZ

...

hostkey的顺序是怎么确定的? 为什么是 ssh-rsa 在前面?

[root@iZi9p0685 ~]# ssh -Q key
ssh-rsa
ssh-dss
ssh-ed25519
ecdsa-sha2-nistp256
ecdsa-sha2-nistp384
ecdsa-sha2-nistp521
ssh-rsa-cert-v01@openssh.com
ssh-dss-cert-v01@openssh.com
ecdsa-sha2-nistp256-cert-v01@openssh.com
ecdsa-sha2-nistp384-cert-v01@openssh.com
ecdsa-sha2-nistp521-cert-v01@openssh.com
ssh-rsa-cert-v00@openssh.com
ssh-dss-cert-v00@openssh.com
ssh-ed25519-cert-v01@openssh.com
null