You are here

Set Static IP Address on Ubuntu 20.04

Submitted by Alan Mels on Fri, 02/04/2022 - 03:52

The detailed version is on https://ubuntu.com/server/docs/network-configuration

Find your network interface by running ip addr

In the following procedure, we will see how to set up the static IP in a Ubuntu system.

Ubuntu 20.04 uses netplan as a default network manager. The configuration file for the netplan is stored in the /etc/netplan directory. You can find this configuration file listed in the /etc/netplan directory the following command:

  1. ls /etc/netplan

The above command will return the name of the configuration file with the .yaml extension, which in my case was 01-network-manager-all.yaml.

Before making any changes to this file, make sure to create a backup copy of it. Use the cp command to do so:
$ sudo cp /etc/netplan/01-network-manager-all.yaml 01-network-manager-all.yaml.bak

Note: You might have a configuration file with the name other than the 01-network-manager-all.yaml. So make sure you use the right configuration file name in the commands.

You can edit the netplan configuration using any text editor. Here we are using the Nano text editor for this purpose.
$ sudo nano /etc/netplan/01-network-manager-all.yaml

Then add the following lines by replacing the interface name, IP address, gateway, and DNS information that fit your networking needs.
network:

  1. version: 2
  2. renderer: NetworkManager
  3. ethernets:
  4. ens33:
  5. dhcp4: no
  6. addresses:
  7. - 192.168.72.140/24
  8. gateway4: 192.168.72.2
  9. nameservers:
  10. addresses: [8.8.8.8, 8.8.4.4]

Once done, save and close the file.

sudo netplan try

If it validates the configuration, you will receive the configuration accepted message; otherwise, it rolls back to the previous configuration.
Next, run the following command to apply the new configurations.
$ sudo netplan apply

After this, confirm the IP address of your machine using the following command:
$ ip a