How to Update the IP Address on Ubuntu Using Netplan (With Persistent Configuration)

Updating the IP address on an Ubuntu system using Netplan might sound tricky, but don’t worry! This guide will walk you through the process step by step, ensuring that your changes are permanent—even if cloud-init is being used.

What is Netplan?

Netplan is a tool used on Ubuntu to configure networking. It’s a simple and modern way to manage your network settings through YAML configuration files. Netplan applies these settings to either NetworkManager or systemd-networkd depending on your setup.

Why Use Netplan for IP Configuration?

Using Netplan to set a static IP address is a great choice because it’s simple, straightforward, and ensures that your network settings are well-documented and easy to manage.

Step 1: Identify Your Network Interface

First, you need to know which network interface you want to configure. A network interface is a physical or virtual device on your computer that connects to a network. To find your network interface name, open a terminal and run:

ip a

Look for an interface name like eth0, enp0s3, or ens33. It usually starts with "eth" or "enp."

Step 2: Create a Custom Netplan Configuration File

Instead of modifying the default Netplan configuration file (like 50-cloud-init.yaml), we’ll create a new custom file. This approach ensures that your changes won’t be overwritten by cloud-init or other processes.

  1. Open a terminal.
  2. Create a new Netplan configuration file in the /etc/netplan/ directory. You can name it something like 01-netcfg.yaml:
sudo vi /etc/netplan/01-netcfg.yaml

Step 3: Add Your Custom Network Configuration

Now, you’ll add your desired IP address, gateway, and DNS settings to the new configuration file. Here’s an example of what it might look like:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      addresses:
        - 192.168.0.150/24
      routes:
        - to: default
          via: 192.168.0.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 1.1.1.1

Explanation:

  • dhcp4: no: This line disables DHCP, which dynamically assigns IP addresses. Instead, you’ll set a static IP.
  • addresses:: This section defines the IP address you want to use. Replace 192.168.0.150/24 with your desired IP address.
  • routes:: This section sets the default route (gateway). to: default indicates the default route, and via: 192.168.0.1 sets the gateway address.
  • nameservers:: This section lists DNS servers, which translate domain names (like www.google.com) into IP addresses.

Step 4: Apply the Configuration

After editing the configuration file, save and exit the text editor. To apply your changes, run:

sudo netplan apply

This command will apply the configuration and restart the network service with your new settings.

Step 5: Verify Your IP Address

To check that your new IP address is active, run:

ip a

This command will show you the current IP address assigned to your network interface.

Ensuring Persistence with cloud-init

If you’re using cloud-init, which is common on cloud-based Ubuntu installations, it might overwrite your network settings. To prevent this, you can disable cloud-init’s network configuration:

  1. Create a new configuration file to disable cloud-init’s network management:
    sudo vi /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
    
  2. Add the following content to disable cloud-init’s network configuration:
    network:
    config: disabled
    
  3. Save and exit the file, then clean cloud-init data:
    sudo cloud-init clean
    sudo reboot
    

After rebooting, your custom Netplan configuration will be used, and cloud-init will no longer interfere with your settings.

Troubleshooting Tips

  • Test Before Applying: Use sudo netplan try to test the configuration before fully applying it. If there’s an error, you’ll have a chance to fix it before it’s applied permanently.
  • Backup Your Configuration: Always make a backup of your existing configuration before making changes, especially in production environments.

Updating the IP address on Ubuntu using Netplan is simple once you know the steps. By creating a custom Netplan configuration file and optionally disabling cloud-init’s network management, you ensure that your settings persist across reboots and system updates. Whether you’re configuring a server or a desktop, these steps will help you manage your network settings with confidence.

Now that you have your network configured properly, you can enjoy a stable and reliable connection with your new static IP address!