Creating a bridged network with netplan on Ubuntu 22.04

2023/10/04

Tags: linux bridged-network netplan ubuntu

本地LAN环境

  1. LAN网关 192.168.1.1
  2. 子网掩码 255.255.255.0
  3. DHCP范围 192.168.1.2-192.168.32

创建一个bridged network

创建一个网桥br0给虚机使用,使得虚机和其他设备都在一个LAN下

总配置(netplan get)如下:

network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp1s0:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      addresses:
      - "192.168.1.100/24"
      nameservers:
        addresses:
        - 192.168.1.1
      dhcp4: false
      dhcp6: false
      interfaces:
      - enp1s0
      parameters:
        stp: false
      routes:
      - to: "default"
        via: "192.168.1.1"

由三个文件组成:

  1. /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  1. /etc/netplan/10-ethernet-enp1s0.yaml
network:
  ethernets:
    enp1s0:
      dhcp4: false
      dhcp6: false
  1. /etc/netplan/99-bridged-network-br0.yaml
network:
  bridges:
    br0:
      dhcp4: false
      dhcp6: false
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: 
          - 192.168.1.1
          - 223.5.5.5
      interfaces:
        - enp1s0
      parameters:
        stp: false

应用网络配置

容易失联,如果是ssh远程操作请谨慎操作

netplan apply

补充

  1. 如何没有安装NetworkManager需要先安装(通过systemctl status NetworkManager查看是否安装)
apt install network-manager -y
  1. 生产环境可以systemd-networkdNetworkManager共存, 但是在我这里遇到了一些问题

nmstate 依赖NetworkManager服务, NM可以使用10-globally-managed-devices.conf配置不管理哪些接口

  1. 禁用 systemd-networkd
# 先关闭 systemd-networkd.socket, 否则每次关闭 systemd-networkd 都会被马上重新激活
systemctl stop systemd-networkd.socket
systemctl disable systemd-networkd.socket

systemctl stop systemd-networkd
systemctl disable systemd-networkd
  1. netplan apply之后会发现br0会出现好几个inet6

相关讨论 Why does my ubuntu-server have 4 ipv6 addresses?