linux centos设置密钥并且使用密钥登录

服务器端生产密钥(一直默认回车就好,当然你也可以重新命名):
[root@localhost ~]# ssh-keygen -t rsa

然后会在 root目录下自动生产.ssh目录和公钥(id_rsa.pub)私钥(id_rsa)
[root@localhost ~]# cd /root/.ssh
[root@localhost .ssh]# ll
total 8
-rw——- 1 root root 1675 Jun 8 19:09 id_rsa
-rw-r–r– 1 root root 408 Jun 8 19:09 id_rsa.pub

新建一个公钥文件,设置权限为600
[root@localhost .ssh]# touch authorized_keys
[root@localhost .ssh]# chmod 600 authorized_keys

将公钥拷贝到authorized_keys
[root@localhost .ssh]# cat id_rsa.pub >> authorized_keys

修改配置
[root@localhost /]# vi /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
重启服务(只有root有权限)
[root@localhost /]# service sshd restart

将私钥id_rsa拷贝到我们的本地(可以使用xshell ,WinSCP)

 

 

到现在,可以用密钥登录,但是密码还是能登录,所以要去掉密码登录。
修改配置(普通用户)PermitRootLogin yes
[root@localhost /]# vi /etc/ssh/sshd_config
PasswordAuthentication no
重启服务(只有root有权限)
[root@localhost /]# service sshd restart
修改配置(root用户)
[root@localhost /]# vi /etc/ssh/sshd_config
PermitRootLogin no
重启服务(只有root有权限)
[root@localhost /]# service sshd restart

你可能感兴趣的:(centos,linux)