热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

实现三台虚拟机间的无密码SSH登录

本文介绍如何配置三台名为node01、node02和node03的虚拟机,以实现它们之间通过SSH进行无密码登录的过程。包括密钥对的生成、公钥的分发以及最终的登录测试。

为了实现三台虚拟机(命名为node01、node02和node03)之间的无密码SSH登录,首先需要在node01上生成SSH密钥对。这可以通过执行以下命令来完成:

[root@node01 ~]# ssh-keygen -t rsa -b 2048
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.

接下来,需要将生成的公钥复制到其他两台虚拟机(node02和node03)上。确保这些机器之间网络互通,能够互相ping通。使用scp命令完成公钥的复制:

[root@node01 ~]# scp /root/.ssh/id_rsa.pub root@192.168.22.130:/root/.ssh/authorized_keys
root@192.168.22.130's password:

[root@node01 ~]# scp /root/.ssh/id_rsa.pub root@192.168.22.100:/root/.ssh/authorized_keys
The authenticity of host '192.168.22.100 (192.168.22.100)' can't be established.
ECDSA key fingerprint is a2:25:2d:c3:ef:90:24:5b:ab:e3:13:b6:0f:ea:a0:de.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.22.100' (ECDSA) to the list of known hosts.
root@192.168.22.100's password:

确保node02和node03的/root/.ssh/目录下已存在authorized_keys文件:

[root@node02 ~]# ls /root/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts

[root@node03 ~]# ls /root/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts

最后,从node01尝试无密码登录到node02和node03,以验证配置是否成功:

[root@node01 ~]# ssh root@192.168.22.130
Last login: Thu Jan 7 13:44:40 2021 from 192.168.22.1

[root@node02 ~]# exit
登出
Connection to 192.168.22.130 closed.

[root@node01 ~]# ssh root@192.168.22.100
Last login: Thu Jan 7 00:35:18 2021 from 192.168.22.1

同样地,可以在node02和node03之间相互尝试无密码登录,以进一步验证整个配置过程的有效性。


推荐阅读
author-avatar
ZZ张朝_288
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有