多台服务器之间配置ssh免密登录

发布时间 2023-04-03 19:39:47作者: 一个安静的man

需求:假设有N台服务器,N台服务器之间都需要配置相互间免密登录

步骤1:在一台服务器上安装ansible

yum -y install epel-release &&  yum -y install ansible

步骤2:在这台服务器上生成密钥对

ssh-keygen

步骤3:生成authorized_keys文件,并修改为600权限

cp id_rsa.pub authorized_keys && chmod 600 authorized_keys

步骤4:ansible 关闭ssh首次连接时提示,关闭后不需要指纹验证

使用vim /etc/ansible/ansible.cfg编辑,将下面一行注释取消

host_key_checking = False

步骤5:编辑/etc/ansible/hosts,新增一个web组,添加要连接的服务ip,用户和密码,并测试连接是否成功

 

步骤6:使用ansbile批量新建.ssh文件夹,并设置权限

ansible web -m file -a "path=/root/.ssh state=directory mode=0700"

 

 

步骤7:使用ansbile拷贝authorized_keys到远端/root/.ssh/目录下,并设置权限

ansible web -m copy -a "src=/root/.ssh/authorized_keys dest=/root/.ssh mode=0600"

 

 

步骤8:使用ansbile拷贝id_rsa到远端/root/.ssh/目录下,并设置权限

ansible web -m copy -a "src=/root/.ssh/id_rsa dest=/root/.ssh mode=0600"

 

 

 步骤9:测试