Setting a Static IP Address in Ubuntu 24.04 via the Command Line

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
Setting a Static IP Address in Ubuntu 24.04 via the Command Line
Setting a Static IP Address in Ubuntu 24.04 via the Command Line
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

YouTube video

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.

  1. 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.

    Ubuntu terminal showing ip a command output displaying ens18 network interface with IP address 172.16.1.131/24
    Using the ip a command to identify the ens18 network interface and its current dynamic IP address before configuring a static IP
  2. 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.

    Ubuntu terminal listing Netplan configuration files showing 01-network-manager-all.yaml and 50-cloud-init.yaml in /etc/netplan directory
    Listing Netplan configuration files with ls -la /etc/netplan/ command, showing both 01-network-manager-all.yaml and 50-cloud-init.yaml files

    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.

    Terminal showing contents of 50-cloud-init.yaml file with ens18 interface configured for DHCP with dhcp4: true
    Output of sudo cat /etc/netplan/50-cloud-init.yaml showing the ens18 interface with DHCP enabled before static IP configuration

    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.

    Terminal output showing nmcli device show command confirming ens18 interface is managed by netplan-ens18 connection
    Using nmcli device show ens18 command to verify that the interface is managed by Netplan, showing GENERAL.CONNECTION: netplan-ens18
  3. Edit the Netplan Configuration File: Once you’ve identified the correct file, open it for editing. In this example, we’ll edit 50-cloud-init.yaml:
    $ sudo nano /etc/netplan/50-cloud-init.yaml

    Here’s an example configuration to set a static IP address:

    network:
      version: 2
      ethernets:
        ens18:
          dhcp4: false
          addresses:
            - 172.16.1.240/24
          routes:
            - to: default
              via: 172.16.1.1
          nameservers:
            addresses: [8.8.8.8, 8.8.4.4]
    

    Replace ens18 with your interface name. Adjust the IP address, subnet mask, default gateway, and DNS servers as necessary for your network.

    ALT: "Terminal displaying edited 50-cloud-init.yaml file with static IP configuration showing dhcp4: false, address 172.16.1.240/24, gateway 172.16.1.1, and DNS servers
    Configured 50-cloud-init.yaml file with static IP address 172.16.1.240/24, default gateway 172.16.1.1, and Google DNS servers

    MULTIPLE IP ADDRESSES EXAMPLE
    To configure multiple IP addresses on the same interface, simply add them to the addresses list:

    network:
      version: 2
      ethernets:
        ens18:
          dhcp4: false
          addresses:
            - 172.16.1.240/24
            - 172.16.1.241/24
            - 172.16.1.242/24
          routes:
            - to: default
              via: 172.16.1.1
          nameservers:
            addresses: [8.8.8.8, 8.8.4.4]

    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.

  4. 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.

  5. 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.

  6. 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.

    Terminal showing complete static IP configuration process: chmod 600, netplan apply, ip a verification showing 172.16.1.240/24, and successful ping to gateway 172.16.1.1
    Complete workflow showing file permissions secured with chmod 600, configuration applied with netplan apply, static IP 172.16.1.240 verified, and successful gateway connectivity test

Understanding Multiple Netplan Files

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.