Post

Configure Network Interfaces with Netplan on Linux

How to setup a static IP address using the netplan network configuration utility.

Configure Network Interfaces with Netplan on Linux

Today, I wanted to configure a network interface on my Ubuntu server to always use the same IP address. Normally, I would expect to find basic network configs in /etc/network/interfaces. As I do things on my Ubuntu server, I continue to find minor differences compared to what I expect. In this case, a network configuration utility for Linux, called netplan, is being used. I imagine it is available for pretty much any Linux distro.

Below are the steps I used to configure my Ubuntu server (version 24.04.3 LTS) to request a specific IP address for the ethernet port.

Select your network interface

You can apply this to any network interface you would like. List your available network interfaces with the following command.

1
ip a

You should see similar output as show below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 noprefixroute 
       valid_lft forever preferred_lft forever
2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:ab:10:e3:a4:e1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.42/24 brd 192.168.1.255 scope global enp4s0
       valid_lft forever preferred_lft forever
    inet6 fe80::24e:11f:fee4:1234/64 scope link 
       valid_lft forever preferred_lft forever
3: wlp3s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 20:43:34:12:33:22 brd ff:ff:ff:ff:ff:ff

Without going in to detail about network naming schemes1, these are how the interfaces are prefixed:

  • enp is for ethernet
  • wlp is for wireless

Configure your network interface

Commands are available to configure network interfaces. I prefer making all of my changes to the config file. With netplan the configs are in YAML format2, located in the /etc/netplan/ directory. If no config files exist, create one. Keep in mind files here are run in the order you see them sorted when you run ls. I am creating a file called 50-ethernet.yaml with the following contents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
network:
  version: 2
  renderer: networkd # Use NetworkManager on desktop environments
  ethernets:
    enp4s0:
      dhcp4: no
      addresses:
        - 192.168.1.20/24 # Sets the IP to 192.168.1.20
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 192.168.1.1 # Local router
          #- 58.6.115.42 # Add as many as you'd like
          #- 58.6.115.43

Considerations for different environments

  • If you do this on a system running a desktop environment (e.g. KDE or Gnome) you may need to used renderer: NetworkManager.
  • In this example, the nameserver address is for the local router. It will use whatever DNS your router is configured to use. You can specify your own by replacing 192.168.1.1 with another DNS server (e.g. 9.9.9.9). List as many additional servers as you’d like on a new line with the same amount of indentation as the one above it.

Apply the configuration

Prevent accidental lock out! Before applying the configuration, be sure you have access to the system with a monitor or over another network interface that you are not configuring.

Tell netplan to apply it if the config.

1
sudo netplan apply # Use --debug if there are problems

Check the configuration

After applying the new configuration, you should see that the enp4s0 interface is now using the IP address you specified in the YAML config file.

1
ip addr show enp4s0 # Use `ip a` to show all interfaces

Additional reading

  1. Network device naming schemes ↩︎

  2. Learn about formatting at yaml.org, under “YAML Resources”. ↩︎

This work by Jason Raveling is licensed under CC BY-ND 4.0 .