背景: 有时候我们在两个主机之间复制文件的时候,提示输入密码,很不方便,那如何免密码复制呢?,就是使用通过linux公钥和秘钥,建立双机信任关系。

在整理之前,我先说下ssh免密码的要点 :

你想免密码登陆到哪个主机哪个用户, 就把你自己的公钥文件内容追加到远程主机对应用户下的authorized_keys文件中(对面可能没有这个文件,创建即可)。

1. 生成秘钥,并添加信任

我的环境中node1的ip是192.168.168.201,node2的ip是192.168.168.202.

[root@node1 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa         #生成rsa
[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.168.202 #复制201的公钥到202机器上,这样就可以使用在201机器上免密码登录202机器了。 [root@node2 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #生成rsa
[root@node2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.168.201 #复制202的公钥到201机器上,这样就可以使用在202机器上免密码登录201机器了。

注意:

  •   如果远程主机的端口非22端口,需要指定-p port选项。
  •   ssh-copy-id是由openssh-clients包提供,没有这个命令可以安装这个包。
  • centos6,7使用ssh-copy-id的时候可以不用指定-i选项,centos5必须指定的。

2.信任自己主机

[root@node1 ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.168.201
[root@node2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.168.202

3.测试

[root@node1 ~]# ssh 192.168.168.202 'ip addr show dev eth0 '
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :::3f:: brd ff:ff:ff:ff:ff:ff
inet 192.168.168.202/ brd 192.168.168.255 scope global eth0
inet6 fe80:::56ff:fe3f:/ scope link
valid_lft forever preferred_lft forever [root@node2 ~]# ssh 192.168.168.201 'ip addr show dev eth0'
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::c9:: brd ff:ff:ff:ff:ff:ff
inet 192.168.168.201/ brd 192.168.168.255 scope global eth0
inet 192.168.168.200/ brd 192.168.168.255 scope global secondary eth0
inet6 fe80::20c:29ff:fec9:/ scope link
valid_lft forever preferred_lft forever

4 自动配置免密码登陆

这里有个情况, 有一个跳板机,内部好多自己的服务器。远程只能通过跳板机远程内部服务器,现在需要在跳板机上免密码登陆内部的服务器。

4.1 各个机器上面需要先生成各自的公钥和密码

4.2 跳板机编写脚本和配置文件如下

#!/bin/bash
#================================================
#FileName :expect_ssh.sh
#Author :zhaojiedi
#Description:
#DateTime :-- ::
#Version :V1.
#Other :
#================================================
host_username_password_file=hosts.txt # install expect
rpm -q expect &>/dev/null || yum install -yq expect &>/dev/null # create id_rsa.pub file
pubkey=~/.ssh/id_rsa.pub
if [ ! -e "$pubkey" ] ; then
ssh-keygen -P "" -t rsa -f ~/.ssh/id_rsa
fi
while read host username password ; do
con=${username}"@"${host}
echo $password
expect <<EOF
set timeout
spawn ssh-copy-id $con
expect {
"yes/no" { send "yes\n" ; exp_continue }
"password:" { send "${password}\n"; exp_continue }
}
EOF
done < $host_username_password_file

样例的配置文件是这样的。

[root@centos74 bin]$ cat hosts.txt
172.18.46.6 root oracle

4.3 集群主机免密码配置

上面的跳板机配置免密码登陆都是单向的, 但是对于集群环境我们需要配置任意2个主机的主机信任的。

#!/bin/bash 

# 配置项
# 文件格式是 ip usename password port 中间使用空格分割
host_username_password_file=/root/hosts.txt # 配置是否copy文件到其他目标主机上去,只有第一个执行的主机需要copy,其他不需要的
need_copy_to_other=
src_host="current_host_ip"
#echo $#
if [ "$#" -eq ] ; then
need_copy_to_other=
else
src_host=$
fi # 安装 expect
rpm -q expect &> /dev/null || yum install -yq expect &>/dev/null # 创建 秘钥文件 pubkey=~/.ssh/id_rsa.pub
prikey=~/.ssh/id_rsa if [ ! -e "$pubkey" ] ; then
ssh-keygen -P "" -t rsa -f $prikey &>/dev/null
fi
while read host username password port ; do
if [ -z "$host" ] ; then
continue
fi
printf "%16s=======>%-16s\n" $src_host $host #echo "==============================$host $username $password $port ========================================= "
con=${username}"@"${host}
cmd_with_argv="ssh-copy-id -p $port $con "
#echo $password
expect <<-EOF
set timeout
log_user
spawn -noecho $cmd_with_argv
expect {
"yes/no" { send "yes\n"; exp_continue }
"password:" { send "${password}\n"; exp_continue }
}
EOF
&> /dev/null if [ "$need_copy_to_other" -eq ] ; then
ip a | grep $host &> /dev/null
if [ "$?" -ne ] ; then
#echo "==>复制必要的文件到远程主机($host)上去"
scp -q -p -P $port $host_username_password_file $host:$host_username_password_file
scp -q -p -P $port $ $host:$
#echo "==>在目标主机${host}上执行下"
ssh -f -p $port $con "$0 $host "
fi
fi done < $host_username_password_file
sleep
[root@localhost ~]# cat hosts.txt
192.168.46.151 root oracle
192.168.46.152 root oracle
192.168.46.153 root oracle
192.168.46.154 root oracle
192.168.46.157 root oracle

使用方式

[root@localhost ~]# /root/expect.sh
current_host_ip=======>192.168.46.151
current_host_ip=======>192.168.46.152
current_host_ip=======>192.168.46.153
192.168.46.152=======>192.168.46.151
192.168.46.152=======>192.168.46.152
192.168.46.152=======>192.168.46.153
current_host_ip=======>192.168.46.154
192.168.46.153=======>192.168.46.151
192.168.46.152=======>192.168.46.154
192.168.46.153=======>192.168.46.152
192.168.46.152=======>192.168.46.157
192.168.46.153=======>192.168.46.153
current_host_ip=======>192.168.46.157
192.168.46.154=======>192.168.46.151
192.168.46.153=======>192.168.46.154
192.168.46.154=======>192.168.46.152
192.168.46.153=======>192.168.46.157
192.168.46.154=======>192.168.46.153
192.168.46.154=======>192.168.46.154
192.168.46.154=======>192.168.46.157

Linux两台主机之间建立信任(ssh免密码)的更多相关文章

  1. 在两台服务器之间建立信任关系解决scp,ssh等不用输入密码等问题

    A服务器(client)向B服务(server)SCP,SSH. A服务器:ssh-keygen -t rsa -C "kangzj" 一直回车. cd .ssh vim id_r ...

  2. Linux:两台服务器之间添加信任关系,进行远程操作的时候不需要输入密码

    两台机器之间建立信任关系的步骤: 1. 在机器1上root用户执行ssh-keygen命令,生成建立安全信任关系的证书,直接Enter [root@CentOS64-x64 ~]# ssh-keyge ...

  3. 两台主机之间单向Ping不通的问题

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; color: #454545 } p.p2 ...

  4. Linux 两台服务器之间传输文件和文件夹

    今天处理一个项目要迁移的问题,突然发现这么多图片怎么移过去,可能第一时间想到的是先从这台服务器下载下来,然后再上传到另外一台服务器上面去,这个方法确实是可行,但是实在是太费时间了,今天我就教大家怎么快 ...

  5. 多台CentOS服务器下实现SSH免密码登录

    ROOT用户下实现SSH免密码登录 第一步:进入目录/root/.ssh $ cd  /root/.ssh/ 执行以下命令,会在当前目录下生成公钥(id_rsa.pub)/私钥(id_rsa)对 第二 ...

  6. 【原】linux两台服务器之间免密登录方法

    搭建集群机器192.168.0.100和192.168.0.200里,需要两台机器中间相互拷贝文件: 方式一:下载192.168.0.100机器文件到本地,再将本地文件拷贝到B机器 方式二:192.1 ...

  7. 两条命令在Linux主机之间建立信任关系

    ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa //生成当前用户密钥 ssh-copy-id -i /root/.ssh/id_rsa.pub r ...

  8. Linux 两台服务器之间传输文件

    一.scp命令的使用 1.传输文件(不包括目录) 命令格式:scp 源文件路径目录/需要传输的文件 目标主机的用户名@目标主机IP/主机别名:目标主机存储目录 举个例子:scp /root/ceshi ...

  9. 验证两台机器已经建立的ssh互信

    1.expect方法 #!/bin/bash checkTrust() { expect -c ' set timeout 2; spawn ssh $1 "expr 12345678 + ...

随机推荐

  1. mysql自定义时间段分组

    说明:一下是自定义7天为一个时间段的分组统计 select datediff(ms_time, '开始时间') div 7 as time , count(*) count,sum(money) mo ...

  2. Ubuntu18.04安装Guake下拉式终端

    Guake的优势是方便,可以很迅速的唤起与隐藏,已经成为我所需终端的主力. 安装方式:sudo apt-get install guake bug修复:使用时输入exit命令会导致终端卡死系统报错,同 ...

  3. JavaScript基础视频教程总结(101-110章)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. 交叉编译ffmpeg(hi3520d)

    ./configure \--prefix=/usr/local/ffmpeg-3520D \--cross-prefix=/opt/hisi-linux-nptl/arm-hisiv100-linu ...

  5. .NET CORE迁移踩坑

    https://www.cnblogs.com/leolaw/p/10740678.html

  6. redis在linux云服务器上完整的搭建步骤

    Redis的安装 搭建环境: 华为云linux服务器 Linux系统CneterOS-7.3 SSH客户端 Xshell6 安装c语言编译环境软件如下: 安装报错 然后找到了解决方法: 安装kerne ...

  7. Nginx 教程(2):性能

    tcp_nodelay, tcp_nopush 和 sendfile tcp_nodelay 在 TCP 发展早期,工程师需要面对流量冲突和堵塞的问题,其中涌现了大批的解决方案,其中之一是由 John ...

  8. python基础自学 第四天

    break和continue break:某一条件满足,退出循环,不在执行后续重复代码 continue:某一条件满足时,不执行后续重复的代码 注意:在循环中,如果使用continue这个关键字,使用 ...

  9. GodMode

    将“GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}”(不含引号)复制过去,保存即可.

  10. css伪元素 ::after ::before

    我遇到的问题: div盒子标签设置了伪元素 ::after  ::before  并给这俩content内容设置了空属性,添加了背景图,发现这两个伪元素没有宽度和高度. 解决方法 给设置伪元素的盒子的 ...