服务器ssh_key授权,并禁止密码登陆

来源:互联网 发布:双系统平板删掉windows 编辑:程序博客网 时间:2024/06/05 06:42

·生成rsa_key , 并添加公钥到服务器

windows:

下载XSHELL,在登陆的时候可以选择用密钥登陆, 然后直接生成,保存可以得到rsa_key.pub。

linux or Mac:

1、生成SSH密钥对

ssh-keygen -t rsa

2、建议直接回车使用默认路径

Enter file in which to save the key (/root/.ssh/id_rsa):

3、输入密码短语(留空则直接回车)

Enter passphrase (empty forno passphrase):

4、重复密码短语

Enter same passphrase again:

最后会显示生成的路径

Your identification has been saved in/root/.ssh/id_rsa.Yourpublic key has been saved in/root/.ssh/id_rsa.pub.

·添加res_key到服务器

打开刚才生成的.pub文件,复制里面内容 , 在服务器里面输入

echo "XXX(.pub里面所有的内容)" >> /root/.ssh/authorized_keys

·禁止密码登陆,并启动ssh_key验证

1、编辑sshd_config文件

vi /etc/ssh/sshd_config

2、禁用密码验证

PasswordAuthentication no

3、启用密钥验证

RSAAuthentication yesPubkeyAuthentication yes

重启服务

RHEL/CentOS系统:service sshd restartUbuntu系统:service ssh restartdebian系统:/etc/init.d/ssh restart
0 0