设置SSH通过密钥登录

制作密钥对

1
ssh-keygen -t rsa

在服务器上安装公钥

1
2
3
4
5
6
7
//服务器
ssh root@192.168.1.129
mkdir ~/.ssh
//本地
scp ~/.ssh/id_rsa.pub root@192.168.1.129:~/.ssh/
//服务器
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

设置SSH,打开密钥登录功能

编辑 /etc/ssh/sshd_config 文件,进行如下设置:

RSAAuthentication yes
PubkeyAuthentication yes

另外,请留意 root 用户能否通过 SSH 登录:

PermitRootLogin yes

当你完成全部设置,并以密钥方式登录成功后,再禁用密码登录:

PasswordAuthentication no

最后,重启 SSH 服务:

service sshd restart