In most network configurations, the router DHCP server assigns the IP address dynamically by default. If you want to ensure that your system IP stays the same every time, you can force it to use a static IP.
That's what we will learn in this article. We will explore two ways to set a static IP in Ubuntu.
Static IP addresses find their use in the following situations:
- Configuring port forwarding.
- Configuring your system as a server such as an FTP server, web server, or a media server.
To follow this tutorial you will need the following:
- Ubuntu installation, preferably with a GUI.
sudorights as we will be modifying system configuration files.
How to Set a Static IP Using the Command Line
In this section, we will explore all the steps in detail needed to configure a static IP.
Step 1: Launch the terminal
You can launch the terminal using the shortcut
Step 2: Note information about the current network
We will need our current network details such as the current assigned IP, subnet mask, and the network adapter name so that we can apply the necessary changes in the configurations.
Use the command below to find details of the available adapters and the respective IP information.
The output will look something like this:
For my network, the current adapter is
eth0. It could be different for your system
- Note the current network adapter name
As my current adapter is
eth0, the below 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
- Note the subnet
We can find the subnet mask details using the command below:
Select the output against your adapter and read it carefully.
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 vast topic. For more info on subnetting and your usable IP ranges, check out this article.
Step 3: Make configuration changes
Netplan is the default network management tool for the latest Ubuntu versions. Configuration files for Netplan are written using YAML and end with the extension
Note: Be careful about spaces in the configuration file as they are part of the syntax. Without proper indentation, the file won't be read properly.
- Go to the
netplandirectory located at
ls into the
If you do not see any files, you can create one. The name could be anything, but by convention, it should start with a number like
01- and end with
.yaml. The number sets the priority if you have more than one configuration file.
I'll create a file named
Let's add these lines to the file. We'll build the file step by step.
The top-level node in a Netplan configuration file is a
network: mapping that contains
version: 2 (means that it is using network definition version 2).
Next, we'll add a renderer, that controls the overall network. The renderer is
systemd-networkd by default, but we'll set it to
Now, our file looks like this:
Next, we'll add
ethernets and refer to the network adapter name we looked for earlier in step#2. Other device types supported are
As we are setting a static IP and we do not want to dynamically assign an IP to this network adapter, we'll set
Now we'll specify the specific static IP we noted in step #2 depending on our subnet and the usable IP range. It was
Next, we'll specify the gateway, which is the router or network device that assigns the IP addresses. Mine is on
Next, we'll define
nameservers. This is where you define a DNS server or a second DNS server. Here the first value is
126.96.36.199 which is Google's primary DNS server and the second value is
188.8.131.52 which is Google's secondary DNS server. These values can vary depending on your requirements.
Step 4: Apply and test the changes
We can test the changes first before permanently applying them using this command:
sudo netplan try
If there are no errors, it will ask if you want to apply these settings.
Now, finally, test the changes with the command
ip a and you'll see that the static IP has been applied.
How to Set a Static IP Using the GUI
It is very easy to set a static IP through the Ubuntu GUI/ Desktop. Here are the steps:
- Search for
- Click on either Network or Wi-Fi tab, depending on the interface you would like to modify.
- To open the interface settings, click on the gear icon next to the interface name.
- Select “Manual” in the IPV4 tab and enter your static IP address, Netmask and Gateway.
- Click on the
- Verify by using the command
In this article, we covered two methods to set the static IP in Ubuntu. I hope you found the article useful.
What’s your favorite thing you learned from this tutorial? Let me know on Twitter!
You can read my other posts here.