How to set up a static IP address in Ubuntu 20.04

This article explains how to set up a static IP address in Ubuntu 20.04.

Typically in most network configurations, the IP address is assigned dynamically by the DHCP server of the router. Setting a static IP address may be required in various situations, such as when setting up port forwarding or when starting a media server.

Configuring a static IP address using DHCP

The easiest and most recommended way to assign a static IP address to a device on your local network is to set up static DHCP on your router. Static DHCP or DHCP Reservation is a feature of most routers that causes the DHCP server to automatically assign the same IP address to a specific network device every time the device requests an address from the DHCP server. It works by assigning a static IP address to the device’s unique MAC address.

The steps to set up DHCP reservations vary from router to router. Refer to vendor documentation for more information.

Netplan

Ubuntu 17.10 and newer uses Netplan as the default network management tool. Previous versions of Ubuntu used ifconfig and its / etc / network / interfaces config file to configure the network.

Netplan configuration files are written in YAML syntax with a .yaml file extension. To set up a network interface using Netplan, you need to create a YAML description for the interface, and Netplan will generate the necessary configuration files for the selected renderer.

Netplan supports two renderers, NetworkManager and Systemd-networkd. NetworkManager is mainly used on desktops, while Systemd-networkd is used on servers without a GUI.

Setting up a static IP address on an Ubuntu server

In Ubuntu 20.04, the system identifies network interfaces using “predictable network interface names”.

The first step to setting up a static IP address is to determine the name of the Ethernet interface you want to configure. To do this, use the ip link command as shown below:

ip link

The command prints a list of all available network interfaces. In this example, the interface name is ens3:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff

Netplan configuration files are stored in the / etc / netplan directory. You will likely find one or more YAML files in this directory. The file name may differ from setting to setting. The file is usually named either 01-netcfg.yaml, 50-cloud-init.yaml, or NN_interfaceName.yaml, but it may be different on your system.

If your cloud-based Ubuntu instance has cloud-init, you need to disable it. To do this, create the following file:

sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

network: {config: disabled}

To assign a static IP address on the network interface, open the YAML configuration file in a text editor:

sudo nano /etc/netplan/01-netcfg.yaml

/etc/netplan/01-netcfg.yaml

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

Before changing the configuration, let’s briefly explain the code.

Every Netplan Yaml file starts with a network key, which contains at least two required elements. The first required element is the version of the network configuration format, and the second is the device type. The device type can be ethernets, bonds, bridges, or vlans.

The above config also has a line showing the renderer type. By default, if you installed Ubuntu in server mode, the renderer is configured to use networkd as the backend.

Under the device type (ethernets) you can specify one or more network interfaces. In this example, we only have one ens3 interface configured to receive IP addressing from the dhcp4: yes DHCP server.

To assign a static IP address to the ens3 interface, edit the file as follows:

  • Set DHCP to dhcp4: no.
  • Enter a static IP address. In the addresses: section, you can add one or more IPv4 or IPv6 IP addresses to be assigned to the network interface.
  • Specify the gateway.
  • In the nameservers section, set the IP addresses of the nameservers.

/etc/netplan/01-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.121.221/24
      gateway4: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

When editing YAML files, make sure you follow the YAML indentation standards. If the syntax is incorrect, the changes will not be applied.

After that, save the file and apply the changes by running the following command:

sudo netplan apply

Test the changes by typing:

ip addr show dev ens3
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.221/24 brd 192.168.121.255 scope global dynamic ens3
       valid_lft 3575sec preferred_lft 3575sec
    inet6 fe80::5054:ff:feb0:f500/64 scope link 
       valid_lft forever preferred_lft forever

All! You have assigned a static IP address to your Ubuntu server.

Setting up a static IP address on the Ubuntu desktop

Setting up a static IP address on Ubuntu desktops requires no technical knowledge.

On the Actions screen, find β€œsettings” and click the icon. The GNOME Preferences window will open. Depending on the interface you want to change, click the Network or Wi-Fi tab. To open interface settings, click the gear icon next to the interface name.

On the IPV4 Method tab, select Manual and enter your static IP address, netmask and gateway. After that press the “Apply” button.

To test your changes, open a terminal with the keyboard shortcut Ctrl + Alt + T or by clicking the terminal icon and run:

ip addr

The output will show the IP address of the interface:

...
2: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 50:5b:c2:d8:59:7d brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.221/24 brd 192.168.31.255 scope global dynamic noprefixroute wlp1s0
       valid_lft 38963sec preferred_lft 38963sec
    inet6 fe80::45e3:7bc:a029:664/64 scope link noprefixroute 

Output

We showed you how to set up a static IP address in Ubuntu 20.04.

If you have any questions, please leave a comment below.

Sidebar