Easy Steps To Set Static IP Address on Ubuntu 22.04 From CLI
This tutorial intends to teach you to Set 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 Static IP Address on Ubuntu 22.04 From CLI.
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 use static IP addresses.
Now follow the steps below on the Orcacore website to see how to Set Static IP Address on Ubuntu 22.04 From CLI by using the Linux Commands.
To Set Static IP Address on Ubuntu 22.04 From CLI, 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.
Follow the steps below to learn how to Set Static IP Address on Ubuntu 22.04 From CLI.
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:
**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 called Netplan to Set Static IP Address on Ubuntu 22.04 From CLI. 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
[Screenshot of ip a output]
Then, you must create a new file in the /etc/netplan. Here we named it 01-netcfg.yaml and created it 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. You have learned to Set Static IP Address on Ubuntu 22.04 From CLI.
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 also interested in these articles:
Update Timezone DB in Linux
10 Useful ncat Commands on AlmaLinux
Install the Latest Zabbix on Ubuntu 24.04
Installing Apache Guacamole on Ubuntu 24.04
Alternative Solutions for Setting a Static IP Address on Ubuntu 22.04
While the article effectively demonstrates how to Set Static IP Address on Ubuntu 22.04 From CLI using nmcli
and Netplan, there are other approaches worth considering, especially when dealing with specific network configurations or legacy systems. Here are two alternative methods:
1. Editing the /etc/network/interfaces
File (Legacy Method – Less Recommended for Ubuntu 22.04)
Historically, the /etc/network/interfaces
file was the primary method for configuring network interfaces on Debian-based systems like Ubuntu. While Netplan is now the default, understanding this method can be helpful for troubleshooting older configurations or interacting with systems that haven’t fully migrated. Note that this method might conflict with Netplan, so you might need to disable Netplan first.
Explanation:
This method involves directly editing the /etc/network/interfaces
file to define the static IP address, netmask, gateway, and DNS servers. This file is read by the ifup
and ifdown
commands, which are used to bring network interfaces up or down.
Steps:
-
Identify the Interface: Use
ip a
to determine the name of your network interface (e.g.,eth0
,enp0s3
). -
Edit the
/etc/network/interfaces
file: Open the file with a text editor likenano
orvim
usingsudo
:sudo nano /etc/network/interfaces
-
Add the configuration: Add the following lines to the file, replacing the placeholders with your actual values:
auto <interface_name> iface <interface_name> inet static address <static_ip_address> netmask <netmask> gateway <gateway_ip_address> dns-nameservers <dns_server_1> <dns_server_2>
Example:
auto enp0s3 iface enp0s3 inet static address 192.168.1.90 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8 8.8.4.4
-
Restart the interface: Bring the interface down and then up again to apply the changes:
sudo ifdown <interface_name> sudo ifup <interface_name>
Or, restart the networking service:
sudo systemctl restart networking
Caveats:
- This method is generally discouraged on modern Ubuntu systems that use Netplan. Conflicts can arise if both Netplan and
/etc/network/interfaces
attempt to manage the same interface. - Ensure Netplan is disabled if you choose to use this method. You can disable Netplan by removing or commenting out the configuration files in
/etc/netplan/
. - Double-check your syntax and IP address information to avoid network connectivity issues.
2. Using DHCP Reservation on the Router
Another approach to achieving a static IP address is to configure a DHCP reservation on your router. This method doesn’t directly configure the IP address on the Ubuntu server itself but instructs the router to always assign the same IP address to the server based on its MAC address.
Explanation:
Most modern routers have a DHCP server that dynamically assigns IP addresses to devices on the network. DHCP reservations allow you to create a persistent mapping between a device’s MAC address and a specific IP address. When the Ubuntu server requests an IP address from the router’s DHCP server, the router will recognize its MAC address and assign the reserved IP address.
Steps:
-
Find the Server’s MAC Address: Use the
ip link
command on your Ubuntu server to find the MAC address of your network interface. Look for thelink/ether
field in the output.ip link
Example Output:
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:b2:05:1a brd ff:ff:ff:ff:ff:ff ...
In this example, the MAC address is
08:00:27:b2:05:1a
. -
Access Your Router’s Configuration: Open a web browser and enter your router’s IP address in the address bar. This is typically
192.168.1.1
or192.168.0.1
, but it may vary depending on your router manufacturer. Consult your router’s documentation if you’re unsure. -
Log In to Your Router: Enter your router’s username and password. These are often printed on a sticker on the router itself.
-
Find the DHCP Reservation Settings: The location of these settings varies depending on the router’s manufacturer and model. Look for sections like "DHCP Server," "Static DHCP," "Address Reservation," or similar terms. Consult your router’s documentation for specific instructions.
-
Add a New Reservation: Create a new reservation, entering the following information:
- MAC Address: The MAC address you found in step 1.
- IP Address: The static IP address you want to assign to the server. Choose an IP address within your network’s subnet but outside the DHCP address pool (the range of IP addresses that the router dynamically assigns). For example, if your DHCP pool is
192.168.1.100
to192.168.1.200
, you could choose192.168.1.99
. - Hostname (Optional): A descriptive name for the server (e.g., "UbuntuServer").
-
Save the Reservation: Save the DHCP reservation settings.
-
Restart the Server or Renew the DHCP Lease: Restart your Ubuntu server or release and renew its DHCP lease. To renew the DHCP lease, you can use the following commands:
sudo dhclient -r <interface_name> # Release the current lease sudo dhclient <interface_name> # Request a new lease
Advantages:
- Centralized management: The IP address configuration is managed centrally on the router, making it easier to manage IP addresses for multiple devices.
- No configuration changes on the server: The Ubuntu server doesn’t require any manual configuration changes.
- Avoids conflicts: Reduces the risk of IP address conflicts, as the router ensures that the reserved IP address is always assigned to the correct device.
Disadvantages:
- Router-dependent: The process for configuring DHCP reservations varies depending on the router manufacturer and model.
- Requires router access: You need administrative access to your router’s configuration.
By understanding these alternative methods, you can choose the most appropriate approach for Set Static IP Address on Ubuntu 22.04 From CLI based on your specific needs and network environment. Remember to always document your changes and test your configuration thoroughly to ensure network connectivity.