配置使用SSH公钥、私钥登陆

1、root用户登陆后,运行以下第一句指令,其他根据提示进行输入:

ssh-keygen -t rsa

2、使用以下命令将公钥导入到VPS:

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

3、修改SSH的配置文件/etc/ssh/sshd_config :

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile .ssh/authorized_keys

去调上面3行前面的#,保存后重启SSH服务。

service sshd restart

4、关闭root用户使用密码登陆(测试能登录),修改SSH的配置文件/etc/ssh/sshd_config,找到下面1行:

PasswordAuthentication yes

修改为:

PasswordAuthentication no

保存后重启SSH服务。

service sshd restart

保存好你的私钥(为安全,建议删除服务器端的私钥)

sed -i 命令快速设置sshd_config

行尾加入3行设置

sed -i '$a RSAAuthentication yes' /etc/ssh/sshd_config
sed -i '$a PubkeyAuthentication yes' /etc/ssh/sshd_config
sed -i '$a AuthorizedKeysFile .ssh/authorized_keys' /etc/ssh/sshd_config

关闭root用户使用密码登陆

sed -i "s/PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config

重装系统后将authorized_key上传至/root/.ssh,重启sshd服务即可

mkdir .ssh
cd .ssh/