[提供可行性脚本] RHEL/CentOS 7 多节点SSH免密登陆
实验说明:
在自动化部署时,会经常SSH别的机器去操作,然而每次的密码认证却很令人烦躁,尤其是很长的密码,因此SSH免密登陆就显得必不可少;
在机器数目很多的时候,使用更过的往往是Ansible分发并执行SSH免密登陆脚本,使得每台机器之间都能免密登陆。
实验环境:
宿主机系统 :Fedora 28 WorkStation
虚拟机管理器 :Virt-Manager 1.5.1
虚拟机配置 :ha1 CentOS 7.2 1511 (minimal) virbr0: 192.168.122.57
ha2 CentOS 7.2 1511 (minimal) virbr0: 192.168.122.58
ha3 CentOS 7.2 1511 (minimal) virbr0: 192.168.122.59
实验步骤:
安装系统并配置网络(所有虚拟机都需联网)
先操作第一台虚拟机(ha1)
编写主机名与IP的映射关系
[root@ha1 ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.57 ha1
192.168.122.58 ha2
192.168.122.59 ha3创建公有密钥
[root@ha1 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
40:c3:81:eb:60:49:2e:f7:fe:59:bb:ef:7d:ad:bb:06 root@ha2
The key's randomart image is:
+--[ RSA 2048]----+
| o+. |
| . .... |
| o . .. |
|. * . . |
| + + S |
| o E |
| . . . . |
| . o . . o .|
| .o o+o .o++ |
+-----------------+发送公有密钥至远程机器
[root@ha1 ~]# ssh-copy-id root@192.168.122.58
[root@ha1 ~]# ssh-copy-id root@192.168.122.59以上是单台虚拟机的逐条执行命令的方式,将以上操作写成脚本(脚本在本文末尾PS处)
下面操作其他虚拟机(ha2、ha3)
# 虚拟机ha2
[root@ha2 ~]# chmod 777 build-ssh-credit.sh
[root@ha2 ~]# ./build-ssh-credit.sh# 虚拟机ha3
[root@ha3 ~]# chmod 777 build-ssh-credit.sh
[root@ha3 ~]# ./build-ssh-credit.sh至此,三台虚拟机之间相互访问都无需输入密码,实现了SSH的免密登陆
Complete!!!
PS:公钥初始化和实现SSH免密登陆的脚本(build-ssh-credit.sh),直接拷贝就可使用。
#!/usr/bin/bash # 安装expect,minimal没有此rpm包,需联网或有本地yum源
yum install expect -y
expect << EOF
set timeout 10 # 创建公有密钥 spawn ssh-keygen -t rsa
expect {
"*to save the key" {send "\n";exp_continue}
"*(y/n)" {send "y\r";exp_continue}
"Enter passphrase" {send "\n";exp_continue}
"Enter same passphrase" {send "\n";exp_continue}
} EOF # 获取/etc/hosts文件中除localhost的映射关系
ip_list=`grep -v 'localhost' /etc/hosts | awk -F ' ' '{print $1,$2}'`
for ip in $ip_list
do
expect << EOF
set timeout 2 # 发送公有密钥
spawn ssh-copy-id root@$ip
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "000000\r";exp_continue}
} # 拷贝/etc/hosts文件到远程机器
spawn scp /etc/hosts $ip:/etc
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "root\r";exp_continue}
}
EOF
done
[提供可行性脚本] RHEL/CentOS 7 多节点SSH免密登陆的更多相关文章
- shell脚本配置ssh免密登陆
通过shell脚本配置免密登陆,分为两个脚本,一个是配置文件config.env,一个是正式脚本sshkey.sh. # config.envexport HOST_USER=(root) expor ...
- 多节点ssh免密匙登录
1,在所有节点上,使用yourname用户名执行: ssh-keygen -t dsa -P '' -f /home/yourname/.ssh/id_dsa 2,在node1的/home/yourn ...
- [提供可行性脚本] RHEL 7/CentOS 7/Fedora28 重命名网卡名称
实验说明: 在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(CentOS7/RHEL7),网卡命名采用“一致性网络设备接口”的命名方法,导致不同设备的不同网卡名称各 ...
- Centos下 自动化配置SSH免密码登陆
hosts文件,存储要部署的节点IP地址,其中以#开头表示注释掉 192.168.101.52 192.168.101.53 192.168.101.54 192.168.101.55 192.168 ...
- CentOS SSH免密登陆
#环境说明客户机:Mac OS X服务器:CentOS 6.5客户端:OpenSSH,OS X及大多数Linux都内置了OpenSSH.’ssh -v’命令可以查看版本. #大致流程1.在客户机创建一 ...
- centos或者ubuntu设置ssh免密码登陆
1. 输入 # ssh-keygen -t rsa -P "" 然后一路回车 2.输入 # cat ~/.ssh/id_rsa.pub >> ~/.ssh/aut ...
- Centos 集群配置SSH免登陆脚本
首先编写脚本生成集群服务器列表: hostsList.sh #!/bin/bash preIp="11.11.225." pwd="dyj2017" for i ...
- Shell脚本实现SSH免密登录及批量配置管理
本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...
- CentOS 6.5配置SSH免密码登录
centos 系统对权限的设置非常微妙,如果权限设置大了则ssh 拒绝,如果权限小了,则ssh 更是被拒绝(我曾经配置好久没有打通,就是因为权限过大的原因) 参考链接:http://www.linux ...
随机推荐
- 在github上下载子文件夹(svn命令)
Q: 平时都是用git clone这个命令从github中克隆出完整的文件,但有时我们仅需要其中某个文件夹时,该如何下载? A: 可以使用svn命令来完成. 具体用法:(以视觉slam14讲的gith ...
- Rebus消息总线
这里主要讲一下我基于Rebus写的一个ABP框架的模块 目录结构 对于Rebus网上的资料很少,其实我对于服务总线也不是很理解 ..个人理解的就是像ABP中的EventBus那样的,但是集成了一些 ...
- 巨杉db
巨杉数据库 and mongo db ,分布式数据库,
- Spring创建对象的几种方法
一.通过构造器 无参构造器 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- (转)nginx限制上传大小和超时时间设置说明/php限制上传大小
nginx限制上传大小和超时时间设置说明/php限制上传大小 原文:http://www.cnblogs.com/kevingrace/p/6093671.html 现象说明:在服务器上部署了一套后台 ...
- Strut2 Action的生命周期
一般而言,Action都是放在Spring容器中管理的,我会把属性设为prototype,这样,每一个请求,都会创建一个action对象. 今天碰到一个问题,当我用从一个jsp页面中输入一个属性,比如 ...
- 如何解决Eureka Server不踢出已关停的节点的问题?
如何解决Eureka Server不踢出已关停的节点的问题? eureka端: eureka.server.enable-self-preservation ...
- 关于ajax中return并不能作为方法的返回值
接下来关于ajax中的return值最后没有办法是方法的最终返回值问题 login(username,password) { console.log("进入方法"); $.ajax ...
- 1126 数字统计 2010年NOIP全国联赛普及组
1126 数字统计 2010年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 请统计某个 ...
- 如何设置FusionCharts图片导出格式
通过设置FusionCharts的<chart exportEnabled='1' ...>属性,就可以导出图表,图表的右键菜单将会显示所有可能导出的格式- JPEG, PNG and P ...