ubuntu-22.04.5LTS网络自定义

解决/etc/netplan/50-cloud-init.yaml 配置重启后被重置,导致自定义网络信息不生效…

env

  • Ubuntu 22.04.5 LTS (GNU/Linux 5.15.0-130-generic x86_64)

解决

1.查看默认dhcp分配IP信息

1
2
3
4
5
6
7
8
9
10
gaga@gaga:~$ sudo cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
ens33:
dhcp4: true

2.开启自定义网络信息

默认提示很明确,要创建配置让自定义配置生效

 To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

创建99-disable-network-config.cfg

1
2
3
vi /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
# enable define network
network: {config: disabled}

设置网络信息50-cloud-init.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
gaga@gaga:~$ sudo cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
ens33:
dhcp4: false # 关闭dhcp
addresses: [192.168.3.140/24]
gateway4: 192.168.3.1
nameservers:
addresses: [8.8.8.8, 223.5.5.5]
version: 2

应用当前配置:
netplay apply

netplay命令

root@gaga:~# netplan --help
usage: /usr/sbin/netplan  [-h] [--debug]  ...

Network configuration in YAML

options:
  -h, --help  show this help message and exit
  --debug     Enable debug messages    //打印更详细日志

Available commands:
  
    help      Show this help message
    apply     Apply current netplan config to running system
    generate  Generate backend specific configuration files from /etc/netplan/*.yaml
    get       Get a setting by specifying a nested key like "ethernets.eth0.addresses", or "all"
    info      Show available features
    ip        Retrieve IP information from the system
    set       Add new setting by specifying a dotted key=value pair like ethernets.eth0.dhcp4=true
    rebind    Rebind SR-IOV virtual functions of given physical functions to their driver
    status    Query networking state of the running system
    try       Try to apply a new netplan config to running system, with automatic rollback

查看当前网络信息

root@gaga:~# netplan status 
     Online state: online
    DNS Addresses: 127.0.0.53 (stub)
       DNS Search: .

●  1: lo ethernet UNKNOWN/UP (unmanaged)
      MAC Address: 00:00:00:00:00:00
        Addresses: 127.0.0.1/8
                   ::1/128
           Routes: ::1 metric 256

●  2: ens33 ethernet UP (networkd: ens33)
      MAC Address: 00:0c:29:0d:df:56 (Intel Corporation)
        Addresses: 192.168.3.140/24
                   2408:8340:a22:c440:b0fe:e551:cb93:f/128
                   2408:8340:a22:c440:20c:29ff:fe0d:df56/64
                   fe80::20c:29ff:fe0d:df56/64 (link)
    DNS Addresses: 8.8.8.8
                   223.5.5.5
                   fe80::b2fe:e5ff:fe51:cb93
           Routes: default via 192.168.3.1 (static)
                   192.168.3.0/24 from 192.168.3.140 (link)
                   2408:8340:a22:c440::/64 metric 1024 (ra)
                   fe80::/64 metric 256
                   default via fe80::b2fe:e5ff:fe51:cb93 metric 1024 (ra)

Refer