In this article, we will discuss how to set a static IP addresses on Ubuntu through CLI and GUI, so keep reading to know everything you need about it.
IP Configurations in Linux
In most Linux systems, Dynamic Host Configuration Protocol (DHCP) servers assign the IP addresses. These IP addresses are dynamic. To elaborate, these IP addresses may change when the system is restarted. Dynamic IP addresses are not an issue unless a private network runs between computers.
For instance, the dynamic IP address would cause issues for the wireless keyboard you share with Ubuntu and Raspberry Pi. Furthermore, dynamic IP addresses would also cause issues with remote servers and client machines. In this case, the static IP address provides stability and consistency between the machines.
How to Set a Static IP Address on Ubuntu
There are multiple ways to set a static IP address on Ubuntu. Read this guide to explore different methods. Ubuntu uses the NetworkManager daemon to manage network configuration. Here are the network configurations for this tutorial:
- IP Address: 192.168.17.129
- Netmask: 255.255.255.0
- Default gateway route address: 192.168.17.2
- DNS nameserver addresses: 8.8.8.8, 192.168.17.2
Set a Static IP Address on Ubuntu Using CLI
First, open the Terminal by pressing “Ctrl + Alt + T”. Next, get the name of the network interface and default gateway. For this step, use the nmcli
command. The nmcli is a command-line utility that displays configurations related to the network.
Typical information includes viewing networking status and changing its states. To get more information on nmcli
, type:
nmcli connection show
Output:
Once done, go to the Terminal and type:
nmcli d
You should get a similar output:
With this output, you can determine the network under the DEVICE section. After that, look for the default gateway IP address by using the ip route
command. Specifically, type:
ip route
The output should look something like this:
As you can see from the output above, the default gateway, in our case, is 192.168.17.2.
Set a Static IP Address on Ubuntu Using the Netplan Utility
Now that we know the default gateway, let’s look for the netplan configuration. The default location for netplan .yaml files is in the /etc/netpla
n directory. Create a duplicate of this file before making any changes. After that, open the file present in the netplan directory by using your favorite text editor. For example, type:
If you’re using vim editor, type:
sudo vim /etc/netplan/01-netcfg.yaml
If you’re using nano editor, type:
sudo nano /etc/netplan/01-netcfg.yaml
After that, edit the file and your network configurations like this:
network:
version: 2
renderer: networkd
ethernets:
enp0s25:
dhcp4: no
addresses:
- 192.168.17.129/24
gateway4: 192.168.17.2
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
In the previous file, I have set the static IP to the default gateway IP address. Be extra careful about the spaces, as the .yaml file uses spaces for indentation.
Save the file and exit the editor. Lastly, use the netplan to apply the command to apply the changes. Type:
sudo netplan apply
And with that, you have set the static IP address successfully! To verify the static IP address, execute:
ip route
#or
if config
To confirm the DNS servers, run the command:
systemd-resolve --status
Set a Static IP Address on Ubuntu Using nmcli Command
Alternatively, you can use the nmcli utility to set the static IP address. For this command, first, display the active and inactive interfaces:
nmcli dev status
Output:
Next, identify the IP address using the ip addr
command. Type:
ip addr
Now set up the IP address that you picked from the ip addr
command (written beside the inet section). Execute the command given below:
nmcli con mod ens33 ipv4.addresses 192.168.17.129/24
After that, set up the default gateway as shown below:
nmcli con mod ens33 ipv4.gateway 192.168.17.2
Also, do not forget to set up the DNS server like this:
nmcli con mod ens33 ipv4.dns “8.8.8.8”
Lastly, change the addressing from the DHCP to static:
nmcli con mod ens33 ipv4.method manual
To save the configurations, type this command and hit the “Enter” key.
nmcli con up ens33
The changes will be reflected in the /etc/sysconfig/network-scripts/ifcfg-enps03
. To verify, use the ip addr
command. Specifically, type:
ip addr ens33
On the other hand, you can also view the ifcfg-ens33
using the cat
or the less
command. For example:
cat /etc/sysconfig/network-scripts/ifcfg-ens03
less /etc/sysconfig/network-scripts/ifcfg-ens03
Set a Static IP Address on Ubuntu Using GLI
You can change the IP address from the GNOME desktop as well. Plus, this option is much easier for novice Linux users.
In the Activities screen, search for ‘Settings’.
Go to Settings and locate network settings. This looks like a cog/gear symbol.
Next, click the IPv4 tab to open further settings. Click Manual and type the IP address you want in the static IP section. Also, enter the netmask (which is 24 by default) and the default IP gateway.
Once you are done, click ‘Apply’.
And that’s a wrap! In this article, we have discussed two command-line utilities that you can use to set a static IP Address on Ubuntu. We also looked at how you can achieve the same using the GLI. We hope the guide was useful to you.
If this guide helped you, please share it.