1. 在服务器上制作密钥对。
首先使用root或者其他账户登录服务器,然后执行以下命令:
## 创建密钥对
[root@host ~]$ ssh-keygen
Generating public/private rsa key pair.
## 指定存放目录,默认回车即可
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
## 设置密钥密码,不设密码则留空
Enter passphrase (empty for no passphrase):
## 再次输入
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
2. 安装公钥
键入以下命令,在服务器上安装公钥:
[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:
[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh
3. 设置 SSH,打开密钥登录功能
编辑 /etc/ssh/sshd_config 文件,进行如下设置:
RSAAuthentication yes
PubkeyAuthentication yes
另外,请留意 root 用户能否通过 SSH 登录:
PermitRootLogin yes
最后,重启 SSH 服务:
[root@host .ssh]$ service sshd restart
当你以密钥方式登录成功后,可以禁用密码登录(可选的):
PasswordAuthentication no
客户端导入
下载私钥文件/root/.ssh/id_rsa
至本地,导入密钥,
如果使用Xshell则无需转换密钥,使用Putty&WinSCP则需要把密钥文件转换为PPK