linux主机互信操作
一.主机互信原理
两个主机之间ssh登录需要提示输入对方的密码,当频繁需要登录操作时,可以通过linux公钥和秘钥,建立双机信任关系。
把你源主机的公钥文件内容追加到目的主机对应用户下的authorized_keys文件中(如果没有这个文件,则创建一个)
二.操作流程
两台主机分别是node1的ip是1192.168.37.8,node2的ip是192.168.37.9
1. 生成秘钥(两台主机分别操作)
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #生成rsa
1) node1节点操作 - 生成密钥
[root@node1 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #node1节点生成密钥
Generating public/private rsa key pair.
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:
SHA256:1RzOG0HzkXAAZz6YdYMp6vgGev0zzv3zzM3VF5cKb30 root@node1
The key's randomart image is:
+---[RSA 2048]----+
| .o@==. |
| .%oB.o |
| .+.O . |
| .. + .|
| oS .. o.|
| o . o o +|
| . + + .E|
| . . +.o.. .+=|
| . . o+o...o*|
+----[SHA256]-----+
[root@node1 ~]#
2) node2节点操作 -生成密钥
[root@node2 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #node2节点生成密钥
Generating public/private rsa key pair.
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:
SHA256:9mRYbTPIQ3pSYwdMO5/byswezu7uicSAF9AJAxPisqU root@node2
The key's randomart image is:
+---[RSA 2048]----+
| . +o+o +B.. |
| . . . oo*.* |
|. o + O = |
| = . * = + |
|E . S o o |
| o * o |
| + o . |
| . B + |
| .*# |
+----[SHA256]-----+
[root@node2 ~]#
2.信任主机操作(两台主机分别操作)
ssh-copy-id -i ~/.ssh/id_rsa.pub root@对方ip
1) node1节点设置互信
[root@node1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.37.9 #复制37的公钥到38机器上,这样就可以使用在37机器上免密码登录38机器了。
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.37.9's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.37.9'"
and check to make sure that only the key(s) you wanted were added.
[root@node1 ~]#
2) node2节点设置互信
[root@node2 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.37.8 #复制node2的公钥到node1机器上,这样就可以使用在node2机器上免密码登录node1机器了
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.37.8's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.37.8'"
and check to make sure that only the key(s) you wanted were added.
[root@node2 ~]#
3.测试ssh机器免密
1) node1节点访问node2
[root@node1 ~]#
[root@node1 ~]# ssh 192.168.37.9
Last login: Wed Oct 13 22:13:07 2021 from 192.168.37.8
[root@node2 ~]#
node2节点访问node1
[root@node2 ~]# ssh 192.168.37.8
Last login: Mon Sep 20 18:30:35 2021 from 192.168.37.9
[root@node1 ~]#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了