This tutorial intends to teach you to Set a Static IP Address on Ubuntu 22.04 From CLI.  A static IP address is an IP address that doesn’t change over time. Let’s see why we should set a static IP address.
What Is the Purpose of a Static IP Address?
Because of insufficient IP addresses, some service providers may assign the same address to two different users. For this reason, connection problems can happen. Instead of this, you can see the static IP addresses.
Now follow the steps below to see how to configure a static IP address on Ubuntu 22 server from the command line interface by using the Linux Commands.
How To Set Static IP Address on Ubuntu 22.04 From CLI?
To complete this guide, you must have access to your server as a non-root user with sudo privileges. To do this, you can check this guide on Initial Server Setup with Ubuntu 22.04.
Step 1 – Configure a Static IP on Ubuntu 22 with nmcli Command
The nmcli is a command line tool that can be used to create, edit, activate/deactivate, and delete network connections. So we want to use this command line utility to set a static IP address on our Ubuntu server.
To use this tool, you must install Network Manager on your server. To do this, run the command below:
sudo apt install network-manager -y
Then, you can use the nmcli command to get your connection information:
sudo nmcli connection show
In your output, you will get the following connection information:
- NAME
- UUID
- TYPE
- DEVICE
Example Output
NAME UUID TYPE DEVICE
Wired connection 1 12f312a7-f0e7-334a-8ab4-c7b3f8249c7e ethernet enp0s3
Next, you should create a static link. You need to manually configure the device and ipv4 settings with the appropriate parameters in the nmcli command:
sudo nmcli con add type ethernet con-name 'static' ifname enp0s3 ipv4.method manual ipv4.addresses 192.168.1.89/24 gw4 192.168.1.1
When you are done, recheck your connection information and you should see the static link has been added:
sudo nmcli connection show
Example Output
NAME UUID TYPE DEVICE
Wired connection 1 12f312a7-f0e7-334a-8ab4-c7b3f8249c7e ethernet enp0s3
static ... ethernet --
At this point, you need to add the static connection you have created to the DNS IP with the command below:
sudo nmcli con mod static ipv4.dns 192.168.*.*
Then, you must activate the static connection on Ubuntu with the following command:
sudo nmcli con up id 'static'
In your output, you should see the “connection successfully activated,” message.
Also, you can use the following IP command to verify your static IP address:
ip route
That’s it, you have set a static IP address on your server.
Step 2 – Configure a Static IP on Ubuntu 22 with Netplan
At this step, you can use an alternative command to set your static IP address called Netplan. To do this, you just need to follow the steps below.
First, use the command below to find your desired network interface:
sudo ip a
Example Output
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
...
Then, you must create a new file in the /etc/netplan. Here we named it 01-netcfg.yaml and create t by using the vi editor:
sudo vi /etc/netplan/01-netcfg.yaml
Add the following content with your settings to the file:
network:
version: 2
renderer: networkd
ethernets:
eth0: #Edit this line with your network interface name.
dhcp4: no
addresses:
- 192.168.1.10/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
Here we disabled the DHCP and set DNS settings with Google.
When you are done, save and close the file.
Finally, you must run the command below to apply your configuration:
sudo netplan apply
That’s it, you are done. For more information, you can visit Ubuntu – Configure Networks page.
Conclusion
At this point, you have learned why it is good to set a static IP and configure it from CLI with Linux Commands such as Netplan and NMCLI on Ubuntu 22.04.
Hope you enjoy it. You may be interested in these articles: