Categories: TutorialsUbuntu

Setting a static IP address on Ubuntu 18.04 and higher using netplan

During the installation of Ubuntu, it will try and automatically obtain an IP address using DHCP. While that may be fine for most users, if you are wanting to set up a server, the chances are you will want it to always have the same IP address.

There are two options for ensuring you get/use the same IP address at every boot up. You could set up a IP address reservation on the DHCP server based on the hardware/MAC address of the network interface, or alternatively, set the operating system to use a static IP address.

If for whatever reason you don’t want to go down the path of setting a DHCP reservation (you may not have access to the DHCP server; unsure how to set it up; etc), the only option is to set a static IP address on the machine itself.

Sponsored

The following steps will guide you through setting up your Ubuntu installation to use a static IP address on Ubuntu 18.04 and higher using the netplan configuration tools.

Note: With Ubuntu versions prior to 18.04, you had to manually edit the /etc/network/interfaces file to set up static IP addressing. (See “Setting a static IP address in Ubuntu” for Ubuntu versions prior to 18.04).

Setting static IP using netplan config

With the introduction of Ubuntu 18.04, along came the netplan YAML based network configuration tool.

The netplan configuration files are located in the /etc/netplan folder.

Find the netplan config file

Run the following command to list the netplan configuration files.

$ ls /etc/netplan

You should see something like the following:

$ ls /etc/netplan
01-netcfg.yaml
$

or

$ ls /etc/netplan
50-cloud-init.yaml
$

In this guide we will be using the “01-netcfg.yaml” file, as that is the default for a bare metal installation of Ubuntu 18.04.

Edit the netplan configuration file

Open up your netplan config file in a text editor.

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

You will be presented with a default configuration similar to this:
(network interface names will differ machine to machine)

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: yes

Disable DHCP
The first thing you will want to do, is disable the dhcp4 option. Set this to false.

      dhcp4: false

Then you can add the following static IP options.

The IP address and netmask
Add the following line below the “dhcp4” line mentioned above. Modify the IP address and netmask to suit. Ensure you keep the indentation correct as per the final example yaml file further down.

Sponsored
      addresses: [192.168.0.20/24]

In this example, the IP address we are assigning to the machine, is “192.168.0.20” with a subnet mask of 255.255.255.0.

The default gateway
Add the following line below the “addresses” line mentioned above. Modify the gateway IP address to suit. Ensure you keep the indentation correct as per the final example yaml file further down.

      gateway4: 192.168.0.1

In this example, the default gateway will be pointing to “192.168.0.1”.

DNS Nameservers
Add the following lines below the “gateway4” line mentioned above. Modify the DNS server ip addresses to suit. Ensure you keep the indentation correct as per the final example yaml file further down.

      nameservers:
        addresses: [8.8.8.8,8.8.4.4,1.1.1.1]

In this example, the DNS servers in use are seperated out by commas. They are currenly set to “8.8.8.8”, “8.8.4.4” and “1.1.1.1”.

Example netplan yaml config file

The file should now look something like this:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: false
      addresses: [192.168.0.20/24]
      gateway4: 192.168.0.1
      nameservers:
        addresses: [8.8.8.8,8.8.4.4,1.1.1.1]

Note: With YAML configuration files it is critical to get the spacing/indentation/layout correct.
Make sure the layout and indentaion matches the example above, otherwise you may get syntax errors when going to trying to reload the netplan config changes.

Save the configuration, and exit the editor.

Applying the configuration change

You can either apply the changes straight away, with the following command:

$ sudo netplan apply

or, if you want to test it first, and potentially roll back any changes, you can use the following command:

$ sudo netplan try

This option apply the changes, and provide a 120 timeout where by if you don’t accept the changes, they will revert back. This is useful to prevent you from locking yourself out of the system, if the network change didn’t work the way you were intending.
The message that will be displayed will look like this:

$ sudo netplan try
Do you want to keep these settings?


Press ENTER before the timeout to accept the new configuration


Changes will revert in 120 seconds

Simply press the ENTER key on the keyboard before the timeout expires, and the changes will have been applied.

Your PC should now retain its IP address across reboots, and wont be relying on a DHCP server being operational. Use the ‘ip address show’ command to confirm the IP address has been updated successfully.

$ ip address show

You should see an example output like this. This should now reflect your static IP address as configured with netplan.

1: lo:  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: eno1:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.20/24 brd 192.168.0.255 scope global eno1
       valid_lft forever preferred_lft forever
    inet6 fe80::aabb:ccdd:aabb:ccdd/64 scope link
       valid_lft forever preferred_lft forever
3: wlp2s0:  mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether aa:cc:ee:11:22:33 brd ff:ff:ff:ff:ff:ff
,multicast>,multicast,up,lower_up>,up,lower_up>

The post Setting a static IP address on Ubuntu 18.04 and higher using netplan appeared first on Networking HowTos.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

15 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

2 days ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

3 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

4 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

4 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago