【三】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登录。

相关推荐
pingglala13 小时前
ubuntu-nat模式不能上网解决方法
linux·运维·ubuntu
微软技术分享14 小时前
Ubuntu 本地部署Ollama+OpenWebUI教程
数据库·ubuntu·postgresql
一叶龙洲20 小时前
Ubuntu从图标到卸载应用
linux·windows·ubuntu
头发够用的程序员20 小时前
别被 TOPS 参数迷惑:手把手推导边缘 GPU 整型算力
人工智能·python·ubuntu·计算机视觉·硬件架构·边缘计算
深念Y21 小时前
PVE 安装黑苹果并直通 Quadro P400
macos·ios·黑苹果·驱动·苹果·英伟达·p400
ACP广源盛139246256731 天前
M6/M5 Pro Mac mini 端侧 AI 落地@ACP#IX6024 PCIe2.0 交换芯片在轻量化 AI 服务中的机会与应用场景
大数据·数据库·人工智能·嵌入式硬件·macos·开源
不剪发的Tony老师1 天前
Navop:一款工具搞定数据库、SSH、SFTP、远程桌面、AI Agent
运维·数据库·ssh
sysinside1 天前
VMware Fusion 26H1u1 for Mac - 领先的免费桌面虚拟化软件
macos·fusion
捷瑞电子工坊1 天前
VSCode的配置和一些报错问题
vscode·ubuntu·c/c++·编译报错·开发环境配置·初学者教程·json配置
夕小瑶1 天前
豆包工作」升级实测:多Agents并行,操作Mac电脑
macos