Configure Static IP Last updated: 2022.04.25

Overview

Ubuntu 20.04, Ubuntu 18.04

Assign a static IP on your Ubuntu system using Netplan. If you are trying to set a static IP on an Ubuntu desktop, this is not the page you are looking for.

Assumptions

  • Ubuntu installation completed.
  • Logged in as administrative user.

Netplan Files

Your Ubuntu system likely already has a Netplan configuration file present in the /etc/netplan/ directory; it may have one of three names: 01-netcfg.yaml, 01-network-manager-all.yaml, or 50-cloud-init.yaml. The leading number (##-) in the filename indicates the order of priority in which Netplan will apply the included rules.

01-netcfg.yaml

The 01-netcfg.yaml file may be used to manually configure the system’s network interfaces. If this file already exists, it likely contains a DHCP configuration that we will edit in the next section. If your system is using one of the other configuration files listed below, they should be disabled after setting up 01-netcfg.yaml.

01-network-manager-all.yaml

The 01-network-manager-all.yaml file will contain a configuration instructing Ubuntu to allow NetworkManager to control all interfaces on the system. Usage of NetworkManager is beyond the scope of this bit, and likely not relevant on a server installation. The configuration will resemble the following. Remove this file after setting up 01-netcfg.yaml if you do not wish to use NetworkManager.

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

50-cloud-init.yaml

The 50-cloud-init.yaml file will be present if the network interface was configured using cloud-init. The configuration will be prefaced with the following comment. Follow comment’s disable instructions after setting up 01-netcfg.yaml if you do not wish to use cloud-init.

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

If your server is hosted with a cloud provider, refer to the providers documentation for editing the IP address when a cloud-init.yaml file is present; you may not be able to change the address on the system.

Set IP Address

Create or open the 01-netcfg.yaml file in your editor of choice. Interface eth0 will be used as example in this section, replace it with whatever interface you are configuring.

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

DHCP

The following configuration is what you would see for a DHCP configuration on interface eth0.

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

Static

The next snippet is an example of a static IP definition for interface eth0. Paste it into 01-netcfg.yaml and modify it to suit your server.

  • Replace eth0 with your interface of choice.
  • Set dhcp4 to no, disabling DHCP.
  • Set addresses to the IP address you want the system to have.
  • Set gateway4 to the router’s IP address.
  • Replace nameservers: addresses with your DNS servers of choice.
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [172.16.0.101/24]
      gateway4: 172.16.0.1
      nameservers:
        addresses: [1.1.1.1,1.0.0.1]

Apply

After configuring Netplan, and disabling any Netplan configurations you do not plan on using, apply the changes.

sudo netplan --debug apply