一、生成密钥对
[root@localhost ~]# ssh-keygen #输入指令生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):#指定密钥文件名,默认直接回车
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y #我这里已经存在密钥,所以选择覆盖
Enter passphrase (empty for no passphrase): #为密钥文件设置密码,空密码直接回车
Enter same passphrase again:
已成功生成密钥文件
二、在服务器安装公钥
[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
为文件和目录加权限
三、配置ssh,打开密钥登录功能
[root@host ~]$ vi /etc/ssh/sshd_config
打开文件进行配置
RSAAuthentication yes
PubkeyAuthentication yes
PermitRootLogin yes
找到以上字符串进行设置,保存并退出文件配置
[root@host ~]$systemctl restart sshd.service
重启一下ssh服务
四、将私钥下载到客户端
使用MobaXterm密码登陆在.ssh文件夹中找到私钥文件下载到本地
五、使用私钥连接ssh
在MobaXterm新建一个会话
在高级设置里导入我们的私钥文件,确认并连接即可
六、设置禁止密码登录
[root@host ~]$ vi /etc/ssh/sshd_config
进入配置文件
PasswordAuthentication no
设置禁止密码登录