虚拟机软件:VirtualBox
Linux 发行版本:Ubuntu 20.04.4
模板机准备 初始化 root 密码为什么使用 VirtualBox ?
- 免费
- Windows 和 macOS 都有
$ sudo passwd连不上 VirtualBox 虚拟机
网络模式设置两个网卡,分别为 NAT 和 Host-Only
NAT 用于连外网,Host-Only 用于连内网
静态 IP查看网卡信息
$ ip addr
enp0s3 可见是 dhcp 动态分配的,也就是上面的网卡 1
enp0s8 也就是网卡 2,没有分配 ip ,我们需要给网卡 2 设置静态 ip,通过他来内网通信
配置网卡
$ vim /etc/netplan/00-installer-config.yaml
network:
ethernets:
enp0s3:
dhcp4: yes
enp0s8:
dhcp4: no
addresses: [192.168.128.101/24]
nameservers:
addresses: [8.8.8.8]
version: 2
应用网卡配置
$ netplan apply配置主机名
$ vim /etc/hosts 192.168.128.101 node01 192.168.128.102 node02 192.168.128.103 node03 $ vim /etc/hostname node01配置 ssh
更改 ssh 配置文件
$ vim /etc/ssh/sshd_config
# 允许登录 root 用户 PermitRootLogin yes # 允许密码登录 PasswordAuthentication yes
重启 ssh 服务
$ service ssh restart更换阿里源
$ vim /etc/apt/sources.list # 复制以下内容 deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse $ apt update多机
准备
上述模板机准备完毕后,关机。复制出多台机子,本文起三台。
复制虚拟机注意:
- 生成新的 mac 地址
- 完全复制
详细配置参考上文
| HostName | IP |
|---|---|
| node01 | 192.168.128.101 |
| node02 | 192.168.128.102 |
| node03 | 192.168.128.103 |
生成公私钥
# 默认位置在 ~/.ssh $ ssh-keygen
id_rsa 是私钥,id_rsa.pub 是公钥
把生成的公钥复制给要登录的机子上 ~/ssh/authorized_keys
配置多个只要另起一行就行
分发脚本$ vim /bin/xsync
#!/bin/bash
#1 获取输入参数个数,如果没有参数,直接退出
pcount=$#
if((pcount==0)); then
echo no args;
exit;
fi
#2 获取文件名称
p1=$1
fname=`basename $p1`
echo fname=$fname
#3 获取上级目录到绝对路径
pdir=`cd -P $(dirname $p1); pwd`
echo pdir=$pdir
#4 获取当前用户名称
user=`whoami`
#5 循环,分发到 node01 ~ node03
for((i=1; i<=3; i++)); do
echo ------------------- node0$i --------------
rsync -rvl $pdir/$fname $user@node0$i:$pdir
done
$ chmod +x /bin/xsync
所有机子间都必须配置免密登录
将分发脚本分发下去
$ xsync /bin/xsync



