How to Configure Static IP Address on Ubuntu 22.04

Choose a different version or distribution

Introduction

Before we begin talking about how to configure Static IP Address on Ubuntu 22.04. Let’s briefly understand – What is a Static IP Address?

Your router DHCP server is responsible for assigning the IP addresses. There can be many use cases in which you want to set a static IP address like you may want to configure port forwarding or run a media server on the network.

In this tutorial, you will configure the Static IP Address on Ubuntu 22.04. We will also address some FAQs related to the Static IP Address.

Step 1 – Configuring the Static IP address using DHCP

One of the easiest ways to assign a static IP address to the device on your LAN is to set up a static DHCP server on your router. Static DHCP which is also called DHCP reservation is a very useful feature found on routers that enable the DHCP server to assign the same IP addresses to a specific network device every time it asks for the address from the DHCP server.

For this, you need to assign a static IP to the device’s unique MAC address. There are different ways to configure DHCP for different routers, it is recommended to go through the vendor's manual.

Step 2 - Netplan

Netplan is a default network management tool present on ubuntu which now replaces the configuration file etc/network/interfaces which was used previously to configure the network on Ubuntu.

The configuration files in Netplan follow the yaml syntax.  You need to create a yaml description of the interface and Netplan will generate the required configuration files for your chosen renderer tool.

There is a total of two renderers supported by Netplan, NetworkManager, and Systemd-networkd. For desktop machines, NetworkManager is used and Systemd-networkd is used on servers without GUI

Step 3 - Configuring Static IP address on Ubuntu Server

1) The latest versions of Ubuntu uses Predictable Network Interface Names.

You will first identify the name of the Ethernet interface which you want to configure then you need to use ip link command like below:

ip link

The above command will print a list of all available network interfaces. Here, the name of the interface 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
3: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether 56:00:00:60:20:0a brd ff:ff:ff:ff:ff:ff

2) Netplan configuration files are present in the /etc/netplan directory with .yaml extension. You will find one or two YAML files in this directory. The file will differ from setup to setup. Basically, the file has a name01-netcfg.yaml, 50-cloud-init.yaml or, as NN_interfaceName.yaml, but in your system, it will differ.

3) You'll need to disable cloud-init if your Ubuntu cloud instance was provisioned with it. Create the following file to do so:

sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}

4) Now, open the YAML configuration file using your text editor:

sudo nano /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: yes

5) Let's understand the code in short before changing the configuration.

Each Netplan Yaml file starts with a network key that has at least two necessary elements. The first element is the version of the network configuration format and the second one is the device type. It supports different device types like ethernets, bonds, bridges, or the vlans.

The above configuration also has a renderer type. If you install Ubuntu in a server mode, then the renderer configuration is done using networkd as the back end.

Under the device’s type which in this case is ethernet, you can specify one or more network interfaces. Like in this example, we have only one interface ens3 that is configured to obtain IP addressing from a DHCP server dhcp4: yes. So, to assign a static IP address to ens3 interface edit the file as follows:

  • Setting the DHCP to dhcp4: no.
  • Specifying the static IP address 192.168.121.199/24. Under, the addresses: you need to add one or more IPv4 or IPv6 IP addresses that will be assigned to the network interface.
  • Specifying gateway gateway4: 192.168.121.1.
  • Under nameservers, set IP addresses of the nameservers as addresses: [8.8.8.8, 1.1.1.1]:
                           /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.121.199/24
      gateway4: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

6) It is necessary to follow the code indent standards while editing the Yaml files. If there are any syntax errors in the configuration, the changes will not get applied.

7) After, this save and close the file. Continue to apply the changes, using the following command:

sudo netplan apply

8) You can then verify the changes by using the following command:

ip addr show dev ens3
Output

3: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 56:00:00:60:20:0a brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.199/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

That's it, you have successfully assigned the Static IP to your Ubuntu server.

Step 4 - Configuring Static IP address on the Ubuntu Desktop

There is no technical knowledge required for setting up a static IP address on Ubuntu Desktop Computers.

1) Go to the Activities screen, search for the network and click on the Network Icon. It will then open the GNOME Network Configuration settings. You need to click on the cog icon now.

2) Then, the Network interface settings dialog box will appear:

3) In the “IPV4” Method section, you need to select “Manual” and enter your static IP address, the Netmask, and Gateway. After completing, click on the Apply button.

4) Now, after setting set-up a static IP Address, you should open your terminal using Ctrl+Alt+T a keyboard shortcut or by clicking on the terminal icon. You can verify the changes using the following command:

ip addr

The output below will show the interface IP address:

Output

...
2: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:e9:40:f2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.121.106/24 brd 192.168.121.255 scope global dynamic noprefixroute ens3
       valid_lft 3523sec preferred_lft 3523sec
    inet6 fe80::5054:ff:fee9:40f2/64 scope link 
       valid_lft forever preferred_lft forever

FAQs to Configure Static IP Address on Ubuntu 22.04

Why would I want to configure a static IP address on Ubuntu 22.04?

Configuring a static IP address on Ubuntu 22.04 can provide a more stable and predictable network connection. It can also be necessary for some network configurations, such as setting up a server.

How do I find my current IP address on Ubuntu 22.04?

You can find your current IP address on Ubuntu 22.04 by typing ip addr show in the terminal. Your IP address will be listed next to inet on the line for your network interface.

Can I configure a static IP address on Ubuntu 22.04 through the graphical interface?

Yes, you can configure a static IP address on Ubuntu 22.04 through the graphical interface by opening the "Settings" application, navigating to the "Network" tab, selecting your network connection, and entering the necessary information in the "IPv4" tab.

Can I revert back to a dynamic IP address after configuring a static IP address on Ubuntu 22.04?

Yes, you can revert back to a dynamic IP address by editing the netplan configuration file and removing the static IP address configuration information.

Conclusion

We hope this detailed guide helped you to Configure the Static IP Address on Ubuntu 22.04.

If you have any queries please leave them in the comment below. We'll be happy to address them.