How to configure network in Ubuntu 18.04 and derivatives

As you may have noticed, the network configuration in Ubuntu 18.04 has changed. /etc/network/interfaces is not used anymore.
We’ll see how to configure network interfaces from command line in Ubuntu 18.04 and derivatives.
This is what we will learn:

  • Netplan tool and configuration files.
  • Modify configuration yaml file, a few examples.
  • Apply modified network settings.

This article applies to network configuration through networkd, which is going to be specially usefull for command line setup.
If you are on a desktop environment, you probably have network-manager installed, so just config your network interfaces using the network graphic tool.

Netplan tool and configuration files

The tool we have to use now is “netplan”.
This tools uses a different network configuration files, the new files are under /etc/netplan, there you can find a “YAML” file.
If there are no files there, or you need to create them, you can generate them with:

netplan generate

Modifying configuration yaml file, a few examples

This is how my VM test config file /etc/netplan/01-netcfg.yaml looks like:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes
    enp0s8:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.56.23/24]

We can easily find the patern here.
Lets see 2 typical examples.

Static IP with gateway and DNS

In this example, IP:192.168.56.23 and netmask 255.255.255.0, gateway:192.168.56.1 and DNS servers:192.168.56.1 and 8.8.8.8.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no
      addresses: [192.168.56.23/24]
      gateway4: 192.168.56.1
      nameservers:
        addresses: [192.168.56.1,8.8.8.8]

Dynamic IP – DHCP

Automatic config with DHCP:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
	  dhcp4: yes

Applying modified network settings

There are two ways to do this: using netplan tool or restarting networkd daemon.

Using Netplan

sudo netplan apply

Restarting networkd daemon

systemctl status systemd-networkd

 

3 thoughts on “How to configure network in Ubuntu 18.04 and derivatives”

  1. And how you use netplan to use dhcp for the IP but allow static DNS settings (IP and domain server order). ie the equivlent of:
    nmcli connection modify ${ID} ipv4.dns ‘10.10.100.21 10.10.100.22 10.17.182.31’
    nmcli connection modify ${ID} ipv4.ignore-auto-dns yes
    nmcli connection modify ${ID} ipv4.dns-search ‘abc01.domain.com. abc02.domain.com.’

    It looks like netplan is totally borked for this very common configuration…

    1. Edit the config file with:

      network:
       version: 2
        renderer: networkd
        ethernets:
          enp0s3:
            dhcp4: yes
            dhcp6: no
            nameservers:
              search: [abc01.domain.com, abc02.domain.com]
              addresses: [10.10.100.21, 10.10.100.22, 10.17.182.31]
      

      After that “netplan apply” and then you can check it with

      systemd-resolve --status
      

      The problem comes with “ignore-auto-dns yes”, looks that it’s still not possible using netplan.

      1. I have put together a workaround, this has only been tested using NetworkManager as the renderer… This script will configure NetworkManager as the renderer on the active NIC and set the search domain order and DNS servers to supercede what was supplied by dhcp.

        ################## Start Netplan config (renderer: NetworkManager)
        systemctl start NetworkManager
        NIC=$(nmcli device show|grep GENERAL.DEVICE|head -n1|awk ‘{print $2}’)

        #NetConn=$(nmcli device show|grep GENERAL.CONNECTION|head -n1|awk ‘{print $2}’)
        #IP=$(nmcli device show|grep IP4.ADDRESS|head -n1|awk ‘{print $2}’)
        #GATEWAY=$(nmcli device show|grep IP4.GATEWAY|head -n1|awk ‘{print $2}’)
        #sed -i ‘s/renderer: networkd/renderer: NetworkManager/’ //etc/netplan/01-netcfg.yaml

        ##### create Netplan yaml config file
        cat >/etc/netplan/01-netcfg.yaml <> /etc/netplan/01-netcfg.yaml
        cat >>/etc/netplan/01-netcfg.yaml <<EOF
        dhcp4: yes
        nameservers:
        search: [abc.domain.edu, def.domain.edu]
        addresses: [10.10.11.22, 10.10.11.23]

        EOF

        #work around DNS resolv bug
        systemctl stop systemd-resolved
        sed -i 's/#DNS=/DNS=10.10.11.22 10.10.11.23/' /etc/systemd/resolved.conf
        sed -i 's/#Domains=/Domains=abc.domain.edu def.domain.edu' /etc/systemd/resolved.conf
        systemctl start systemd-resolved
        systemctl restart NetworkManager
        netplan apply
        reboot
        ############### End Netplan Config

Leave a Reply

Your email address will not be published. Required fields are marked *