How to Assign a Static IP Address Using Netplan

optimize disk IO linux cpynet

Assigning a static IP address in Ubuntu is a straightforward process, especially with the use of Netplan. Netplan is a modern tool used for network configuration management in Ubuntu. In this guide, we will walk through how to configure a static IP address with Netplan in Ubuntu.

What is Netplan?

Netplan is a network configuration tool used in Ubuntu for managing network settings. It utilizes YAML files to define network configurations in a clear and manageable format. Netplan reads these configuration files and applies the settings using various network rendering methods, such as NetworkManager or systemd-networkd. The beauty of Netplan lies in its ability to simplify network configuration and make it more readable and easier to maintain.

In the past, network configuration in Ubuntu required manual edits to multiple files. Netplan brings all of that under one umbrella, making it more structured and user-friendly.

Steps to Assign a Static IP Address with Netplan

To assign a static IP address using Netplan, you need to edit the appropriate configuration file. These files are typically found in the /etc/netplan/ directory and have a .yaml extension. The default filename is usually something like 00-installer-config.yaml, but it may vary depending on the system.

Follow these steps to configure a static IP address:

  1. Open the Terminal and Get Root Access:
    First, open your terminal and gain root privileges to make changes to the configuration files.
   sudo -i
  1. Edit the Netplan Configuration File:
    Now, you need to edit the Netplan configuration file. You can use any text editor, but we will use nano for this example. If the default configuration file exists, open it as follows:
   sudo nano /etc/netplan/00-installer-config.yaml

If the file doesn’t exist, you may need to create it or check for files in the directory.

  1. Edit the Configuration File to Assign the Static IP Address:
    Once the file is open, you need to specify your network interface and static IP settings. Below is an example configuration:
   network:
     version: 2
     renderer: NetworkManager
     ethernets:
       enp1s0:
         addresses:
           - 172.16.1.61/24
         nameservers:
           addresses: [8.8.8.8, 8.8.4.4]
         routes:
           - to: default
             via: 172.16.1.1

Explanation:

  • version: 2: Specifies the version of Netplan configuration.
  • renderer: NetworkManager: Tells Netplan to use NetworkManager for managing the network.
  • ethernets: The section where you define the Ethernet interface, in this case, enp1s0.
  • addresses: Specifies the static IP address (e.g., 172.16.1.61/24), where /24 denotes the subnet mask.
  • nameservers: Specifies the DNS servers to use, in this case, Google’s DNS servers (8.8.8.8 and 8.8.4.4).
  • routes: Defines the default gateway, which is the IP address that packets will be routed to if they are not for the local network (in this case, 172.16.1.1).
  1. Save and Close the File:
    After making the necessary changes, save and close the file. In nano, you can do this by pressing CTRL + X, then Y to confirm changes, and finally Enter to save the file.
  2. Apply the Netplan Configuration:
    To apply the new network configuration, run the following command:
   sudo netplan apply

This command applies the new network settings, including the static IP address you specified.

Testing the Configuration

Once the configuration is applied, you can check if the static IP address has been successfully assigned by using the following command:

ip a

This command will display a list of network interfaces and their assigned IP addresses. You should see the static IP address you assigned (e.g., 172.16.1.61) listed under the appropriate network interface.

Additional Tests

To ensure the network is functioning correctly, you can perform a few additional tests:

  • Ping the Gateway:
    To verify that the network gateway is reachable, run:
  ping 172.16.1.1

If you receive responses from the gateway, it means the route is correctly set.

  • Check DNS Resolution:
    You can verify that DNS is working properly by running:
  nslookup google.com

This will query the DNS server to ensure that domain names are being resolved correctly.

Conclusion

Netplan is an excellent tool for managing network configurations in Ubuntu. Assigning a static IP address with Netplan is straightforward and involves editing a single YAML file. This process helps maintain clear and easy-to-manage network configurations, especially for systems that require consistent network settings.

By following the steps in this guide, you should be able to configure a static IP address on your Ubuntu system quickly and effectively.

Previous Article

Monitoring Linux Server Performance: Top, du, and netstat Explained

Next Article

Limit Memory and CPU Usage in Docker

Subscribe to our Newsletter! 📬

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨