Set a Static IP on Ubuntu Server 26.04 with Netplan

Aidin

Illustration of a LAN cable plug for a guide on setting a static IP on Ubuntu Server 26.04

Hi everyone, this time I want to share how to set a static IP on Ubuntu Server 26.04 using netplan. A static IP is something you really need to set before the server is used for any service, whether that is SSH, a DHCP server, a DNS server, a web server, or a file server. If the server still gets its IP from DHCP, the address can change at any time and every client computer that connects to the server will lose its connection.

I did this hands on with the same server from my guide on how to install Ubuntu Server 26.04 LTS, and then tested it from a Windows PC acting as the client. Two things turned out to be different from most tutorials you find online. The netplan file on my server is not called 50-cloud-init.yaml, and the 26.04 installer had already turned off cloud-init network configuration automatically. I cover both of them in their own steps.

The steps below also work on Ubuntu Server 24.04 and 22.04 because they all use netplan. The only things you need to adjust are the file name and the interface name.

Preparation Before Setting a Static IP

Before changing anything, gather three pieces of information first: the name of your network interface, the gateway address, and the DNS used on your network. Also decide which IP you want to use as the static IP. Make sure that IP is not already used by another device, and ideally pick one outside your router’s DHCP range so there is no conflict.

On my network, the router sits at 192.168.1.1 and I chose 192.168.1.200 for the server. Before using it, I pinged 192.168.1.200 from another PC and got no reply, which means the IP was still free.

Check the interface name and current IP

ip a and ip route output showing the DHCP IP 192.168.1.16 on Ubuntu Server 26.04

Log in to the server, then run the following commands.

ip -4 a show
ip route

You can see that the interface on my server is named enp0s3 with the IP 192.168.1.16 obtained from DHCP, marked by the words dynamic and proto dhcp. The line default via 192.168.1.1 shows the gateway in use. Write down your interface name and gateway because you will need both in the configuration file. The interface name can be different on every computer, for example ens18, ens33, or eth0.

Check the netplan configuration file

Contents of the 00-installer-config.yaml netplan file with dhcp4 true on Ubuntu Server 26.04

All netplan configuration is stored in the /etc/netplan folder. Look at the file name and then display its contents.

ls /etc/netplan/
sudo cat /etc/netplan/00-installer-config.yaml

On my server there is only one file, named 00-installer-config.yaml, which was created by the installer with the setting dhcp4: true. A lot of tutorials mention a file called 50-cloud-init.yaml, and that file is indeed used on servers built from a cloud image or on a VPS. Just use whatever file name exists on your own server.

Make sure cloud-init does not overwrite the configuration

The 00-subiquity-disable-cloudinit-networking.cfg file with the network config set to disabled

On a cloud server or VPS, cloud-init can overwrite the netplan configuration every time the server boots, so the IP goes back to DHCP. Check first whether cloud-init network configuration has already been disabled.

ls /etc/cloud/cloud.cfg.d/ | grep network
sudo cat /etc/cloud/cloud.cfg.d/00-subiquity-disable-cloudinit-networking.cfg

On my fresh Ubuntu Server 26.04 install, the file 00-subiquity-disable-cloudinit-networking.cfg was already there with the content network: {config: disabled}. That means the installer has already turned it off and you do not need to create any file.

Note: If that file does not exist on your server, which usually happens on a VPS or a cloud image, create the file /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg containing a single line network: {config: disabled}. This method follows the official cloud-init documentation.

How to Set a Static IP with Netplan

Back up the old configuration file

Before editing, keep a copy of the original file. I save it in my home folder, not in /etc/netplan, because netplan reads every file ending in .yaml inside that folder.

sudo cp /etc/netplan/00-installer-config.yaml ~/00-installer-config.yaml.bak

Edit the netplan YAML file

Static IP 192.168.1.200 configuration in the netplan file opened with nano

Open the configuration file with nano.

sudo nano /etc/netplan/00-installer-config.yaml

Delete the old content, then type the following configuration. Adjust the interface name, IP, gateway, and DNS to match your own network.

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: false
      dhcp6: false
      addresses:
        - 192.168.1.200/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [192.168.1.1, 8.8.8.8]

The addresses line holds the static IP along with its prefix, and /24 is the same as the subnet mask 255.255.255.0. The routes section with to: default and via is the new way of writing the gateway. The old gateway4 syntax has been marked as deprecated in the netplan documentation, so you should not use it anymore. Nameservers holds the DNS, and I use the router’s DNS with 8.8.8.8 as a backup. Save with Ctrl + O, press Enter, then exit with Ctrl + X.

Warning: YAML is very sensitive to indentation. Use spaces, not Tab, and make sure the number of spaces at each level is exactly the same as in the example. A single wrong space can make netplan reject the whole file.

Check the permission and validate the file

Netplan file permission set to rw for root only and netplan generate running without errors

The netplan file should only be readable and writable by root. Check its permission, then validate the file contents with netplan generate.

sudo stat -c '%A %U:%G %n' /etc/netplan/00-installer-config.yaml
sudo netplan generate

The result -rw——- root:root means the permission is already 600. The netplan generate command prints nothing at all when the YAML is written correctly. If there is a mistake, netplan immediately points to the line that has the problem.

Applying the Static IP Configuration

Test it first with netplan try

netplan try screen with a 120 second countdown before the configuration is reverted

Do not jump straight to netplan apply, especially if you manage the server remotely. Use netplan try first.

sudo netplan try

Netplan applies the new configuration and then waits for confirmation. If you do not press Enter within 120 seconds, the configuration is automatically rolled back to the previous state. This feature saves you if it turns out you mistyped the IP or gateway and your SSH connection drops.

My experience: Before pressing Enter, I opened PowerShell on my Windows PC and pinged 192.168.1.200. Only once I got a reply was I sure the new configuration was working and safe to accept.

Configuration accepted message after pressing Enter during netplan try

Press Enter, and the message Configuration accepted will appear, which means the new configuration has been saved permanently.

Apply it with netplan apply

Static IP 192.168.1.200 with a proto static gateway and DNS on Ubuntu Server 26.04

Run netplan apply to make sure the configuration is really applied, then check the IP, route, and DNS again.

sudo netplan apply
ip -4 -br a show enp0s3
ip route
resolvectl dns enp0s3

You can see that the IP has changed to 192.168.1.200/24, the gateway is now default via 192.168.1.1 proto static, and the DNS shows 192.168.1.1 and 8.8.8.8. The words dynamic and proto dhcp are gone.

Testing the Static IP

Testing should be done from two sides. From the server, to make sure it can reach the internet, and from a client PC, to make sure the server can be reached at its new IP. The client PC is another computer on the same network, and here I use a Windows PC connected to the same router as the server.

Test the internet connection from the server

Successful ping to 8.8.8.8 and ubuntu.com from Ubuntu Server after setting a static IP

Ping a public IP and a domain name.

ping -c 3 8.8.8.8
ping -c 3 ubuntu.com

Pinging 8.8.8.8 tests the gateway, while pinging ubuntu.com also tests DNS at the same time. If the ping to the IP works but the ping to the domain fails, the problem is in your nameservers setting.

Check the network status with netplan status

netplan status output showing the interface online with IP 192.168.1.200

Netplan has a command that shows the full state of the network.

sudo netplan status enp0s3

At the top it says Online state: online. Below that you can see the address 192.168.1.200/24, the DNS, and the route default via 192.168.1.1 labeled static.

netplan get ethernets enp0s3 output showing address, nameservers, and routes

To see the configuration netplan is currently reading, use netplan get.

sudo netplan get ethernets.enp0s3

Test ping from the client PC

Pinging the server static IP 192.168.1.200 from PowerShell on a Windows client PC

Now move to the client PC. On Windows, open PowerShell or Command Prompt, then ping the server IP.

ping 192.168.1.200

If you see Reply from 192.168.1.200 with 0% loss, the server can now be reached from other computers using its new static IP.

Test SSH from the client PC

Successful SSH login to Ubuntu Server via static IP 192.168.1.200 from a Windows PC

Still in PowerShell, try connecting to the server over SSH using the new IP. Replace the username with your own.

ssh mamangdigital@192.168.1.200

After entering the password, I was logged in to the server and the command ip -4 -br a showed 192.168.1.200. From now on the server can simply be accessed through this IP, and the IP will not change even when the server or the router is restarted.

Common Problems and How to Fix Them

Permissions for /etc/netplan are too open

Permissions for /etc/netplan are too open warning when running netplan generate

I deliberately changed the file permission to 644 to see this message. Netplan immediately showed WARNING Permissions for /etc/netplan/00-installer-config.yaml are too open. Netplan configuration should NOT be accessible by others. This message does not take your network down, but you should still fix it because a netplan file can contain sensitive data such as a WiFi password.

sudo chmod 600 /etc/netplan/00-installer-config.yaml
netplan generate running without warnings after the permission is changed to 600

After the permission was set back to 600, netplan generate ran again without any warning.

gateway4 has been deprecated

This message shows up if you are still using the gateway4: 192.168.1.1 syntax from an old tutorial. Replace it with a routes block containing to: default and via, like the example above. Do not use both at once, because the default route can only be defined once for each IP family.

Error in network definition

This is almost always caused by wrong YAML indentation or a missing colon. Netplan shows the line and column number where the problem is, so open the file again and check that line. Make sure there are no Tab characters, because YAML only accepts spaces.

IP goes back to DHCP after a reboot

The most common cause is cloud-init rewriting the netplan file during boot, usually on a VPS. Create the 99-disable-network-config.cfg file as shown in the preparation section. Another cause is that there is still another .yaml file in /etc/netplan that enables dhcp4. Netplan reads all .yaml files in order of their names, so delete or move any file you are not using.

SSH disconnects after changing the IP

This is normal because the old SSH session is still using the previous IP. Just reconnect SSH to the new IP. So you do not end up locked out of a remote server, get used to using netplan try, because the configuration is rolled back automatically if you are unable to press Enter.

At this point the Ubuntu 26.04 server is running on a static IP and can be reached from the client PC through both ping and SSH. The next step is securing remote access to this server, which I cover in the guide to configure an SSH server on Ubuntu 26.04 with SSH keys, from passwordless login to changing the SSH port. If you run into a different error while trying this, just write it in the comments along with the error message, and we can figure out the solution together.

Leave a Comment