DEV Community

Cover image for Configure static IP address on Netplan
Sergio Peris
Sergio Peris

Posted on • Originally published at sertxu.dev

Configure static IP address on Netplan

I've recently set up a new Ubuntu Server 24.04 and needed to assign it a static IP address. For a server, you don't want the IP to change whenever DHCP leases expire.

Finding your network interface

Before making any configuration changes, you need to know the name of your network interface. On Ubuntu Server, you'll likely see something like ens33 or enp0s3.

Run this command to see all network interfaces:

ip link
Enter fullscreen mode Exit fullscreen mode

Look for the interface that has an IP address assigned to it. That's the one you want to configure. In my case, it was enp0s3.

Configuring Netplan

Ubuntu Server 24.04 stores network configuration in /etc/netplan/. There's usually a file there called something like 50-cloud-init.yaml. Let's check what's in that directory:

ls /etc/netplan/
Enter fullscreen mode Exit fullscreen mode

Open the configuration file and you will see something like this by default:

vi /etc/netplan/50-cloud-init.yaml
Enter fullscreen mode Exit fullscreen mode
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: true
Enter fullscreen mode Exit fullscreen mode

Change it to use a static IP instead. Here's what I configured:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: false
      dhcp6: false
      addresses:
        - 192.168.1.100/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 1.0.0.1
Enter fullscreen mode Exit fullscreen mode

As you can see, the IP address includes the CIDR notation. The CIDR /24 means a subnet mask of 255.255.255.0. I'm using explicit routes instead of the older gateway4 key, because Ubuntu 24.04 deprecated gateway4. For DNS, I pointed to Cloudflare's public DNS servers, but you can use the DNS servers of your choice.

Applying the changes

Once the file is saved, instruct Netplan to apply the changes:

sudo netplan apply
Enter fullscreen mode Exit fullscreen mode

Verifying the changes

Check that your static IP is now assigned:

ip addr show enp0s3
Enter fullscreen mode Exit fullscreen mode

It should now show your new static IP address.

Top comments (0)