24.SSH免密登录-分发公钥脚本

01.管理端创建秘钥对信息
02.管理端需要将公钥进行分发

注意:1.管理端的“id_rsa.pub”文件与被管理端的“authorized_keys”文件内容一致
2.可以不使用“ssh-copy-id”命令分发密钥,使用scp、ftp等命令也可以,使用“ssh-copy-id”命令是为了安全传输

03.进行远程连接测试

# 不写主机名时(root@xxx.xxx.xxx.xxx)时,默认使用的是当前客户端登录的用户名
ssh 192.168.10.77 -p 2222

04.SSH服务配置文件

    /etc/ssh/sshd_config
    Port 22                   --- 修改服务端口信息
    ListenAddress 0.0.0.0     --- 监听地址 指定一块网卡能够接受远程访问请求  *****
                                  PS: 指定监听地址只能是本地网卡上有的地址
    PermitEmptyPasswords no   --- 是否允许远程用户使用空密码登录,默认不允许
    PermitRootLogin yes       --- 是否禁止root用户远程连接主机 建议改为no
    GSSAPIAuthentication no   --- 是否开启GSSAPI认证功能 不用的时候关闭 
    UseDNS no                 --- 是否开启反向DNS解析功能 建议进行关闭
# 如果需要密钥登录需要修改以下配置,并禁止ROOT用户登录
     PubkeyAuthentication yes  --- 开启密钥验证
     AuthorizedKeysFile   .ssh/authorized_keys --- 默认公钥存放的位置
    

05.重启sshd服务

批量分发公钥脚本

cat ssh-distribution-pubkeys.sh
#!/bin/bash
# Author:
# Public key distribution script
# time: 2022-04-13
host_pre=192.168.10.
port=2222
password=123456
. /etc/init.d/functions
if [ ! -f /root/.ssh/id_rsa.pub ] ;then
ssh-keygen -f /root/.ssh/id_rsa -N ''|echo ''
fi
for ip in {66..188}
do
  echo "==================== host $host_pre$ip start distributing pub-keys    ==================== "
  sshpass -p$password ssh-copy-id -i /root/.ssh/id_rsa.pub root@$host_pre$ip -p $port "-o StrictHostKeyChecking=no" &>/dev/null
  if [ $? -eq 0 ] ; then
    action "host $host_pre$ip distribution is" /bin/true
    echo ""
  else
    action "host $host_pre$ip distribution is" /bin/false
    echo "==================== host $host_pre$ip Finished distributing pub-keys ==================== "
    echo ""
  fi
done

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容