## 前言
---
因为ssh用密码登录不太安全,容易被爆破,使用某些工具登录每次都要输入密码,不方便,所以现在一般都禁止用密码登录,而使用key登录。
## key创建和设置
---
首先,要创建一个key来代替密码,其实就是创建一对公钥、私钥。
`ssh-keygen -t rsa`
然后,拷贝公钥到远程要设置的主机上。
用法:
> Usage: ssh-copy-id [-h|-?|-n] [-i [identity_file]] [-p port] [[-o ] ...] [user@]hostname
` ssh-copy-id -i ~/.ssh/id_rsa -p ssh端口 root@主机ip`
测试:
`ssh root@ip #-i 参数加key私钥的路径,如果key的私钥存在其他地方`
如果设置正确,现在不用密码直接登录了。
## 禁止密码登录
---
vim /etc/ssh/sshd_config
#将PasswordAuthentication yes
#改为PasswordAuthentication no
#将ChallengeResponseAuthentication yes
#改为ChallengeResponseAuthentication no
然后需要重启sshd服务生效
`service sshd restart`
## 题外话
---
默认设置ssh为支持key登录,以防万一还是要检查一下
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys