Configuring a static IP address on your Ubuntu server is essential for various reasons, such as ensuring consistent network configuration, facilitating server management, and improving network security. Ubuntu 24.04, like its predecessors, uses the Netplan utility for network configuration, which simplifies the process of setting a static IP address through YAML configuration files.
In this tutorial you will learn:
How to identify your network interface using the command line
How to identify which Netplan configuration file is active
How to edit the Netplan configuration file to set a static IP address
How to apply the configuration changes
How to ensure your configuration file permissions are secure
Software Requirements and Linux Command Line Conventions
Category
Requirements, Conventions or Software Version Used
System
Ubuntu 24.04 LTS
Software
Netplan network configuration tool
Other
Root or sudo privileges required
Conventions
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command $ – requires given linux commands to be executed as a regular non-privileged user
Video Tutorial
Configuring a Static IP Address
Before diving into the configuration steps, ensure you have sudo or root access to the Ubuntu system. This access is necessary for editing network configuration files and applying changes.
Identify Your Network Interface: To set a static IP address, you first need to know which network interface you’re configuring.
$ ip a
This command lists all network interfaces on your system. Note the interface name you plan to configure, such as ens18 or enp0s3.
Identify the Active Netplan Configuration File: Netplan configuration files are stored in /etc/netplan/. Multiple files may exist, and it’s important to identify which one controls your network interface.
$ ls -la /etc/netplan/
You may see files like 01-network-manager-all.yaml or 50-cloud-init.yaml.
UNDERSTANDING NETPLAN FILES
The presence of 01-network-manager-all.yaml indicates a GUI/desktop installation where NetworkManager provides graphical network management in Settings. Server installations typically only contain 50-cloud-init.yaml or similar numbered files for direct interface configuration.
Netplan processes these files in numerical order (01, 50, 99, etc.). To identify which file configures your interface, check the contents:
$ sudo cat /etc/netplan/50-cloud-init.yaml
Look for your interface name (e.g., ens18) in the configuration. The file containing your interface configuration is the one you’ll edit.
MULTIPLE IP ADDRESSES
If you need multiple IP addresses on a single interface, add them within the same file’s addresses list rather than creating multiple files. Multiple files configuring the same interface will cause Netplan to merge configurations, which can lead to unexpected results.
You can also verify which connection manages your interface:
$ nmcli device show ens18 | grep -i "GENERAL.CONNECTION"
If it shows netplan-[interface-name], your interface is managed by Netplan configuration files.
Detailed Explanation of the Netplan Configuration File
The Netplan configuration file is a YAML document used in Ubuntu systems for network configuration. Below is a detailed breakdown of each section:
Configuration Key
Description
network:
Root element signifying the start of network configuration.
version: 2
Specifies the Netplan configuration format version. Version 2 is currently used and supports additional features not available in version 1.
renderer: networkd
Indicates which backend Netplan uses. “networkd” is for server environments, “NetworkManager” is for desktop environments. If not specified, uses the default renderer from lower-numbered files.
ethernets:
Section for Ethernet interface configurations.
ens18:
Interface identifier. Use ip a to find your interface name (e.g., ens18, enp0s3, eth0).
dhcp4: false
Disables DHCP for IPv4. Must be false (not no) for static IP. Set to true for automatic IP assignment.
addresses:
List of static IP addresses with subnet masks (e.g., 172.16.1.240/24). Multiple addresses can be listed.
routes:
Defines static routes. “to: default” with “via: 172.16.1.1” sets the default gateway for outbound traffic.
nameservers:
DNS servers for name resolution. Example uses Google DNS (8.8.8.8, 8.8.4.4).
This configuration sets a static IP, disables DHCP, specifies a default gateway, and defines DNS servers for consistent network connectivity.
Secure Configuration File Permissions: It’s crucial to ensure that all Netplan configuration file permissions are secure to prevent unauthorized access.
$ sudo chmod 600 /etc/netplan/*.yaml
This command sets the file permissions so that only the root user can read and write to the files. Netplan will warn you if permissions are too open.
Apply the Configuration Changes: Once you’ve edited the configuration file, apply the changes to update your network settings.
$ sudo netplan apply
This command activates the new network configuration. If there are no errors, your system will start using the static IP address you’ve configured.
Verify the New Static IP Address: To confirm that the static IP address has been successfully assigned to your network interface, use the ip a command.
$ ip a show ens18
This command displays your specific network interface and its configuration details. Verify the new static IP address is correctly assigned.
You can also test network connectivity to your gateway:
$ ping -c 3 172.16.1.1
Replace the IP address with your actual gateway address.
Ubuntu systems may have multiple Netplan configuration files in /etc/netplan/. Understanding how these files interact is important:
File Processing Order: Netplan processes configuration files in alphanumeric order (01, 50, 99, etc.).
Desktop vs Server: Files like 01-network-manager-all.yaml indicate a GUI/desktop installation and set NetworkManager as the default renderer for graphical network management. Server installations typically only have numbered files like 50-cloud-init.yaml.
Configuration Merging: When multiple files configure the same interface, Netplan merges the configurations rather than overriding them. This can lead to unexpected results such as multiple IP addresses being assigned when only one was intended.
Best Practice: Configure each network interface in only one Netplan file. If you need multiple IP addresses on a single interface, add them within one file’s addresses list.
Conclusion
Setting a static IP address on Ubuntu 24.04 involves identifying the correct network interface, locating and editing the appropriate Netplan configuration file with the new IP settings, securing the file permissions, and applying the changes. This process ensures your server can communicate consistently and securely on your network. Remember to always backup configuration files before making changes to prevent any unintended network disruptions.