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

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.
    $ nmcli d
    Identify Your Network Interface
    Identify Your Network Interface

    This command lists all network interfaces on your system. Note the interface name you plan to configure, such as ‘enp0s3’.

  2. Edit the Netplan Configuration File: Netplan configuration files are stored in /etc/netplan/. You will find this file in the /etc/netplan directory. It might be named 01-netcfg.yaml, 50-cloud-init.yaml, or something similar, depending on your setup. Open or create a file for editing.
    $ sudo nano /etc/netplan/01-netcfg.yaml

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

    network:
      version: 2
      renderer: networkd
      ethernets:
        enp0s3:
          dhcp4: no
          addresses:
            - 192.168.1.10/24
          routes:
            - to: default
              via: 192.168.1.1
          nameservers:
            addresses: [8.8.8.8, 8.8.4.4]
    

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

    Detailed Explanation of the Netplan Configuration File

    The Netplan configuration file is a YAML document used in Ubuntu systems for network configuration. It defines how network interfaces should be configured to connect to the network. Below is a detailed breakdown of each section within the configuration file example provided:

    network: This is the root element of the configuration file, signifying the start of the network configuration.

    version: 2
    This specifies the Netplan configuration format version. Version 2 is currently used for most configurations and supports additional features not available in version 1.




    renderer: networkd
    The renderer indicates which backend Netplan uses to apply the configurations. “networkd” is used for server and headless environments, providing a system daemon to manage network configurations. Another common renderer is NetworkManager, which is more suited for desktop and user-friendly environments.

    ethernets:
    This section specifies that the configuration is for Ethernet interfaces. It’s a top-level key under which individual Ethernet interfaces are defined.

    enp0s3:
    This is the identifier for the specific Ethernet interface being configured. “enp0s3” is a common name for a network interface on virtual machines but could vary depending on your system’s hardware and kernel. Use the command ip link to find out the names of your interfaces.

    dhcp4: no
    This setting disables DHCP for IPv4 on the interface, indicating that a static IP address will be used instead. If set to “yes,” the interface would obtain an IP address automatically from a DHCP server.

    addresses:
    This list specifies the static IP addresses (and optionally, subnet masks) to assign to the interface. “192.168.1.10/24” denotes the static IP address “192.168.1.10” with a subnet mask indicating a “/24” (or 255.255.255.0) network. You can list multiple addresses in this section if needed.

    routes:
    This section is used to define static routes. The “to: default” and “via: 192.168.1.1” lines specify a default route (i.e., where packets should be sent if their destination doesn’t match any other route) through the gateway “192.168.1.1”.

    nameservers:
    The nameservers key specifies the DNS servers to be used by the system. “addresses: [8.8.8.8, 8.8.4.4]” configures Google’s DNS servers as the primary and secondary DNS servers, respectively. This setting is critical for resolving domain names to IP addresses.

    This Netplan configuration example sets a static IP address for a single Ethernet interface, disables DHCP to prevent automatic IP address assignment, specifies a default gateway for outbound traffic, and defines DNS servers for name resolution. It’s a common setup for servers that need a consistent network configuration to ensure reliable network connectivity and services accessibility.

  3. Secure Configuration File Permissions: It’s crucial to ensure that the Netplan configuration file permissions are secure to prevent unauthorized access.
    $ sudo chmod 600 /etc/netplan/01-netcfg.yaml

    This command sets the file permissions so that only the root user can read and write to the file.

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

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

    This command displays all network interfaces and their configuration details. Locate your configured interface (e.g., enp0s3) in the output to verify the new static IP address is correctly assigned.

    Verify the New Static IP Address
    Verify the New Static IP Address

Conclusion

Setting a static IP address on Ubuntu 24.04 involves identifying the correct network interface, editing the 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.