我正在尝试验证 GitLab 服务器的 SSH 指纹。它是公布的指纹是错误的,可能来自负载平衡。VerifyHostKeyDNS=yes
没有错误,但我不相信看不到指纹,并且希望在不连接的情况下这样做。
SSHFPSHA1 和 SHA256 为十六进制。我还希望将“4 2”之类的数字 ID 转换为“SHA256 (ED25519)”之类的英文名称。
$ resolvectl query -t sshfp ssh.gitlab.gnome.org
ssh.gitlab.gnome.org IN SSHFP 1 1 9d319c15965b939bc2086cbebca96d8bbb89e9a3 -- link: enp39s0
ssh.gitlab.gnome.org IN SSHFP 1 2 591469a16a3bb33333c2591815e2e88f4508b665477c023eafe845833c060118 -- link: enp39s0
ssh.gitlab.gnome.org IN SSHFP 3 1 17b29179778339d7c91cb2c281b7afa2798c61d5 -- link: enp39s0
ssh.gitlab.gnome.org IN SSHFP 3 2 4e203da68ea73ba8c5b28ef040dcb8c4b96d875d2521f8efd5dc6e0da6d522ac -- link: enp39s0
ssh.gitlab.gnome.org IN SSHFP 4 1 457e9cb0d575697e27fe8b2185580df5d45ce46b -- link: enp39s0
ssh.gitlab.gnome.org IN SSHFP 4 2 63d1b87568887e2e772d1dc89c95ac21bbf895e9141e89eafc7aea4d39bfadcc -- link: enp39s0
-- Information acquired via protocol DNS in 1.1133s.
-- Data is authenticated: yes; Data was acquired via local or encrypted transport: yes
-- Data from: network
然而,ssh-keyscan
为我提供 SHA1 和 SHA256 的 Base64。只有 SSHFP 中缺少的 MD5 才是十六进制。 SHA256 Base64也是git
连接时提示我的格式。我的用例是手动检查指纹,然后以某种方式将其信任到~/.ssh/known_hosts
.为简单起见,我只关心 ED25519。
$ ssh-keyscan ssh.gitlab.gnome.org | tee gnome.pub
# ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
ssh.gitlab.gnome.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHG6b3deoYMPwKEu9Sj+y6MBHYYUKQiAnta/go3aNv7R
# ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
ssh.gitlab.gnome.org ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCmUnZ33O26kvhcmc6k1T8xlL3yJjeNt3gphvW9/5u/CO0z5G8PGr2km4jUhStuPJR9m77LrdCF5Ywm26hsBnoxK+1zMs7YTjriiGt4r0Q5btQS5J5zFAV6aYJEmCbQz/23n8+w9Jx3hPVS8A3VE9Vd52S+5DMeIQY8QR3LPpROyCIpkR425sN0D0VjRUAH0hn61zS8N1+Pw5te2ULbBriBB9cub2n1GBCsZ2ErX1ernwKffOz/fbi5mpGbGLlJabLBm81/AiNk6NLZWTAZiNZjztxRySZFSpSzGF54kttIePxNC1x9AtW4KMaIzjVK2DIrFONyZYiEX47ox5TXlc7VQaBICUoTrR91wpb5xl2HPKP4MvFqUAzubU2V8r91ISXrnJyfwDrSB7L9Jwg6B6pN+acIttC4gOFGTJg9qanmLxtonSpwv/NaLehI1+D4LR9Or+e8jjtKSc6vu8+ghqAni3XHoT9uSmt1UPd89dOhqbpk8Hc3KTubUC3WIjGnotE=
# ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
ssh.gitlab.gnome.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBH8NuGQ3ArFGochlv6TKXhMhsgIwG743e93pLkAQTzfgds0g0k6QBimb40zavVjITjytiSaQsf+KncJgUjYd98o=
# ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
# ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
$ <gnome.pub ssh-keygen -lf -
3072 SHA256:WRRpoWo7szMzwlkYFeLoj0UItmVHfAI+r+hFgzwGARg ssh.gitlab.gnome.org (RSA)
256 SHA256:TiA9po6nO6jFso7wQNy4xLlth10lIfjv1dxuDabVIqw ssh.gitlab.gnome.org (ECDSA)
256 SHA256:Y9G4dWiIfi53LR3InJWsIbv4lekUHonq/HrqTTm/rcw ssh.gitlab.gnome.org (ED25519)
如何协调这两种格式?理想情况下,我想要传递一些标志ssh-*
或其他命令。
以下命令可能会给出正确的答案,但不可接受,因为它们太长而难以记住/键入,并且格式很糟糕。我的用例是在设置新计算机时运行一次,因此.bashrc
不会出现别名。
(x="$(<gnome.pub)"; for y in sha1 sha256; do echo "$x" | ssh-keygen -lf - -E "$y"; done) | xargs -d\\n -n1 bash -c "echo \"\$0\"; echo \"\$0\" | sed 's/[^:]\+:\([^ ]\+\).*/\1=/' | base64 -d | xxd -p -c 32"
resolvectl query -t sshfp ssh.gitlab.gnome.org | grep ' IN SSHFP ' | xargs -n1 -d\\n bash -c 'echo "$0"; echo "$0" | cut -d\ -f 6 | xxd -p -r | base64'
grep -v \# gnome.pub | cut -d\ -f2- | xargs -n1 -d\\n bash -c 'ssh-keygen -r ssh.gitlab.gnome.org -f <(echo "$0")'
答案1
要将 Base64 转换为十六进制,您可以使用perl
例如:
$ ssh-keygen -lf gnome.pub | perl -MMIME::Base64 -lpe 's{SHA256:\K\S+}{unpack("H*", decode_base64($&))}e'
256 SHA256:63d1b87568887e2e772d1dc89c95ac21bbf895e9141e89eafc7aea4d39bfadcc ssh.gitlab.gnome.org (ED25519)
3072 SHA256:591469a16a3bb33333c2591815e2e88f4508b665477c023eafe845833c060118 ssh.gitlab.gnome.org (RSA)
256 SHA256:4e203da68ea73ba8c5b28ef040dcb8c4b96d875d2521f8efd5dc6e0da6d522ac ssh.gitlab.gnome.org (ECDSA)
ssh-keyscan
还可以-D
选择将输出打印为 SSHFP 记录:
$ ssh-keyscan -D ssh.gitlab.gnome.org
; ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
ssh.gitlab.gnome.org IN SSHFP 1 1 9d319c15965b939bc2086cbebca96d8bbb89e9a3
ssh.gitlab.gnome.org IN SSHFP 1 2 591469a16a3bb33333c2591815e2e88f4508b665477c023eafe845833c060118
; ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
ssh.gitlab.gnome.org IN SSHFP 3 1 17b29179778339d7c91cb2c281b7afa2798c61d5
ssh.gitlab.gnome.org IN SSHFP 3 2 4e203da68ea73ba8c5b28ef040dcb8c4b96d875d2521f8efd5dc6e0da6d522ac
; ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
ssh.gitlab.gnome.org IN SSHFP 4 1 457e9cb0d575697e27fe8b2185580df5d45ce46b
ssh.gitlab.gnome.org IN SSHFP 4 2 63d1b87568887e2e772d1dc89c95ac21bbf895e9141e89eafc7aea4d39bfadcc
; ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
; ssh.gitlab.gnome.org:22 SSH-2.0-OpenSSH_8.0
并且ssh-keygen -r
可以为您提供给定公钥的 SSHFP 记录,因此您还可以执行以下操作:
$ perl -ne '
if (/^\s*+([^#]\S*)\s+(.*)/) {
open K, "|-", qw(ssh-keygen -f /dev/stdin -r), $1;
print K $2;
close K;
}' gnome.pub
ssh.gitlab.gnome.org IN SSHFP 4 1 457e9cb0d575697e27fe8b2185580df5d45ce46b
ssh.gitlab.gnome.org IN SSHFP 4 2 63d1b87568887e2e772d1dc89c95ac21bbf895e9141e89eafc7aea4d39bfadcc
ssh.gitlab.gnome.org IN SSHFP 1 1 9d319c15965b939bc2086cbebca96d8bbb89e9a3
ssh.gitlab.gnome.org IN SSHFP 1 2 591469a16a3bb33333c2591815e2e88f4508b665477c023eafe845833c060118
ssh.gitlab.gnome.org IN SSHFP 3 1 17b29179778339d7c91cb2c281b7afa2798c61d5
ssh.gitlab.gnome.org IN SSHFP 3 2 4e203da68ea73ba8c5b28ef040dcb8c4b96d875d2521f8efd5dc6e0da6d522ac