弹性云主机

如何更换密钥对

2025-11-06 07:32:59

问题描述

更换云主机密钥对后,无法使用新的密钥对登录云服务器。

处理方法

1.  先登录Linux云主机上制作密钥对,登录到云主机,执行以下命令,生成密钥对。

[root@host ~]$ ssh-keygen  <== 建立密钥对
Generating public/private rsa key pair.
Enter ile in which to save the key (/root/.ssh/id_rsa): <== 按 Enter
Created directory ´/root/.ssh´ .
Enter passphrase (empty for no passphrase): <== 输入密钥锁码,或直接按 Enter 留空
Enter same passphrase again: <== 再输入一遍密钥锁码
Your identiication has been saved in /root/.ssh/id_rsa. <== 私钥
Your public key has been saved in /root/.ssh/id_rsa.pub. <== 公钥
The key ingerprint is:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host

2.  在使用私钥时输入密钥锁码,可以保护私钥不被盗用,在/root/.ssh/下面生成了两个密钥文件:id_rsa 为私钥,id_rsa.pub
为公钥。

 

3.  添加公钥。

[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
 

4.  修改.ssh、authorized_keys文件权限。

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh

5.  (可选)打开密钥登录功能。

RSAAuthentication yes
PubkeyAuthentication yes

6.  打开允许root用户登录配置。

PermitRootLogin yes

7.  完成上述配置后,重启sshd服务,以密钥方式登录成功后,再禁用密码登录。

PasswordAuthentication no

8.  重启ssh服务。

systemctl restart sshd
q8yEfqu1.LYn