【三】ubuntu24虚拟机集群配置免密登陆

文章目录

  • 环境背景
  • [1. 配置域名映射](#1. 配置域名映射)
  • [2. 配置免密登录](#2. 配置免密登录)
    • [2.1 在每台机器上生成SSH密钥对:](#2.1 在每台机器上生成SSH密钥对:)
    • [2.2 将公钥分发到其他机器:](#2.2 将公钥分发到其他机器:)
      • [2.2.1 报错问题](#2.2.1 报错问题)
      • [2.2.2 修复方法](#2.2.2 修复方法)
  • [3. 验证免密登录](#3. 验证免密登录)
        • [在 `ubuntu1` 上:](#在 ubuntu1 上:)
        • [在 `ubuntu2` 上:](#在 ubuntu2 上:)
        • [在 `ubuntu3` 上:](#在 ubuntu3 上:)
      • 测试连接

环境背景

当前三台ubuntu节点已经完成了虚拟机在vmware fusion中的部署,网络经过测试均已经互通,并且开机使用远程连接进行访问。


1. 配置域名映射

编辑每台机器的 /etc/hosts 文件,添加以下内容:

shell 复制代码
sudo vim /etc/hosts

添加以下行:

172.16.167.131 ubuntu1
172.16.167.132 ubuntu2
172.16.167.133 ubuntu3

保存并退出文件。

同时,可以给mac物理机主机映射也配置上 ,打开mac终端,先进入root,再编辑hosts文件:

su root
vim /etc/hosts

这样,每台机器都能通过域名访问其他机器。

2. 配置免密登录

在每台机器上生成SSH密钥对,并将公钥分发到其他机器。

2.1 在每台机器上生成SSH密钥对:

shell 复制代码
ssh-keygen -t rsa -b 2048

在提示时按Enter键,使用默认文件路径,不设置密码短语。

2.2 将公钥分发到其他机器:

假设现在在 ubuntu1 上,运行以下命令将公钥分发到 ubuntu2ubuntu3

shell 复制代码
ssh-copy-id ubuntu2
ssh-copy-id ubuntu3

成功的回显如下:

shell 复制代码
root@ubuntu1:~# ssh-copy-id ubuntu3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'ubuntu3 (172.16.167.133)' can't be established.
ED25519 key fingerprint is SHA256:kT9JZ/VheFqnJ6JjAA7RTTRzpdFCJ0DE+SbAAYgeCLc.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: [hashed name]
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@ubuntu3's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'ubuntu3'"
and check to make sure that only the key(s) you wanted were added.

root@ubuntu1:~# 

然后在 ubuntu2 上,将公钥分发到 ubuntu1ubuntu3

shell 复制代码
ssh-copy-id ubuntu1
ssh-copy-id ubuntu3

最后在 ubuntu3 上,将公钥分发到 ubuntu1ubuntu2

shell 复制代码
ssh-copy-id ubuntu1
ssh-copy-id ubuntu2

2.2.1 报错问题

root@ubuntu1:~# ssh-copy-id ubuntu2
/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: ERROR: /etc/ssh/ssh_config: line 54: Bad configuration option: permitrootlogin
ERROR: /etc/ssh/ssh_config: terminating, 1 bad configuration options

出现这个错误是因为 /etc/ssh/ssh_config 文件中包含无效的配置选项 PermitRootLogin。实际上,这个选项应该出现在 sshd_config 文件中,而不是 ssh_config 文件中。ssh_config 是客户端配置文件,而 sshd_config 是服务器配置文件。

2.2.2 修复方法

  1. 编辑 /etc/ssh/ssh_config 文件 :打开并编辑 /etc/ssh/ssh_config 文件,找到包含 PermitRootLogin 的行并删除它:
shell 复制代码
sudo vim /etc/ssh/ssh_config

找到并删除或注释掉这行:

PermitRootLogin yes

保存并退出文件。

  1. 编辑 /etc/ssh/sshd_config 文件 :确保 PermitRootLogin 设置在服务器端配置文件 /etc/ssh/sshd_config 中正确配置:
shell 复制代码
sudo vim /etc/ssh/sshd_config

确保有如下配置:

PermitRootLogin yes

保存并退出文件,然后重启SSH服务,要在三台节点上都重新配置一下ssh:

shell 复制代码
sudo systemctl restart ssh
  1. **再次尝试 **ssh-copy-id:现在,你可以再次尝试使用 ssh-copy-id 命令将公钥复制到 ubuntu2
shell 复制代码
ssh-copy-id ubuntu2

3. 验证免密登录

在每台机器上测试是否可以免密登录到其他机器:

ubuntu1 上:
shell 复制代码
ssh ubuntu2
ssh ubuntu3
ubuntu2 上:
shell 复制代码
ssh ubuntu1
ssh ubuntu3
ubuntu3 上:
shell 复制代码
ssh ubuntu1
ssh ubuntu2

测试连接

可以在每台机器上使用以下命令测试连接:

shell 复制代码
ssh ubuntu1
ssh ubuntu2
ssh ubuntu3

通过上述步骤,成功为三台Ubuntu虚拟机配置域名映射和免密登录。这样,可以通过域名而不是IP地址访问每台机器,并且在每台机器之间进行免密SSH登录。

相关推荐
若云止水40 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
吃汤圆的抹香鲸1 小时前
GoLand 安装包 绿色版 Win,Mac,Linux 包含IntelliJ全家桶 专为Go语言设计的集成开发环境(IDE)
linux·windows·macos·go·intellij-idea·go1.19
吃汤圆的抹香鲸2 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
Mr_wilson_liu10 小时前
ssh被暴力访问了,怎么拦截ip
网络·tcp/ip·ssh
放情11 小时前
Ubutu部署WordPress
ubuntu·云原生
Xiezequan12 小时前
Ubuntu指令(一)
linux·运维·ubuntu
ForTodayzzZ12 小时前
centos和ubuntu安装mysql教程
linux·mysql·ubuntu·centos
甜于酸13 小时前
MuMu模拟器Pro for Mac 安卓手机平板模拟器
android·macos·智能手机
尘佑不尘13 小时前
【无标题】oscp备考,oscp系列——Tr0ll1靶场,两种提权方式,ftp匿名登录,ssh爆破,ubuntu内核提权,计划任务
web安全·ubuntu·渗透测试·ssh·vulnhub
渔夫阿布13 小时前
在MAC上面通过HomeBrew安装node和npm@指定版本
前端·macos·npm