Netplan on Ubuntu for network management

Time:2022-1-17

On the Ubuntu 18.04 platform, it uses netplan to manage the system network. Netplan can generate the configuration file required by} NetworkManager or SYSTEMd network through the configuration file in yaml format (located in / etc / netplan) to configure the network. Since 17.10, Ubuntu has used netplan to replace the default network management tool of ifupdown Linux system. For the previous network configuration, often edit the / etc / network / interfaces file and restart the network service; The introduction of netplan makes the network configuration more convenient.

I mentioned the use of netplan in my blog on configuring static networks in Ubuntu:Linux static address configuration options; Ubuntu 18.04 static network address configuration; Netplan command; Today, I have a systematic understanding of netplan and made a brief record on this blog for my personal memory.

Netplan website:https://netplan.io/

Configuration file corresponding to release version:

INSTALL TYPE RENDERER FILE
Server ISO systemd-networkd /etc/netplan/01-netcfg.yaml
Cloud Image systemd-networkd /etc/netplan/50-cloud-init.yaml
Desktop ISO NetworkManager /etc/netplan/01-network-manager-all.yaml

Sample configuration: after the configuration is completed, use netplan apply to take effect:

Configure the corresponding network card and the desired state:

  • enp3s0 setup with IPv4 DHCP
  • enp4s0 setup with IPv4 static with custom MTU
  • IPv6 static tied to a specific MAC address
  • IPv4 and IPv6 DHCP with jumbo frames tied to a specific MAC address
ethernets:
    enp3s0:
        dhcp4: true
    enp4s0:
        addresses:
            - 192.168.0.10/24
        gateway4: 192.168.0.1
        mtu: 1480
        nameservers:
            addresses:
                - 8.8.8.8
                - 9.9.9.9
    net1:
        addresses:
            - fe80::a00:10a/120
        gateway6: fe80::a00:101
        match:
            macaddress: 52:54:00:12:34:06
    net2:
        dhcp4: true
        dhcp6: true
        match:
            macaddress: 52:54:00:12:34:07
        mtu: 9000

Configuration of one of the machines in the laboratory:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# 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:
        eno1:
            dhcp4: false
            dhcp6: true
            addresses: [10.134.150.79/24]
            gateway4: 10.134.150.1
            nameservers:
                    addresses: [202.112.128.51, 202.112.128.50]
            #eno2:
            #addresses: [192.168.5.49/16]
            #dhcp4 : false
            #gateway4: 192.168.5.1
    version: 2

Of course, you can also use netplan throughSyntax of yamlWrite a configuration file to configure the bonding, bridges and VLANs of the network card;

Netplan design idea: refer to netplan design for details;

 

The figure above shows the relationship between netplan, netplan configuration file and back-end network renderer. Netplan can generate configuration files of corresponding network back-end renderers according to yaml configuration files; The network renderer configures the kernel network through the files generated by netplan.

Netplan common commands:

  • netplan generate: Use /etc/netplan to generate the required configuration for the renderers.
  • netplan apply: Apply all configuration for the renderers, restarting them as necessary.
  • netplan try: Apply configuration and wait for user confirmation; will roll back if network is broken or no confirmation is given.

Related reading:

Ubuntu Bionic: Netplan

Netplan Source Code

Netplan Design Doc

Netplan Reference