「SSH」- 密钥管理

应用

密钥管理

ssh-keygen -t ed25519 -C “your_email@example.com” # -t 密钥类型 -C 注释

ssh-keygen -f ~/.ssh/id_rsa -y > ~/.ssh/id_rsa.pub

-y This option will read a private OpenSSH format file and print an OpenSSH public key to stdout.

清理 SSH 公钥

这绝对是常见现象:在 ~/.ssh/authorized_keys 中,存放着大量的公钥,而且没有注释,完全不知道这些公钥的用途。很多时候这些公钥并不是服务使用的,而是方便连接服务器而添加的。

我们今天的任务就是“清理”这些公钥,仅在服务器中保留必要的公钥信息。当然这和 JumpServer 没有关系,但是我们是借助 JumpServer 来完成该任务(使用其中的 Jobcenter/Batch command 功能,终究是在使用 Ansible 的功能)。

该笔记将记录:如何清理掉服务器上的 SSH 公钥,以及相关问题处理。

我们应该先找到服务器中被使用的公钥,这能够通过 SSH 登录日志来过滤,参考 Find out which ssh key used to access an account 笔记。

但是我们不想搞这么麻烦(偷懒),所以我们备份原来的 SSH KEY,然后将新的、仅有的、期望的公钥写入文件。这里没有什么技巧,仅是利用了 ~/.ssh/authorized_keys2 文件来“备份”,将新的密钥写入 ~/.ssh/authorized_keys 文件文件。

sudo -u root cp -v /root/.ssh/authorized_keys /root/.ssh/authorized_keys2
echo 'public key' | sudo -i tee /root/.ssh/authorized_keys

注意事项:
1)受 SSH 版本限制,在 CentOS 7.x 中,无法使用 ~/.ssh/authorized_keys2 文件