Liunx两台服务器实现相互SSH免密登录

一、首先准备两台Linux虚拟机当作此次实验的两台服务器

服务器1:server IPV4:192.168.110.136

服务器2:client IPV4: 192.168.110.134

二、准备阶段

root@server \~\]# systemctl disable firewalld #关闭防火墙 \[root@server \~\]# setenforce 0 #关闭seLiunx \[root@server \~\]# getenforce Permissive \[root@client \~\]# systemctl disable firewalld #关闭防火墙 \[root@client \~\]# setenforce 0 #关闭seLiunx \[root@client \~\]# getenforce Permissive

三、配置

server免密访问client

root@server \~\]# ssh-keygen -t rsa #此处为rsa密钥对 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. 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: SHA256:EtRb4lrkGMtSaa+JoG3bL2AfzUlJPbx114ZZyOmOf04 root@server The key's randomart image is: +---\[RSA 3072\]----+ \| .= . B. \| \| .\* B o . B o \| \| =.X B . o . \| \| . . \*.B . \| \| o . \*.\*S o \| \|. = o B. . . \| \| o = . . E \| \| . + ... \| \| o. o. \| +----\[SHA256\]-----+ \[root@server \~\]# ll /root/.ssh/ #查看密钥匙目录 总用量 8 -rw-------. 1 root root 2590 10月 26 22:40 id_rsa #私钥文件 -rw-r--r--. 1 root root 565 10月 26 22:40 id_rsa.pub #公钥文件 ![](https://file.jishuzhan.net/article/1718851948553179137/89d20ba858a5dbb51fb1562da77013ba.webp) \[root@server \~\]# ssh-copy-id root@192.168.110.134 #复制该公钥文件到服务端的该目录下 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.110.134 (192.168.110.134)' can't be established. ED25519 key fingerprint is SHA256:rFpTMCYeFtLw16vHvNCtD7f+AutcLz68mnrlopUfino. This key is not known by any other names Are you sure you want to continue connecting (yes/no/\[fingerprint\])? yes /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.110.134's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.110.134'" and check to make sure that only the key(s) you wanted were added. 在client端查看文件 \[root@client \~\]# ll /root/.ssh/ 总用量 4 -rw-------. 1 root root 565 10月 26 22:46 authorized_keys ![](https://file.jishuzhan.net/article/1718851948553179137/b0ac8bcf6e8a1b8e55cd42d4a2fedc6c.webp) \[root@server \~\]# ssh 192.168.110.134 #使用SSH远程登录client Register this system with Red Hat Insights: insights-client --register Create an account or view all your systems at https://red.ht/insights-dashboard Last login: Thu Oct 26 22:33:54 2023 from 192.168.110.1 \[root@client \~\]# ![](https://file.jishuzhan.net/article/1718851948553179137/491c94e32b9f8e277f613668f66bf4f3.webp) 免密登陆成功!!!

client免密访问server

root@client \~\]# ssh-keygen -t rsa #此处为rsa密钥对 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): 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: SHA256:mmA+3Ih66Ue7yv1TBdvjbcsmmTKeK7U9rBEuW1NWOiU root@client The key's randomart image is: +---\[RSA 3072\]----+ \| \| \| . \| \| +E o \| \| . += \| \| o So=o \| \| =.+ +o+..o \| \| .o=.=o=+ = . \| \| oo.o.o+++B + \| \|.o+oooo+== + \| +----\[SHA256\]-----+ \[root@client \~\]# ll /root/.ssh/ #查看密钥匙目录 总用量 20 -rw-------. 1 root root 565 10月 26 22:46 authorized_keys #server免密访问client生成的密钥 -rw-------. 1 root root 2590 10月 26 22:57 id_rsa #私钥文件 -rw-r--r--. 1 root root 565 10月 26 22:57 id_rsa.pub #公钥文件 -rw-------. 1 root root 843 10月 26 22:57 known_hosts #记录服务端发来的公钥信息 -rw-r--r--. 1 root root 97 10月 26 22:57 known_hosts.old #储之前连接过的 SSH 服务器的公钥 ![](https://file.jishuzhan.net/article/1718851948553179137/619904c9ed328bbdc91c7416f45994f5.webp) \[root@client \~\]# ssh-copy-id root@192.168.110.136 #复制该公钥文件到服务端的该目录下 /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.110.136's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.110.136'" and check to make sure that only the key(s) you wanted were added. 在server端查看文件 \[root@server \~\]# ll /root/.ssh/ 总用量 20 -rw-------. 1 root root 565 10月 26 23:01 authorized_keys -rw-------. 1 root root 2590 10月 26 22:40 id_rsa -rw-r--r--. 1 root root 565 10月 26 22:40 id_rsa.pub -rw-------. 1 root root 843 10月 26 22:46 known_hosts -rw-r--r--. 1 root root 97 10月 26 22:46 known_hosts.old ![](https://file.jishuzhan.net/article/1718851948553179137/d247d5a2a56b9d81fc61e211b519627c.webp) \[root@client \~\]# ssh 192.168.110.136 #使用SSH远程登录server Activate the web console with: systemctl enable --now cockpit.socket Register this system with Red Hat Insights: insights-client --register Create an account or view all your systems at https://red.ht/insights-dashboard Last login: Thu Oct 26 22:57:34 2023 from 192.168.110.134 \[root@server \~\]# ![](https://file.jishuzhan.net/article/1718851948553179137/7d07d5fe6518b41c7d1c7361847a5313.webp) 登陆成功!!!

实验结束实现服务器1server和服务器2client相互免密登录!

相关推荐
甲鱼92913 小时前
MySQL 实战手记:日志管理与主从复制搭建全指南
运维
Johny_Zhao1 天前
OpenClaw安装部署教程
linux·人工智能·ai·云计算·系统运维·openclaw
YuMiao2 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
chlk1233 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑3 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件3 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒3 天前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号3 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash3 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI4 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github