Setting a Static IP in Ubuntu – Linux IP Address Tutorial

In most network setups, the router’s DHCP server assigns IP addresses dynamically by default. However, if you want your system’s IP address to remain consistent, you can configure it to use a static IP.

In this article, we’ll learn how to set a static IP in Ubuntu using two different methods.

Static IP addresses are commonly used in the following scenarios:

  • Setting up port forwarding.
  • Configuring your system as a server, such as an FTP server, web server, or media server.

Pre-requisites:

To complete this tutorial, you will need the following:

  • A working Ubuntu installation, preferably with a graphical user interface (GUI).
  • Sudo privileges, as we will be modifying system configuration files.

How to Set a Static IP Using the Command Line

In this section, we will walk through all the steps required to configure a static IP in detail.

Step 1: Launch the terminal

You can open the terminal using the shortcut Ctrl + Shift + T.

Step 2: Note information about the current network

To make the necessary configuration changes, we need to gather our current network details, including the assigned IP address, subnet mask, and network adapter name.

Use the command below to view details of the available adapters and their corresponding IP information.

ip a

The output will resemble something like this:

Image

In my network, the current adapter is eth0, but it may be different on your system.

  • Note the current network adapter name

Since my current adapter is eth0, the following details are relevant.

6: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:df:c3:ad brd ff:ff:ff:ff:ff:ff
    inet 172.23.199.129/20 brd 172.23.207.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fedf:c3ad/64 scope link
       valid_lft forever preferred_lft forever

It is worth noting that the current IP 172.23.199.129 is dynamically assigned. It has 20 bits reserved for the netmask. The broadcast address is 172.23.207.255.

  • Note the subnet

You can find the subnet mask details by using the command below:

ifconfig -a

Review the output for your adapter and examine it carefully.

ImageIP is 172.23.199.129 and subnet mask is 255.255.240.0

Based on the class and subnet mask, the usable host IP range for my network is: 172.23.192.1 - 172.23.207.254.

Subnetting is a broad topic. For more information on subnetting and your usable IP ranges, refer to this article.

Step 3: Make configuration changes

 Netplan is the default network management tool for the latest versions of Ubuntu. Configuration files for Netplan are written in YAML format and have a .yaml extension.

Note: Pay attention to spaces in the configuration file, as they are part of the syntax. Improper indentation will prevent the file from being read correctly.

  • Navigate to the netplan directory located at /etc/netplan.

Run ls in the /etc/netplan directory.

If no files are present, you can create one. The file name can be anything, but conventionally, it should start with a number, like 01-, and end with .yaml. The number determines the priority if you have multiple configuration files.

I’ll create a file named 01-network-manager-all.yaml.

Now, let’s add the following lines to the file. We will build it step by step.

network:
 version: 2

The top-level node in a Netplan configuration file is a network: mapping, which includes version: 2 (indicating that it is using network definition version 2).

Next, we’ll add a renderer, which controls the overall network. By default, the renderer is systemd-networkd, but we’ll change it to NetworkManager.

“At this point, our file appears as follows:”

network:
 version: 2
 renderer: NetworkManager

“Next, we’ll add Ethernet connections and reference the network adapter name we identified in step #2. Other supported device types include modems, Wi-Fi, and bridges.”

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:

“Since we’re configuring a static IP and don’t want to assign an IP dynamically to this network adapter, we’ll set dhcp4 to no.”

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no

Next, we’ll assign the specific static IP address we recorded in step #2, based on our subnet and the available IP range. It was… 172.23.207.254.

Next, we’ll define the gateway, which is the router or network device responsible for assigning IP addresses. For me, it’s located on… 192.168.1.1.

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no
     addresses: [172.23.207.254/20]
     gateway4: 192.168.1.1

Next, we’ll specify the nameservers, which allow you to set a primary and secondary DNS server. In this case, the first value is 8.8.8.8, Google’s primary DNS server, and the second is 8.8.4.4, Google’s secondary DNS server. These values can be adjusted based on your specific needs.

network:
 version: 2
 renderer: NetworkManager
 ethernets:
   eth0:
     dhcp4: no
     addresses: [172.23.207.254/20]
     gateway4: 192.168.1.1
     nameservers:
         addresses: [8.8.8.8,8.8.8.4]

Step 4: Apply and test the changes

We can test the changes before applying them permanently by using the following command:

sudo netplan try

If there are no errors, it will prompt you to confirm whether you want to apply these settings.

Finally, test the changes by running the command ip a, and you should see that the static IP has been successfully applied.

ImageStatic IP applied

How to Set a Static IP Using the GUI

Setting a static IP in Ubuntu through the GUI/desktop is straightforward. Follow these steps:

  • Open the settings menu.
  • Click on either the “Network” or “Wi-Fi” tab, depending on which interface you want to configure.
  • To access the interface settings, click the gear icon next to the interface name.
  • In the IPV4 tab, select “Manual” and input your static IP address, netmask, and gateway.
  • Click “Apply” to save the changes.

ImageManually setting a static IP using Ubuntu Desktop.

  • Verify the configuration by running the command ip a.

ImageStatic IP updated via GUI

Conclusion

In this article, we explored two methods for setting a static IP in Ubuntu. I hope you found the information helpful.

Leave a Comment