Centos7下-使用密钥认证方式登入服务器
SecureCRT免密ssh登录CentOS7
ssh服务支持之中安全认证机制,就是密钥登录,这种方式是比较安全的登入方式。
因为一般的密码方式登录容易被密码暴力破解,使用密钥方式登入主机也是首推一种登入方式,比使用密码的方式登录更佳!
注意事项
PS:在还没有验证使用私钥可以登入系统之前,建议开启密码登入和密钥登入两种方式,等验证使用密钥登入成功后,再关闭对应的禁止root登入和使用密码登录系统的方式。
避免使用密钥无法登入,但是又关闭了密码登入的方式!
前提准备
开始实验之前,需要在centos上安装openssh并启动sshd服务。
yum install openssh
systemctl start sshd
配置步骤
-
使用ssh-keygen生成公钥和私钥
ssh-keygen -t rsa
PS: Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: 经过上面三次回车后,最后在/root/.ssh/id_rsa下面生成了公钥和私钥 |
-
经过上面三次回车后,最后在/root/.ssh/id_rsa下面生成了公钥和私钥
[root@lenovo ~]# cd /root/.ssh
[root@lenovo .ssh]# ll
total 8
-rw-------. 1 root root 1679 May 24 17:33 id_rsa
-rw-r--r--. 1 root root 393 May 24 17:33 id_rsa.pub
-
在服务器上安装公钥
[root@lenovo .ssh]# chmod 700 /root/.ssh
[root@lenovo .ssh]# cat id_rsa.pub authorized_keys
[root@lenovo .ssh]# touch authorized_keys
[root@lenovo .ssh]# cat id_rsa.pub>>authorized_keys
[root@lenovo .ssh]# ll
total 12
-rw-r--r--. 1 root root 393 May 24 17:45 authorized_keys
-rw-------. 1 root root 1679 May 24 17:33 id_rsa
-rw-r--r--. 1 root root 393 May 24 17:33 id_rsa.pub
PS:因为我们是使用ssh-keygen -t rsa方式生成相关密匙信息,所以此时默认的会有对应的:/root/.ssh/这个目录存在,如果没有的话(即使用其他ssh工具生成密钥的时候)就需要收的创建此目录.。
-
修改SSH配置,打开密钥登录功能
[root@lenovo .ssh]# vi /etc/ssh/sshd_config
PubkeyAuthentication yes # 开启密钥登入的认证方式 |
-
重启SSH服务
[root@lenovo .ssh]# systemctl restart sshd
-
把私钥提取出来保存到本地,然后使用SecureCRT登录
pscp root@linux的IP地址:/root/.ssh/id_rsa ./