Configuring networks

Network configuration on Ubuntu is handled through Netplan, which provides a high-level, distribution-agnostic way to define how the network on your system should be set up via a YAML configuration file.

While Netplan is a configuration abstraction renderer that covers all aspects of network configuration, here we will outline the underlying system elements like IP addresses, Ethernet devices, name resolution and so on. We will refer to the related Netplan settings where appropriate, but we do recommend studying the Netplan documentation in general.

Ethernet interfaces

Ethernet interfaces are identified by the system using predictable network interface names. These names can appear as eno1 or enp0s25. However, in some cases an interface may still use the kernel eth# style of naming.

Identify Ethernet interfaces

To quickly identify all available Ethernet interfaces, you can use the ip command from iproute2 as shown below.

$
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:e2:52:42 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.102.66.200/24 brd 10.102.66.255 scope global dynamic eth0
       valid_lft 3257sec preferred_lft 3257sec
    inet6 fe80::216:3eff:fee2:5242/64 scope link
       valid_lft forever preferred_lft forever

Or call Netplan to get a higher level overview of your network interfaces and correlated information, such as nameserver or (default) routes.

$
sudo netplan status -a
     Online state: online
    DNS Addresses: 127.0.0.53 (stub)
       DNS Search: internal

●  1: lo ethernet UNKNOWN/UP (unmanaged)
      MAC Address: 00:00:00:00:00:00
        Addresses: 127.0.0.1/8
                   ::1/128

●  2: enp0s25 ethernet UP (networkd: enp0s25)
      MAC Address: 00:16:3e:e2:52:42 (ASIX Electronics Corp.)
        Addresses: 10.102.66.200/24 (dynamic, dhcp)
                   fe80::216:3eff:fee2:5242/64 (link)
    DNS Addresses: 10.102.66.1
                   fd00::cece:1eff:fe3d:c737
       DNS Search: internal
           Routes: default via 10.102.66.1 from 10.102.66.200 metric 100 (dhcp)
                   10.102.66.0/24 from 10.102.66.200 metric 100 (link)
                   2001:16b8:bd99:3d00::/56 via fe80::cece:1eff:fe3d:c737 metric 100 (ra)
                   default via fe80::cece:1eff:fe3d:c737 metric 100 (ra)

Another application that can help identify all network interfaces available to your system is the lshw command. This command provides greater details around the hardware capabilities of specific adapters. In the example below, lshw shows a single Ethernet interface with the logical name of eth4 along with bus information, driver details and all supported capabilities.

$
sudo lshw -class network
  *-network
       description: Ethernet interface
       product: MT26448 [ConnectX EN 10GigE, PCIe 2.0 5GT/s]
       vendor: Mellanox Technologies
       physical id: 0
       bus info: pci@0004:01:00.0
       logical name: eth4
       version: b0
       serial: e4:1d:2d:67:83:56
       slot: U78CB.001.WZS09KB-P1-C6-T1
       size: 10Gbit/s
       capacity: 10Gbit/s
       width: 64 bits
       clock: 33MHz
       capabilities: pm vpd msix pciexpress bus_master cap_list ethernet physical fibre 10000bt-fd
       configuration: autonegotiation=off broadcast=yes driver=mlx4_en driverversion=4.0-0 duplex=full firmware=2.9.1326 ip=192.168.1.1 latency=0 link=yes multicast=yes port=fibre speed=10Gbit/s
       resources: iomemory:24000-23fff irq:481 memory:3fe200000000-3fe2000fffff memory:240000000000-240007ffffff

Ethernet Interface logical names

Interface logical names can also be configured via a Netplan configuration. If you would like control which interface receives a particular logical name use the match and set-name keys. The match key is used to find an adapter based on some criteria like MAC address, driver, etc. The set-name key can be used to change the device to the desired logical name.

network:
  version: 2
  renderer: networkd
  ethernets:
    eth_lan0:
      dhcp4: true
      match:
        macaddress: 00:11:22:33:44:55
      set-name: eth_lan0

Ethernet Interface settings

ethtool is a program that displays and changes Ethernet card settings such as auto-negotiation, port speed, duplex mode, and Wake-on-LAN. The following is an example of how to view the supported features and configured settings of an Ethernet interface.

$
sudo ethtool eth4
Settings for eth4:
    Supported ports: [ FIBRE ]
    Supported link modes:   10000baseT/Full
    Supported pause frame use: No
    Supports auto-negotiation: No
    Supported FEC modes: Not reported
    Advertised link modes:  10000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: No
    Advertised FEC modes: Not reported
    Speed: 10000Mb/s
    Duplex: Full
    Port: FIBRE
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: off
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000014 (20)
                   link ifdown
    Link detected: yes

IP addressing

The following section describes the process of configuring your system’s IP address and default gateway needed for communicating on a local area network and the Internet.

Temporary IP address assignment

For temporary network configurations, you can use the ip command which is also found on most other GNU/Linux operating systems. The ip command allows you to configure settings which take effect immediately – however they are not persistent and will be lost after a reboot.

To temporarily configure an IP address, you can use the ip command in the following manner. Modify the IP address and subnet mask to match your network requirements.

$
sudo ip addr add 10.102.66.200/24 dev enp0s25

The ip command can then be used to set the link up:

$
ip link set dev enp0s25 up

Or down:

$
ip link set dev enp0s25 down

To verify the IP address configuration of enp0s25, you can use the ip command in the following manner:

$
ip address show dev enp0s25
10: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:e2:52:42 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.102.66.200/24 brd 10.102.66.255 scope global dynamic eth0
       valid_lft 2857sec preferred_lft 2857sec
    inet6 fe80::216:3eff:fee2:5242/64 scope link
       valid_lft forever preferred_lft forever6

To configure a default gateway, you can use the ip command in the following manner. Modify the default gateway address to match your network requirements.

$
sudo ip route add default via 10.102.66.1

You can also use the ip command to verify your default gateway configuration:

$
ip route show
default via 10.102.66.1 dev eth0 proto dhcp src 10.102.66.200 metric 100
10.102.66.0/24 dev eth0 proto kernel scope link src 10.102.66.200
10.102.66.1 dev eth0 proto dhcp scope link src 10.102.66.200 metric 100

If you require DNS for your temporary network configuration, you can use the resolvectl command to set DNS servers temporarily. The example below shows how to set two DNS servers for the interface enp0s25, which should be changed to servers appropriate for your network. A more lengthy description of the proper (persistent) way to do DNS client configuration is in a following section.

$
sudo resolvectl dns enp0s25 8.8.8.8 8.8.4.4

If you no longer need this configuration and wish to purge all IP configuration from an interface, you can use the ip command with the flush option:

$
ip addr flush eth0

Note

Flushing the IP configuration using the ip command does not clear the contents of systemd-resolved. You must remove or modify those entries manually, e.g by calling sudo resolvectl revert enp0s25. Or re-boot, which should also cause the symlink /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf, to be restored.

Dynamic IP address assignment (DHCP client)

To configure your server to use DHCP for dynamic address assignment, create a Netplan configuration in the file /etc/netplan/99_config.yaml. The following example assumes you are configuring your first Ethernet interface identified as enp3s0.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: true

The configuration can then be applied using the netplan command:

$
sudo netplan apply

Static IP address assignment

To configure your system to use static address assignment, create a netplan configuration in the file /etc/netplan/99_config.yaml. The example below assumes you are configuring your first Ethernet interface identified as eth0. Change the addresses, routes, and nameservers values to meet the requirements of your network.

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 10.10.10.2/24
      routes:
        - to: default
          via: 10.10.10.1
      nameservers:
          search: [mydomain, otherdomain]
          addresses: [10.10.10.1, 1.1.1.1]

The configuration can then be applied using the netplan command.

$
sudo netplan apply

Note

netplan in Ubuntu Bionic 18.04 LTS doesn’t understand the “to: default” syntax to specify a default route, and should use the older gateway4: 10.10.10.1 key instead of the whole routes: block.

The loopback interface is identified by the system as lo and has a default IP address of 127.0.0.1. It can be viewed using the ip command.

$
ip address show lo
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever

Adding a multiple IP addresses to one interface

A single interface can broadcast multiple IP addresses to the network. Multiple IP addresses can be used to:

  • host multiple web domains by configuring virtual hosts using different IP addresses rather than server names

  • host multiple server names using Samba

Add IP addresses to an interface by editing your netplan configuration found in /etc/netplan.

Multiple static IP addresses

This example assigns multiple static addresses to a single interface (enter the appropriate values for your server and network):

network:
  version: 2
  ethernets:
    eno1:
      addresses:
        - 192.168.2.99/24
        - 192.168.2.100/24:
            label: eno1:1
        - 192.168.2.101/24:
            label: eno1:some-label

Adding labels to the IP addresses allows you to reference the devices by name in configuration files rather than the IP address which can change.

Dynamic IP addresses

This example adds one (or more) IP addresses to an interface that also has a dynamic address assigned by DHCP.

network:
  version: 2
  ethernets:
    eno1:
      addresses:
        - 192.168.2.100/24:
            label: eno1:some-label
      dhcp4: true

Note

A single interface can only have one address assigned by DHCP. To have multiple dynamic addresses, configure multiple interfaces. Do not add DHCP if it is configured on the same interface in another netplan file, such as one written by cloud-init.

Apply and verify the IP addresses

Apply the configuration to enable the additional IP addresses:

$
sudo netplan apply

Verify the IP addresses are available locally using one or more of the following:

$
netplan status #displays the current network state
$
ip addr    #shows all network interfaces
$
ip addr show <interface_name>    #shows single interface
$
hostname -i    #shows only the available IP addresses on the host

Check the output to see that the addresses were applied to the interface that was configured.

Verify the IP addresses are available on the network. From a different computer on the network:

$
ping 192.168.0.100    #configured IP addresses
$
ping 192.168.0.101

Name resolution

Name resolution (as it relates to IP networking) is the process of mapping hostnames to IP addresses, and vice-versa, making it easier to identify resources on a network. The following section will explain how to properly configure your system for name resolution using DNS and static hostname records.

DNS client configuration

Traditionally, the file /etc/resolv.conf was a static configuration file that rarely needed to be changed, or it automatically changed via DHCP client hooks. systemd-resolved handles nameserver configuration, and it should be interacted with through the resolvectl command. Up to Ubuntu 20.04 LTS the systemd-resolve command could also be used. Netplan configures systemd-resolved to generate a list of nameservers and domains to put in /etc/resolv.conf, which is a symlink:

/etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf

To configure the resolver, add the IP addresses of the appropriate nameservers for your network to the netplan configuration file. You can also add optional DNS suffix search-lists to match your network domain names. The resulting file might