Linux与后台开发归档

LINUX ssh-rsa生成公私钥 实现免密登录

2019-06-13  本文已影响0人  M_ENG

将公司的所有服务器设置为密钥登录

1:首先生成所有服务器的公私钥

#mac 目录 /Users/用户名/.ssh  
#Linux 目录 /root/.ssh/
#进入 .ssh 文件 
cd /Users/用户名/.ssh
#执行生成密钥命令
ssh-keygen -t rsa -C "服务器IP@163.com"
Generating public/private rsa key pair.
#服务器IP_id_rsa 是输入的 密钥名称例如:47.105.216.12_id_rsa
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa): 服务器IP_id_rsa
# 直接enter
Enter passphrase (empty for no passphrase):  
# 直接enter
Enter same passphrase again:
#密钥生成结束

2:COPY 公钥到对于服务器

#执行命令
ssh-copy-id -i /Users/xxx/.ssh/服务器IP_id_rsa.pub root@服务器IP 
#输入密码
root@xxxxxx's password:

3:登录服务器设置开启密钥登录

vim /etc/ssh/sshd_config
#找到 RSAAuthentication  PubkeyAuthentication  没有就直接加上
RSAAuthentication yes
PubkeyAuthentication yes
#重启sshd服务  centos7
systemctl restart sshd.service

4:使用私钥登录服务器

#执行命令
#-i 指定私钥位置  
#不指定会到默认用户下的.ssh文件找id_rsa.pub文件进行登录,如果不对会跳出输入密码
ssh -i /Users/xxx/.ssh/服务器IP_id_rsa root@服务器IP
#登录成功

5:禁用密码登录

vim /etc/ssh/sshd_config
#找到 
PasswordAuthentication no
#重启sshd服务  centos7
systemctl restart sshd.service
上一篇 下一篇

猜你喜欢

热点阅读