1.证书创建,创建完后记得下载密钥文件【这里是root_rsa】
##在服务端创建公钥和私钥
使用root用户登录系统,并处于当前root用户默认的目录下。
[root@iZ28snxdn5mZ ~]# who
root pts/0 2019-12-03 14:06 (xxx.xxx.xxx.xxx)
[root@iZ28snxdn5mZ ~]# pwd
/root
[root@iZ28snxdn5mZ ~]#
执行ssh-keygen -t rsa命令创建公钥私钥文件。ssh-keygen -t rsa -C 'user@mail.com'
其中-t指定密钥类型,这里设置rsa即可,-c是密钥的注释.
[root@iZ28snxdn5mZ ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/root_rsa
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #设置密码(是证书私钥,非服务器登录密码)#
Enter same passphrase again: #设置密码(是证书私钥,非服务器登录密码)#
[root@iZ28snxdn5mZ ~]# ll .ssh
total 2
-rw------- 1 root root 1766 Dec 3 16:23 root_rsa
-rw-r--r-- 1 root root 399 Dec 3 16:23 root_rsa.pub
[root@iZ28snxdn5mZ ~]#
进入.ssh秘钥所在目录
[root@iZ28snxdn5mZ ~]# cd .ssh
[root@iZ28snxdn5mZ .ssh]# pwd
/root/.ssh
将公钥导入当前用户登录脚本中
[root@iZ28snxdn5mZ .ssh]# cat ./root_rsa.pub >> ./authorized_keys
[root@iZ28snxdn5mZ .ssh]# ll
total 12
-rw-r--r-- 1 root root 399 Dec 3 16:28 authorized_keys
-rw------- 1 root root 1766 Dec 3 16:23 root_rsa
-rw-r--r-- 1 root root 399 Dec 3 16:23 root_rsa.pub
设置用户登录秘钥和登录脚本权限
[root@iZ28snxdn5mZ ~]# pwd
/root
[root@iZ28snxdn5mZ ~]# chmod -R 700 .ssh
[root@iZ28snxdn5mZ .ssh]# chmod -R 644 .ssh/authorized_keys
修改ssh配置文件属性,详细参考说明打开把~别重复即可
[root@iZ28snxdn5mZ etc]# vi /etc/ssh/sshd_config
#StrictModes no
# 修改为no,默认为yes.如果不修改用key登陆是出现server refused our key(如果StrictModes为yes必需保证存放公钥的文件夹的拥有与登陆用户名是相同的.“StrictModes”设置ssh在接收登录请求之前是否检查用户家目录和rhosts文件的权限和所有权。这通常是必要的,因为新手经常会把自己的目录和文件设成任何人都有写权限。)
#PermitRootLogin yes #允许root认证登录
#PasswordAuthentication yes #允许密码认证
#Port 22 #ssh默认端口
#ListenAddress 0.0.0.0 #绑定监听IP
#AuthorizedKeysFile .ssh/authorized_keys #默认公钥存放的位置
#PubkeyAuthentication yes #允许公钥认证
#PasswordAuthentication yes #允许密码认证
StrictModes no
PubkeyAuthentication yes #打开证书公钥验证
AuthorizedKeysFile .ssh/authorized_keys #公钥存放位置
重启一下ssh服务
[root@iZ28snxdn5mZ .ssh]# systemctl restart sshd.service
2.打开Shell连接工具 ,连接切换Public key模式,然后密钥需要浏览文件,选择到下载的【root_rsa】导入进去
输入的密码是服务器生成root_rsa时填写的密码。ok后点确定即可【这里不带图了,可以看原文】