ssh 信任关系无密码登陆,清除公钥,批量脚本
实验机器:
主机a:192.168.2.128
主机b:192.168.2.130
实验目标: 手动建立a到b的信任关系,实现在主机a通过 ssh 192.168.2.130不用输入密码远程登陆b主机
1、a主机生成公钥
ssh-keygen -t rsa 三次回车
[root@localhost ~]# 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)?
[root@localhost ~]# cd .ssh
[root@localhost ~]# ls –a
2、将公钥复制到b主机
[root@localhost .ssh]# scp id_rsa.pub root@192.168.255.130:/root
在b主机上将刚才传输过来的公钥文件 追加到root/.ssh/authorized_keys文件里
[root@localhost ~]# cat ~/id_ras.pub >>/root/.ssh/authorized_keys
#没有这个文件的话会自动创建
3、在b主机中设置权限:
[root@localhost ~]# chmod 700 .ssh
[root@localhost ~]# chmod 600 .ssh/authorized_keys
#注意权限必须为700和600,否则不能成功
在a主机验证:
[root@localhost .ssh]# ssh 192.168.255.130
Last login: Fri Mar 17 17:28:34 2017 from 192.168.255.128
退出:
[root@localhost ~]# exit
logout
Connection to 192.168.255.130 closed
第2步和第3步可以用下面的命令代替
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.255.130
清除公钥信息:
ssh-keygen -R 192.168.255.130
如果主机过多,一个个敲命令肯定不现实,脚本如下,此脚本需要安装expect命令
[root@localhost ~]# rpm -qa |grep expect
[root@localhost ~]# yum install expect
[root@localhost ~]# vim install_ssh.sh
#!/bin/bash
#批量ssh认证建立
for p in $(cat /root/ip.txt) #注意ip.txt文件的绝对路径
do
ip=$(echo "$p"|cut -f1 -d":") #取ip.txt文件中的ip地址
password=$(echo "$p"|cut -f2 -d":") #取ip.txt文件中的密码
#expect自动交互开始
expect -c "
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$ip
expect {
\"*yes/no*\" {send \"yes\r\"; exp_continue}
\"*password*\" {send \"$password\r\"; exp_continue}
\"*Password*\" {send \"$password\r\";}
}
"
done
将要建立关系的服务器ip和密码写在ip.txt里,格式如下
ip:密码
[root@localhost ~]# cat ip.txt
172.16.0.113:123456
172.16.0.114:123456
执行过程:
[root@localhost ~]# ./install_ssh.sh
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.0.113
The authenticity of host '172.16.0.113 (172.16.0.113)' can't be established.
RSA key fingerprint is 4d:24:d4:2e:85:c2:6f:73:01:d5:23:b8:50:97:f8:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.0.113' (RSA) to the list of known hosts.
Now try logging into the machine, with "ssh 'root@172.16.0.113'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.0.114
The authenticity of host '172.16.0.114 (172.16.0.114)' can't be established.
RSA key fingerprint is 4d:24:d4:2e:85:c2:6f:73:01:d5:23:b8:50:97:f8:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.0.114' (RSA) to the list of known hosts.
Now try logging into the machine, with "ssh 'root@172.16.0.114'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
验证一下:
[root@localhost ~]# ssh 172.16.0.113
Last login: Fri May 19 19:28:38 2017 from 172.16.0.111
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:3b:4b:c5 brd ff:ff:ff:ff:ff:ff
inet 172.16.0.113/23 brd 172.16.1.255 scope global eth0
inet6 fe80::20c:29ff:fe3b:4bc5/64 scope link
valid_lft forever preferred_lft forever
好了
ssh 信任关系无密码登陆,清除公钥,批量脚本的更多相关文章
- ssh两台机器建立信任关系无密码登陆
在建立信任关系之前先看看基于公钥.私钥的加密和认证. 私钥签名过程 消息-->[私钥]-->签名-->[公钥]-->认证 私钥数字签名,公钥验证 Alice生成公钥和私钥,并将 ...
- 批量部署ssh信任关系
要求1:大批量部署SSH信任关系,在A文件分发服务器上大批量部署WEB层面信任关系文件分发服务器为:10.0.3.9 登录用户为:zhangsan WEB层IP段:10.0.3.10~10.0.3.6 ...
- 18.ssh远程双向无密码登陆
#ssh远程双向无密码登陆 需求:在192.168.10.100执行ssh 192.168.20.205不需要输入密码直接跳转到205机器 #在192.168.10.100执行命令,我这里使用root ...
- Linux创建SSH信任关系
Linux服务器创建信任关系可以解决远程执行命令.远程传输文件多次手工输入的麻烦.可以实现环境一键打包备份. 测试环境 SuSE 手工创建 假设服务器A与B间要建立信任关系.用户想从服务器A免密码登录 ...
- ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs
ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs 第一部分:使用ssh key 实现服务器间的免密码交互登陆 步骤1: 安装openssh-clients [root@001 ...
- Linux远程无密码登陆并远程执行脚本
假设 A (192.168.20.59)为客户机器,B(192.168.20.60)为目标机: 要达到的目的: A机器ssh登录B机器无需输入密码: 加密方式选 rsa|dsa均可以,默认dsa ss ...
- linux建立ssh信任关系
一.建立SSH信任将A主机做为客户端(发起SSH请求 ip:192.168.200.170)将B主机作为服务器端(接收ssh请求 ip:192.168.200.149)以上以主动发起SSH登录请求 ...
- SSH 信任关系建立
需求hostA通过ssh登陆到hostB,实现免密登陆,以及SCP的免密传送文件 由于hostA要登陆到hostB 首先需要在hostA上生成密钥,使用以下命令 ssh-keygen -t rsa 按 ...
- 【ssh信任关系】解决信任关系不生效问题
配置的时候遇见点问题,发现即便将id_rsa.pub拷贝到了另一台机器上,信任也没有建立起来. 原因是另外一台机器上目录权限不对,可以通过su root后观察/var/log/message里的日志信 ...
随机推荐
- c++设计模式概述之访问者
代码写的不够规范,目的是为了缩短篇幅,实际中请注意. 参看: http://c.biancheng.net/view/1397.html 1.概述 类比生活中的场景,购物商场中的商品.顾客.收营员.商 ...
- CMake判断操作系统和编译器
判断操作系统 IF (CMAKE_SYSTEM_NAME MATCHES "Linux") ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Wind ...
- 【LeetCode】504. Base 7 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 内建库 BigInteger类 逐位计算 倍数相加 ...
- 【LeetCode】506. Relative Ranks 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 argsort 堆 日期 题目地址:https ...
- 1119 机器人走方格 V2
1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:131072 KB M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很大,只需要输出Mo ...
- 1036 - A Refining Company
1036 - A Refining Company PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 ...
- Discrete Logging(poj2417)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5120 Accepted: 2319 ...
- Pikachu漏洞练习-SQL-inject(三)
- Codeforces 777B:Game of Credit Cards(贪心)
After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle betwe ...
- toString()、String.valueOf、(String)强转
1.基本类型 (1)基本类型没有toString()方法 (2)推荐使用String.valueOf(); (3)无法强转 =========补========= (String)是标准的类型转换,将 ...