Netplan and how to prepare it correctly

Ubuntu is an amazing operating system, I haven’t worked with Ubuntu server for a long time and there was no point in updating my Desktop from a stable version. And not long ago I had to face the fresh release of Ubuntu server 18.04, my surprise knew no bounds when I realized that I was infinitely behind the times and I couldn’t set up the network because the good old system for setting up network interfaces by editing the /etc/network file /interfaces has sunk into mite. And what has come to replace her? something terrible and at first glance completely incomprehensible, meet Netplan.

To be honest, at first I couldn’t understand what the matter was and “why it was necessary, because everything was so convenient”, but after a little practice I realized that it has its own charm. network settings in Ubuntu, at least “I have not seen this in other distributions.” The essential difference between Netplan is that the configuration is written in the language YAML, yes, you heard right YAML, the developers decided to keep up with the times (and no matter how much they praised it, I still consider it a terrible language). The main disadvantage of this language is that it is very sensitive to spaces, let's look at the config with an example.

The configuration files are located in the /etc/netplan/filename.yaml path, between each block when there should be + 2 spaces.

1) The default header looks like this:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0f0:
      dhcp4:no

Let's take a look at what we've done so far:

  • network: is the start block of the configuration.
  • renderer: networkd - here we specify the network manager we will use, it is either networkd or NetworkManager
  • version: 2 - here, as I understand it, is the YAML version.
  • ethernets: - this block says that we will configure the ethernet protocol.
  • enps0f0: - specify which network adapter we will configure.
  • dhcp4: no - disable DHCP v4, for 6 v6 dhcp6, respectively

2) Let's try to assign ip addresses:

    enp3s0f0:
      dhcp4:no
      macaddress: bb:11:13:ab:ff:32
      addresses: [10.10.10.2/24, 10.10.10.3/24]
      gateway4: 10.10.10.1
      nameservers:
        addresses: 8.8.8.8

Here we set poppy, ipv4, gateway and dns server. Note those that if we need more than one ip address, then we write them separated by commas with a mandatory space after.

3) What if we need bonding?

  bonds:
    bond0:
      dhcp4: no
      interfaces: [enp3s0f0, enp3s0f1]
      parameters: 
        mode: 802.3ad
        mii-monitor-interval: 1

  • bonds: - a block explaining that we will set up bonding.
  • bond0: - arbitrary interface name.
  • interfaces: - a set of interfaces assembled in bond-ding, "as previously stated, if there are several parameters, we describe them in square brackets".
  • parameters: - describe the parameter setting block
  • mode: - specify the mode on which bonding will work.
  • mii-monitor-interval: - set the monitoring interval to 1 second.

Inside the block named bond, you can also configure parameters such as addresses,gateway4,routes, etc.

We have added redundancy for our network, now it remains only to hang up Vlan and the setup is complete.

vlans: 
    vlan10:
      id: 10
      link: bond0
      dhcp4: no
      addresses: [10.10.10.2/24]
      gateway: 10.10.10.1
      routes:
        - to: 10.10.10.2/24
          via: 10.10.10.1
          on-link: true

  • vlans: - declare the vlan configuration block.
  • vlan10: - arbitrary name of the vlan interface.
  • id: - tag of our vlan.
  • link: - interface through which vlan will be available.
  • routes: - declare a route description block.
  • - to: - set the address / subnet to which the route is needed.
  • via: - specify the gateway through which our subnet will be available.
  • on-link: - specify that routes should always be registered when link is raised.

Pay attention to how I place spaces, in YAML this is very important.

Here we have described network interfaces, created bonding, and even added vlans. Let's apply our config, the netplan apply command will check our config for errors and apply it if successful. Next, the config will rise by itself when the system is rebooted.

Having collected all the previous blocks of code, here is what we got:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0f0:
      dhcp4: no
    ensp3s0f1:
      dhcp4: no
  bonds:
    bond0:
      dhcp4: no
      interfaces: [enp3s0f0, enp3s0f1]
      parameters: 
        mode: 802.3ad
        mii-monitor-interval: 1
  vlan10:
      id: 10
      link: bond0
      dhcp4: no
      addresses: [10.10.10.2/24]
      routes:
        - to: 10.10.10.2/24
          via: 10.10.10.1
          on-link: true
  vlan20:
    id: 20
    link: bond0
    dhcp4: no
    addresses: [10.10.11.2/24]
    gateway: 10.10.11.1
    nameserver:
      addresses: [8.8.8.8]
    

Here is our network and it is ready for operation, everything turned out to be not as scary as it seemed at first, and the code turned out to be very beautiful and readable. PC thanks for netplan there is an excellent manual at the link https://netplan.io/.

Source: habr.com

Add a comment