1. 问题描述
更换云服务器密钥对,删除旧的密钥对。
2. 处理方法
登录Linux云服务器上制作密钥对,首先用密码或旧的密钥登录到待更换密钥对的云服务器,然后执行以下命令:
[root@host ~]$ ssh-keygen <== 建立密钥对
Generating public/private rsa key pair.
Enter file 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 identification has been saved in /root/.ssh/id_rsa. <== 私钥
Your public key has been saved in /root/.ssh/id_rsa.pub. <== 公钥
The key fingerprint is:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host
密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。现在,在 root 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件:id_rsa 为私钥,id_rsa.pub 为公钥。在Linux云服务器上执行以下命令安装公钥。
$ cd .ssh
$ cat id_rsa.pub >> authorized_keys
为了确保连接成功,请保证以下文件权限正确。
$ chmod 600 authorized_keys
$ chmod 700 ~/.ssh
(可选)设置 SSH,打开密钥登录功能(使用公共镜像创建的Linux云服务器默认是开启的),编辑 /etc/ssh/sshd_config 文件,进行如下设置:
RSAAuthentication yes
PubkeyAuthentication yes
检查如下配置项,确保root用户能通过SSH登录:
PermitRootLogin yes
完成上述配置后,并以密钥方式登录成功后,再禁用密码登录:
PasswordAuthentication no
重启SSH服务。
service sshd restart