mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6
214 字
1 分钟
SSH 密钥轮换
2026-03-27

SSH 密钥轮换#

SSH 密钥不要直接覆盖旧文件,正确做法是:生成新密钥 -> 添加公钥 -> 测试登录 -> 删除旧公钥

1. 生成新密钥#

ssh-keygen -t ed25519 -a 64 -C "remark"

建议保存为新文件,例如:

~/.ssh/id_ed25519_new

生成后会得到:

  • 私钥:~/.ssh/id_ed25519_new
  • 公钥:~/.ssh/id_ed25519_new.pub

2. 将新公钥加入服务器#

二选一。

方法一:ssh-copy-id#

ssh-copy-id -i ~/.ssh/id_ed25519_new.pub user@host

方法二:手动追加到 authorized_keys#

普通用户:

vim ~/.ssh/authorized_keys

root 用户:

sudo vim /root/.ssh/authorized_keys

把 id_ed25519_new.pub 的内容追加进去。

3. 使用新密钥测试登录#

ssh -i ~/.ssh/id_ed25519_new user@host

如果登录成功,说明新密钥已经生效。

4. 可选:更新本地 SSH 配置#

编辑 ~/.ssh/config:

Host myserver
HostName host
User user
IdentityFile ~/.ssh/id_ed25519_new
IdentitiesOnly yes
ForwardAgent yes

之后直接连接:

ssh myserver

5. 删除旧公钥#

确认新密钥可用后,登录服务器,编辑:

vim ~/.ssh/authorized_keys

或:

sudo vim /root/.ssh/authorized_keys

删除旧公钥对应的那一行。

分享

如果这篇文章对你有帮助,欢迎分享给更多人!

SSH 密钥轮换
https://blog.sleep0.de/posts/ssh-keygen/
作者
Sleep1223
发布于
2026-03-27
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录