Setting up a static IP address in Ubuntu Linux is a common task for system administrators and advanced users. This guide will explore various ways to configure a static IP address in Ubuntu 22.04 LTS operating system.
Assigning a static IP in Ubuntu and its derivatives can be done using both graphical and command-line tools such as netplan, nmcli, and nmtui etc.
First, we will discuss the netplan method.
Table of Contents
Method 1 : Configure Static IP Address via Netplan Configuration File
Netplan is a utility for easily configuring networking on a Ubuntu Linux system. Introduced in Ubuntu 18.04 and later versions, Netplan has become the default network management tool.
It uses YAML configuration files, offering a declarative approach to network configuration.
In Ubuntu 24.04 LTS, you can set a static IP address via the command line by editing the Netplan configuration files.
Let us see how to assign static IP address via Netplan config file.
1. Identify the Network Interface:
Use the ip a
or ip link
commands to list your network interfaces.
Note the name of the interface you want to configure, e.g., eth0
or ens33
.
As you see in the output above, my network interface name is ens18
.
2. Backup the Current Configuration:
The Netplan configurations are typically located in /etc/netplan/
.
$ ls /etc/netplan/ 01-network-manager-all.yaml
Before making changes, it's a good practice to back up the existing configuration.
$ sudo cp /etc/netplan/01-network-manager-all.yaml /etc/netplan/01-network-manager-all_backup.yaml
3. Edit the Configuration:
Netplan configurations use the YAML format. Open the configuration file using a text editor like nano
:
$ sudo nano /etc/netplan/01-network-manager-all.yaml
4. Configure Static IP:
Modify the file to look something like this (adjust values according to your network):
network: version: 2 renderer: networkd ethernets: ens18: dhcp4: no addresses: - 192.168.1.22/24 gateway4: 192.168.1.101 nameservers: addresses: [8.8.8.8, 8.8.4.4]
Replace the network interface name, IP address, gateway and DNS with your own.
If you want to configure multiple IP addresses, add all IP addresses one by one as shown below.
network: version: 2 renderer: networkd ethernets: ens18: dhcp4: no addresses: - 192.168.1.22/24 - 192.168.1.23/24 - 192.168.1.24/24 routes: - to: default via: 192.168.1.101 nameservers: addresses: [8.8.8.8, 8.8.4.4]
Press CTRL+O
followed by CTRL+X
to save the file and close it.
5. Validate Configuration File:
Please be mindful that Netplan uses YAML (Yet Another Markup Language) for its configuration syntax, and YAML is highly sensitive to whitespace and indentation.
Incorrect indentation can lead to errors or misconfigurations. A common practice is to use spaces (typically 2 or 4) for indentation rather than tabs to maintain consistency.
When editing a Netplan configuration file:
- Be consistent with the number of spaces you use for indentation at each level.
- Avoid using tabs. If you're using an editor like
nano
, you can configure it to replace tabs with spaces for consistency.
Before applying any changes, always validate the configuration using the command:
$ sudo netplan try
This command allows you to test the configuration, and if there's an error, it will revert to the previous working configuration after a timeout.
6. Apply the Configuration:
After saving and validating the configuration file, apply the configuration with command:
$ sudo netplan apply
7. Verify the Changes:
Use ip a
or ip addr show dev ens18
(replace ens18
with your interface name) to verify that the IP address has been set correctly.
8. Restart the Network Service (Optional):
If needed, you can restart the networking service to ensure the changes take effect:
$ sudo systemctl restart systemd-networkd
And that's it! You've set a static IP address for your Ubuntu 22.04 LTS machine via the command line. Remember to adjust the IP address, gateway, and DNS servers to match your network's configuration.
Understanding Netplan Configuration File Precedence and Merging
If there are multiple YAML configuration files under /etc/netplan/
, netplan will read and process all of them. However, the behavior depends on how these configurations are structured:
- Merging Configurations: Netplan merges configurations from all files. If two files configure different aspects of the network (like one configuring one interface and another configuring a different interface), both configurations will take effect simultaneously.
- Conflicting Configurations: If multiple YAML files configure the same interface but with different settings, the configuration from the file that's lexicographically last will take precedence. This is because of how netplan processes the files in the directory. For example, if you have two files,
01-config.yaml
and02-config.yaml
, and both configure theeth0
interface but with different IP addresses, the IP address specified in02-config.yaml
will be the one that's set foreth0
. - Order Matters: As noted above, the order in which files are processed is lexicographic, so naming of files matters. It's often a good practice to name files in a way that reflects their priority, if multiple files might contain conflicting settings.
- Consistent Naming: Although you can have multiple files, it's a good idea to have a consistent naming scheme or structure, especially if there are chances of overlapping configurations. This way, you can control which configurations take precedence by naming them accordingly.
In summary, if there are multiple files with different IP addresses for the same interface, only one IP address (from the lexicographically last file) will be applied to the interface. If you want multiple IP addresses on a single interface, that needs to be specified within a single file's configuration.
While Netplan is powerful, it requires knowledge of YAML. Its strict syntax and indentation can sometimes lead to errors, especially for those unfamiliar with the format.
If you don't prefer the Netplan method for configuring Ubuntu static IP, you can use other command line utilities such as nmcli
or nmtui
as described in the subsequent sections.
Method 2 : Configure Static IP Address using nmcli in Ubuntu
The another common method to set a static IP address on Ubuntu 22.04 LTS is by using the nmcli
tool.
The nmcli
is a command-line client for NetworkManager, which is used by many Linux distributions to manage network connections. It is designed to be user-friendly, providing a straightforward command-line interface to manage NetworkManager.
It's especially handy for those who are not familiar with YAML or prefer not to deal with its strict syntax and indentation rules, as is the case with Netplan.
nmcli
is available in Ubuntu Server editions even if they do not have a GUI. Ubuntu Server often comes with NetworkManager and its command-line interface tool, nmcli
, to manage network configurations.
However, it's worth noting that not all minimal installations or specific server configurations will have NetworkManager or nmcli
installed by default. If it's not present for any reason and you wish to use it, you can install it using the commands below:
$ sudo apt update $ sudo apt install network-manager
After installing, you can use the nmcli
tool to manage your network connections as described below.
Steps to Set a Static IP Address using nmcli
1. List Network Connections:
First, identify the name of the connection you want to modify:
$ nmcli con show
Sample Output:
NAME UUID TYPE DEVICE Wired connection 1 60e8eaf3-89f9-3e9f-9919-1944e7abee20 ethernet ens18
As you can see, my network connection name is Wired connection 1.
2. Modify the Connection:
Use the nmcli con modify
command to set the static IP, gateway, and DNS. Replace YourConnectionName
, YourIPAddress
, YourGateway
, and YourDNS
with the appropriate values:
General Syntax:
nmcli con modify YourConnectionName ipv4.addresses YourIPAddress/24 nmcli con modify YourConnectionName ipv4.gateway YourGateway nmcli con modify YourConnectionName ipv4.dns YourDNS nmcli con modify YourConnectionName ipv4.method manual
For example, to set the IP address to 192.168.1.22
, the gateway to 192.168.1.101
, and use Google's DNS, you'd run:
$ sudo nmcli con modify 'Wired connection 1' ipv4.addresses 192.168.1.22/24 $ sudo nmcli con modify 'Wired connection 1' ipv4.gateway 192.168.1.101 $ sudo nmcli con modify 'Wired connection 1' ipv4.dns "8.8.8.8,8.8.4.4" $ sudo nmcli con modify 'Wired connection 1' ipv4.method manual
You can also set all details in a single command like below.
$ sudo nmcli connection modify 'Wired connection 1' ip4 192.168.1.22/24 gw4 192.168.1.101 ipv4.dns 8.8.8.8
3. Restart the Network Connection:
To apply the changes, you'll need to bring the connection down and then back up:
$ sudo nmcli con down 'Wired connection 1' $ sudo nmcli con up 'Wired connection 1'
4. Verify the Changes:
Use the ip a
command or nmcli con show YourConnectionName
to verify the IP address and settings.
$ nmcli con show 'Wired connection 1'
This will show you long list of details of the given network connection. If you want to filter out the IP address from the output, use the grep command like below:
$ nmcli con show 'Wired connection 1' | grep ipv4.addresses
Similarly, to show the gateway and DNS, run:
$ nmcli con show 'Wired connection 1' | grep ipv4.gateway
$ nmcli con show 'Wired connection 1' | grep ipv4.dns
If you find nmcli
more intuitive and easier to use, it's a great choice for managing network settings on your Ubuntu system, regardless of whether it's a desktop or server edition.
Still, for server environments, many administrators prefer to use Netplan (as described in the method 1) since it provides a more direct way of configuring the system's network settings.
Using nmcli
allows for quick network configurations without worrying about the potential pitfalls of YAML. Plus, it provides immediate feedback if a command is entered incorrectly, whereas Netplan requires you to apply the configuration to see if there are errors.
Method 3 : Set Static IP Address using nmtui in Ubuntu
The nmtui
is a terminal-based user interface tool that allows you to manage network configurations on systems using NetworkManager.
Here are the step-by-step instructions to configure a static IP address in Ubuntu using nmtui
:
1. Install nmtui (if not already installed):
Depending on your Ubuntu installation, nmtui
might not be installed by default. You can install it using the following command:
$ sudo apt update $ sudo apt install network-manager
2. Launch nmtui:
Simply type the command:
$ sudo nmtui
3. Navigate the nmtui menu:
Use the arrow keys to navigate and the Enter key to make selections. Highlight the "Edit a connection" option and press Enter.
From the list of connections, use arrow keys to highlight the connection for which you want to set a static IP, then press TAB key to choose "Edit" and press Enter.
Enter your network settings such as IP address, Gateway, DNS etc.
- Navigate to the "IPv4 CONFIGURATION" section.
- Change the "Automatic" option (which indicates DHCP) to "Manual" using the arrow keys and space bar.
- Below the "Addresses" section, press Enter to add a new address. Here, input the desired static IP and subnet mask. For example:
192.168.1.22/24
(where192.168.1.22
is the IP and24
is the subnet mask). - Set the gateway in the "Gateway" field.
- In the "DNS servers" section, input your desired DNS servers, separated by semicolons (
;
). For example, to use Google's DNS:8.8.8.8;8.8.4.4
.
After filling in all the details, navigate to the "OK" option at the bottom and press Enter.
Finally, navigate to "Back" and then "Quit" to exit nmtui
.
4. Restart NetworkManager:
To apply the changes, you might need to restart the NetworkManager service:
$ sudo systemctl restart NetworkManager
And that's it! You've now configured a static IP address on your Ubuntu machine using nmtui
.
Finally, verify IP address using ip a
command.
Method 4 : Assign a Static IP Address in Ubuntu GNOME Desktop
Assigning a static IP address through the GUI in Ubuntu is straightforward, especially if you're using the default GNOME desktop environment.
1. Open Settings:
Click on the system menu at the top right corner of your screen (where the battery, network, and audio settings are). Choose "Settings" from the dropdown.
2. Go to Network Settings:
In the settings window, on the left sidebar, click on "Network."
3. Select the Network Interface:
You will see a list of available network interfaces like "Wired" or "Wireless."
Click on the gear icon (⚙️) next to the network connection that you want to configure.
4. Configure the IP Settings:
- Go to the "IPv4" tab.
- Change the "IPv4 Method" from "Automatic (DHCP)" to "Manual."
- Under the "Addresses" section, click on the "Add" button.
- Now, enter the desired static IP address in the "Address" field.
- Enter the appropriate netmask (for most home networks, this will be
255.255.255.0
). - Enter the gateway (often the IP address of your router, e.g.,
192.168.1.1
01). - For DNS, you can use the DNS servers provided by your ISP, or use public DNS servers like Google's
8.8.8.8
and8.8.4.4
. - If needed, you can similarly adjust the IPv6 settings in the "IPv6" tab.
5. Apply Changes:
After configuring the details, click on the "Apply" button at the top right corner. Close the settings window.
6. Reconnect to the Network:
It might be necessary to disconnect and reconnect to the network for changes to take effect. If it's a wired connection, unplug and plug it back in. For a wireless connection, turn off the Wi-Fi and turn it back on.
That's it. You've now assigned a static IP address to your Ubuntu machine using the graphical user interface.
Remember that when you set a static IP, ensure it's not within the DHCP range of your router to avoid IP conflicts. If unsure about the DHCP range, you can check your router's settings or documentation.
Related Read:
- How To Configure Static And Dynamic IP Address In Arch Linux
- How To Assign Multiple IP Addresses To A Single Network Interface Card In Linux
- Configuring Network Bonding In Linux For High Availability
Conclusion
This detailed tutorial has described four different methods for configuring a static IP address in Ubuntu 22.04 LTS. The best method for you will depend on your specific needs and preferences.
The Netplan configuration file method is the default way to configure all aspects of your network interface, including the IP address, subnet mask, gateway, and DNS servers in Ubuntu.
The nmcli method is a good option if you want to quickly configure a static IP address without having to edit any configuration files.
The nmtui method is a good option if you want a graphical user interface for configuring your network interface.
And lastly the graphical user interface method is the simplest option, and best suitable for newbies.
Ubuntu offers flexible network configuration options, allowing users to choose the method that best suits their needs. Whether you prefer the declarative approach of Netplan, the simplicity of nmcli or nmtui, or the graphical user interface, Ubuntu has you covered.