Ubuntu下设置网卡地址

网络管理文件

vim /etc/netplay/xxx.yaml

Ubuntu18.04及20.04

单网卡配置

# This is the network config written by 'subiquity'
network:
  version: 2
  ethernets:
    ens18:
      addresses:
      - 192.168.3.20/24
      gateway4: 192.168.3.1
      nameservers:
        addresses: [192.168.3.1]
        search: []
      optional: true

带网桥管理(常用于构建kvm)

# This is the network config written by 'subiquity'
network:
  ethernets:
    enp4s0:
      dhcp4: true
      optional: true
    enp5s0:
      dhcp4: no
      optional: true
    ens4:
      dhcp4: true
      optional: true
    ens4d1:
      dhcp4: true
      optional: true
  bridges:
      br0:
        interfaces: [enp5s0]
        addresses:
        - 192.168.3.11/24
        gateway4: 192.168.3.1
        nameservers:
          addresses: [192.168.3.1]
          search: []
        optional: true
  version: 2

Ubuntu22.04

单网卡配置

network:
  version: 2
  ethernets:
    ens18:
      addresses:
      - 192.168.50.29/24
      routes:
        - to: default
          via: 192.168.50.1
      nameservers:
        addresses: [114.114.114.114,8.8.8.8]
        search: []
      optional: true

带网桥管理(常用于构建kvm)

network:
  ethernets:
    enp4s0:
      dhcp4: no
      optional: true
    enp5s0:
      dhcp4: no
      optional: true
    ens4:
      dhcp4: no
      optional: true
    ens4d1:
      dhcp4: no
      optional: true
  bridges:
      br0:
        interfaces: [enp5s0]
        addresses:
        - 192.168.3.11/24
        routes:
          - to: default
            via: 192.168.3.1
        nameservers:
          addresses: [192.168.3.1]
          search: []
        optional: true
  version: 2

注意:在ubuntu22以后,设置网关的方式有所更改

gateway4: 192.168.3.1
更改为
routes:
  - to: default
    via: 192.168.3.1

关于optional

在每一个网络接口中都添加optional=true可以防止Ubuntu启动中卡在网络接口的进程几分钟,下面是ubuntu启动中报告的网络配置等待的报错信息

A start job is running for wait for network to be configured ......

执行生效

netplan apply