CentOs7 设置密钥登录、禁用密码、免密登录

步骤

一、密钥登录

  1. 在服务器创建密钥
cd .ssh/
ssh-keygen -t rsa -C "xxxxxxx@email.com" -f ./id_rsa

-f 指定路径, ./ 指当前路径

  1. 在服务器上安装公钥
cat id_rsa.pub >> authorized_keys
  1. 设置文件权限
chmod 600 authorized_keys
chmod 700 ~/.ssh

二、禁用密码

  1. 修改配置文件,打开秘钥登录功能
#备份
cp /etc/ssh/sshd_config  /etc/ssh/sshd_config.bak

#删除配置项
sed -i '/PasswordAuthentication.*/d' /etc/ssh/sshd_config
sed -i '/PubkeyAuthentication.*/d' /etc/ssh/sshd_config
sed -i '/RSAAuthentication.*/d' /etc/ssh/sshd_config
sed -i '/AuthorizedKeysFile.*/d' /etc/ssh/sshd_config

#追加配置项
cat >>/etc/ssh/sshd_config<<EOF
PasswordAuthentication no
PubkeyAuthentication yes
RSAAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

EOF
  1. 重启sshd服务
systemctl restart sshd

三、免密登录

# 安装sshpass
yum install -y sshpass

# 在当前服务器生成密钥对
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa <<< y
# 安装密钥对
cd /root/.ssh && cat id_rsa.pub >> authorized_keys
# 设置ssh_config
sed -i '/StrictHostKeyChecking/c StrictHostKeyChecking no' /etc/ssh/ssh_config

# 通过目标服务器的密码建立免密机制
sshpass -p ${target_password} ssh-copy-id  root@${target_ip} -p 22 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值