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里的日志信 ...
随机推荐
- VS2010 sp1离线下载地址和在线下载地址
地址是:http://www.msdn1.cn/8/42/ 下载: edk2 + 迅雷, 稳的1P
- 第一篇CSDN博客,大家好!
大家好,我是负雪明烛! 我这昵称的来源是喜欢一句很有意蕴的古诗--苍山负雪,明烛天南. 我喜欢这句诗,很多的账号都用了这个"负雪明烛"的昵称,如果大家在其他地方看到叫这个名字的人, ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 【LeetCode】789. Escape The Ghosts 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 小白自制Linux开发板(第二季 V3s篇) 一. 换个核心再来一次
1.前言 大家心心念念(个人认为)的小白自制开发板全新系列正式来了,之前我们使用全志的F1C200s芯片制作了一个小电脑,众所周知,调试很艰难,坑也很多,以至于墨云到现在还是没找到对应的补救方案,为了 ...
- 「算法笔记」FHQ-Treap
右转→https://www.cnblogs.com/mytqwqq/p/15057231.html 下面放个板子 (禁止莱莱白嫖板子) P3369 [模板]普通平衡树 #include<bit ...
- [c++]关于指针的一些问题记录
const char* 和char* 之间的转换 const char*是指向常量的指针,而不是指针本身为常量,可以不被初始化.该指针可以指向常量也可以指向变量,只是从该指针的角度而言,它所指向的是常 ...
- RSA非对称加密算法实现:Golang
RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...
- Linux 安装并启用 PHP-FPM
首先,在编译时带上 --enable-fpm 参数: [root@localhost local]# yum -y install libxml2 libxml2-devel gd gd-devel ...
- Microsoft HoloLens 开发(2): 运行Hello World
1.下载 MixedRealityToolkit-Unity (混合现实工具包) 什么是 MixedRealityToolkit-Unity ? 一个脚本和组件的集合,加速针对微软全息和Windows ...