SSH远程证书登陆是使用"公私钥"认证的方式来进行SSH登录。
1、创建公私钥
创建方式有很多种,比如说通用ssh连接工具创建,然后把公钥上传到Server主机对应的用户目录下:
~/.ssh/authorized_keys
大家可以参考这里:http://www.aiezu.com/system/linux/xshell_ssh_public-key_login.html
这里我使用服务器自带的openssh来创建,以root用户为例:
//检查是否安装了openssh服务
rpm -qa|grep openssh //创建公私钥
ssh-keygen -t rsa
执行上边的语句后,开始进入引导设置:
Generating public/private rsa key pair.
//创建ssh目录,直接回车
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.
The key fingerprint is:
74:4a:c7:28:26:06:20:7d:62:40:8b:2e:fa:14:a8:1b root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|==. |
|o =.. o |
|.o oo o + + |
|.. . o + + |
|o.. S |
|+ . |
|E . |
| = |
|. . |
+-----------------+ //结束
执行上边的方法,我们的公私钥便创建完成啦,可以查看一下:
[root@localhost ~]# ls ~/.ssh/
id_rsa id_rsa.pub //id_rsa id 为私钥
//id_rsa.pub 为公钥
2、服务器配置
//打开配置文件
vim /etc/ssh/sshd_config
修改相应节点:
//启用 RSA 认证
RSAAuthentication yes
//启用公钥私钥配对认证方式
PubkeyAuthentication yes
//公钥文件路径
AuthorizedKeysFile .ssh/authorized_keys
//禁用密码登陆
PasswordAuthentication no
通过上边的设置可以看出公钥存储在 authorized_keys 文件里,我们需要将第一步生成的公钥写入到该文件,执行以下命令:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
注意:非root用户至少需要授于只读权限
chmod 400 ~/.ssh/authorized_keys
最后重启ssh服务
systemctl restart sshd.service
3、客户端配置
本地创建一个文件key.txt,下载服务端生成的私钥,打开并复制到本地key.txt文件中:
more ~/.ssh/id_rsa
注意:复制的时候请选择 -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- 全部内容,否则无法使用,例如:
-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEAuUD63UpzsyrPc5YmW0VdQmF3fCqlPJmddGkVwl2V2nv7QDZX
+oUan96XfABAms3Sp2LHKQHidHPDjgs/3hfIAsB/I8gBQZJupoA6DBJ2HHhoUD7O
FG99S/YTM4nMfu3BPD6yFDMyhMjKY4Ed38WEaESG6QeFMO5EDqlvJiGjY8vnUoFe
GEm3yQn3nOtxd3XHafeDdC0bt6CELs6x50zAhtvBogoeqDWQoCtAPcm4MxgZ8+wd
f2GKdAj6fBNuAVOi/ARGVnOGma4PYgBovFlue1xIZJ4iGpVVRKTBopWZpicOiBtm
5qRzKsk5hYoR2L8WFAOYJGmdwmUs15+K9T1uqQIDAQABAoIBAQCjsRQ6N1BWz4wx
cUqBOaB3SFJhB/mru30S4MwWS8VfIlBXY+2Hcteczf4C1uI/J4Fs4G5lAMJN7gCs
4anUoCTesaNmRZM+kpptSz5/bkypSYe66FQGYKFRXqWG08s7mPA3QJrwGHEaPaYA
GJYCJbCixXKENF3vZ4oBfpu1EOIkCgQp9Gsw3TzKXtieihynwf6PBVA4gRDCA8S/
p3cEDhFJUiI/n8WG6VRd59/+4lhFL6zpX8FRVtWFshaNg60I/+DrGxzDxG+1MbkA
BTkTpMq5evVsCDz/tJyyqBZTAv8pwluwn0bxoCEXQgE2FvpjJVl5qtfarWh3z7kB
1PVcIQjVAoGBAPNPRMqrtT7uUS8OvZf/bUHug+ENeJ/5mVJS/ZWPliLcNjAqJiek
0ye+T/XQxtOqJC2P4ds4+4dSkV+8jRsB47kC9r4Aa/6MhmfifQhXrUBwc54cQ8rj
grw2BSfd5gZNqrYvaYwZOJMkTx2ljTuCNrKECHwNQwMHWtG5OTyw6NB7AoGBAMLq
iRJPorQ1dXkzm795vuNrEayj8bEOUclpoyZctDflMEilj5p7/crslImSlOHXJHry
ING5CmOJIE2GMPZAmV4UNrQRAffTpMuhPgu6YLmoKPXvQk85ArnGFImxsETTY3aX
2pW09O2EAfFLSEZfO+KtDfvjNeLgPpVObTSk3B4rAoGBAL7vyAZcNYHLN758DOEJ
O85vxOKJ1c2E7IMkSYhjA+kbcxLdINAfku0vdkRrsHxOmtF+hjmpQAp0C01cBEfb
db+sycFVR2qdEKTZolE3rOhS1wiPGcxQOjpFEkq04lyz+nG6R1QAPtuJtOSJFIpM
p2v0HUxsg9gOn4DRoE82bZ2dAoGBAKLqF0F30F1hZVu1eBNVdehtYGt06Bl+B3wa
UGRLDdK+PFosD1gnFJycmxZVrx0IjpQ4dpf0WquRPK+vXpMksUvCB2r/4C489rvA
fLSexiPHWt0casb+trmBxz7DhrowGS1RhUCl7CXttRXyP2maRdqTnGGRqkOD/Ksc
zwg2V0jbAoGBAM8MWhHmA6elQbLD3x38Kkcn2YNOrOcgqV3ihDTX6/VWkJQ9MFhC
BnygVC/bA/0P+yhsivCfmS0YR1bfeTyfftlh7aprnfRNPsiAYwgWv2HM5we0qv7o
pocCUHuflpIAQxm/mxQObFty/ie4Wrme9OcCe0ksjaJNDpOwFzLWx9oh
-----END RSA PRIVATE KEY-----
打开xshell客户端,身份验证选择Public key方式,输入用户名,刚才我们使用的是root用户,所以这里输入root,用户密钥选择浏览,找到key.txt文件导入,密码如果刚才我们在创建密钥的时候输入了密码,那此处密码填写刚才设置的密码,如果没有设置,则不用填写。
至此,便可以连接接使用啦,安全又升级了!