ssh-keygen踩坑

记录下这周发布的时候遇到了一个奇葩问题。

问题描述

使用ssh-keygen -t ecdsa -b 521 新生成了一对key,更新了服务器的authorized_keys,登录正常,但是cap deploy时,一直报错:

Net::SSH::Exception: Cannot decode private key of type ecdsa-sha2-nistp521

即使按照提示,安装了指定的gem 也没有效果。

gem 'ed25519'
gem 'bcrypt_pbkdf'

本机 OpenSSH 版本: 8.1。

解决方法

简单提一下中间的解决过程。

最开始的时候,采取了重新生成key的方式,bits 改为 256:

ssh-keygen -t ecdsa -b 256

结果:无效。

然后在老大的忽悠下,重装了系统。「成功满足了他老人家看机子不爽,孜孜不倦建议重装的诉求。」

重装后问题依然存在。

鉴于老大可以正常发布,对比了private key,得出的结论是format 不一致。

本机上private key 的format 使用的是 OpenSSH private key,也就是私钥的第一行是

-----BEGIN OPENSSH PRIVATE KEY-----

而老大的则是OpenSSL format,也就是private key 第一行以-----BEGIN EC PRIVATE KEY----- 打头。

OpenSSH Release Notes ,change log 中提到了,7.8+版本后默认生成的private key format 是OpenSSH,如果需要生成OpenSSL的PEM format,指定format为 PEM 即可。

ssh-keygen -t ecdsa -b 521 -m PEM

随后部署,OK。「但感觉还是懵逼的,服务器上sshd版本问题?只支持PEM format ?」

有关ssh-keygen

问题解决后,补一补 ssh-keygen 的小知识。

ssh-keygen 可以根据不同的算法生成key,使用 option -t 可以指定生成的key的type(rsa, dsa, ecdsa and ed25519) .

 -t dsa | ecdsa | ed25519 | rsa

默认使用的是rsa-sha2-512。

The available RSA signature variants are “ssh-rsa” (SHA1 signatures, not recommended), “rsa-sha2-256”, and “rsa-sha2-512” (the default)

四种算法 dsa | ecdsa | ed25519 | rsa 的对比,参考SSH-KEYGEN - GENERATE A NEW SSH KEY

rsa:

an old algorithm based on the difficulty of factoring large numbers. A key size of at least 2048 bits is recommended for RSA; 4096 bits is better.Choosing a different algorithm may be advisable. It is quite possible the RSA algorithm will become practically breakable in the foreseeable future. All SSH clients support this algorithm.

dsa:

an old US government Digital Signature Algorithm. It is based on the difficulty of computing discrete logarithms. A key size of 1024 would normally be used with it. DSA in its original form is no longer recommended.【不推荐使用】

ecdsa:

a new Digital Signature Algorithm standarized by the US government, using elliptic curves. This is probably a good algorithm for current applications. Only three key sizes are supported: 256, 384, and 521 (sic!) bits. We would recommend always using it with 521 bits, since the keys are still small and probably more secure than the smaller keys (even though they should be safe as well). Most SSH clients now support this algorithm.【可以使用,推荐521bits】

ed25519:

this is a new algorithm added in OpenSSH. Support for it in clients is not yet universal. Thus its use in general purpose applications may not yet be advisable.【新算法,支持不广泛,可以尝试使用】

例子:

ssh-keygen -t rsa -b 4096
ssh-keygen -t dsa
ssh-keygen -t ecdsa -b 521
ssh-keygen -t ed25519

生成的key,可以指定comment,比如:

ssh-keygen -t rsa -b 4096 -C '[email protected]'

这样生成的key 中comment 是指定的邮箱,而不是有关本机的用户信息。

默认情况下,使用ssh-keygen 生成的public 和 private key,会分别设置访问权限为644和600,这也是为什么,把本地的public key 添加到服务器后,需要自己手动 chmod, 给 authorized_keys 设置 644,而本机生成时则不用。

最后,说一个根据private key 生成 public key的小技巧。

如果你不小心丢了public key「虽然概率极低」,可以根据 private key 重新生成,比如:

key=~/.ssh/id_ecdsa
ssh-keygen -yf $key > $key.pub

不过需要多一句的是,根据 private key 重新生成的public key 中,comment 默认为空。我试过使用 -C 来指定 comment,但是无效,comment 依然为空,不过comment不影响使用key的正常使用,可以pass。

参考

SSH-KEYGEN - GENERATE A NEW SSH KEY

ssh-keygen

Unix and Linux System Administration Handbook