ubuntu虛擬機器克隆後需要進行的修改

二乘八是十六發表於2024-03-08

在虛擬化中,如果採用機器克隆的情況,使用同一個主機名和系統id會造成機器衝突,需要進行主機名修改和機器id重置

1. 修改系統IP地址資訊

  • 新版本使用netplan配置主機IP地址
ubuntu@ubuntu:~$ cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens160:
      addresses:
      - 192.168.63.31/24 		#配置主機IP地址
      gateway4: 192.168.63.1	#配置主機閘道器
      nameservers:
        addresses:
        - 192.168.64.3 			#配置主機主DNS
        - 223.5.5.5				#配置主機備DNS
        search: []
  version: 2

  • 配置完成後應用配置
root@ubuntu:~# netplan apply
  • 配置完成後的地址檢視
root@ubuntu:~# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:50:56:9b:9c:48 brd ff:ff:ff:ff:ff:ff
    inet 192.168.63.31/24 brd 192.168.63.255 scope global ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::250:56ff:fe9b:9c48/64 scope link
       valid_lft forever preferred_lft forever
root@ubuntu:~#

2. 修改主機名

  • 克隆後的虛擬機器,名字與模板一致,容易造成衝突,需要進行修改
root@ubuntu:~# hostnamectl
   Static hostname: ubuntu
Transient hostname: clt
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 25648f8254ff40be8832c34fd4744d35
           Boot ID: 191c8d415c854021aea170d7a7a9b863
    Virtualization: vmware
  Operating System: Ubuntu 20.04.6 LTS
            Kernel: Linux 5.4.0-146-generic
      Architecture: x86-64
root@ubuntu:~#
  • 修改主機名
root@ubuntu:~# hostnamectl set-hostname docker.sec.local
root@ubuntu:~# hostnamectl
   Static hostname: docker.sec.local
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 25648f8254ff40be8832c34fd4744d35
           Boot ID: 191c8d415c854021aea170d7a7a9b863
    Virtualization: vmware
  Operating System: Ubuntu 20.04.6 LTS
            Kernel: Linux 5.4.0-146-generic
      Architecture: x86-64
root@ubuntu:~#

3. 修改machine-id

  • ubuntu預設會帶有machine-id,所有克隆出來的主機都和模板上的機器一致
root@ubuntu:~# hostnamectl
   Static hostname: docker.sec.local
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 25648f8254ff40be8832c34fd4744d35 	#預設生成的Machine ID
           Boot ID: 191c8d415c854021aea170d7a7a9b863
    Virtualization: vmware
  Operating System: Ubuntu 20.04.6 LTS
            Kernel: Linux 5.4.0-146-generic
      Architecture: x86-64
root@ubuntu:~#

  • 修改或者刪除原有模板內的Machine ID
root@ubuntu:~# rm -f /etc/machine-id
root@ubuntu:~# systemd-machine-id-setup
Initializing machine ID from random generator.
root@ubuntu:~#

root@ubuntu:~# hostnamectl
   Static hostname: docker.sec.local
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 17958315705a4d1d94899f826d844a23	#生成新的ID值
           Boot ID: 191c8d415c854021aea170d7a7a9b863
    Virtualization: vmware
  Operating System: Ubuntu 20.04.6 LTS
            Kernel: Linux 5.4.0-146-generic
      Architecture: x86-64
root@ubuntu:~#

相關文章