Web servers and networking · Intermediate · 14 min
Configure a static IP address on Ubuntu with Netplan
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Netplan 1.2 (systemd-networkd)
You will give an Ubuntu 26.04 server a fixed address, gateway, DNS servers and a second interface on a private network, check the Netplan file offline first, then apply it with netplan try so a mistake rolls itself back.
On this page
Most cloud servers get their address from DHCP, and most of the time that is fine. You want a fixed address when other machines point at this one by IP: a database on a private network, a firewall rule on another host, a port forward on an office router. On an office or home-lab server behind a fibre or LTE router, a router that restarts after a power outage can hand out a different lease, and every port forward breaks until someone notices.
Ubuntu configures networking through Netplan. You describe interfaces in YAML files under /etc/netplan, and Netplan turns them into configuration for a back end, systemd-networkd on a server. In this tutorial you will read the current setup, stop cloud-init from rewriting it, write a static configuration for a public interface and a private one, render it in a scratch directory to check it before it touches the network, and then apply it with netplan try, which undoes the change by itself if you lose the connection.
The examples use these values. Replace every one with your own:
| Setting | Example | Where yours comes from |
|---|---|---|
| Public interface | eth0 | ip -br link (often ens3, enp1s0 or eth0) |
| Public address | 203.0.113.10/24 | Your provider's control panel |
| Gateway | 203.0.113.1 | ip route show default |
| DNS servers | 1.1.1.1, 9.9.9.9 | Your provider's resolvers, or a public pair |
| Private interface | eth1, 10.10.0.11/24 | Your private network or VPC settings |
The 203.0.113.0/24 block is reserved for documentation, so the example addresses never belong to anyone else's machine.
Step 1 · Read the current network setup
Before you change anything, write down what the server uses now. List the interfaces with their addresses, then the default route:
ip -br addr
ip route show defaultThe first command prints one line per interface, something like eth0 UP 203.0.113.10/24 metric 100 fe80::.../64. The second prints something like default via 203.0.113.1 dev eth0 proto dhcp src 203.0.113.10 metric 100. The address after via is your gateway, and the name after dev is the interface you will configure.
Now look at the Netplan files themselves:
sudo ls -l /etc/netplan/
sudo cat /etc/netplan/50-cloud-init.yamlOn a cloud image you see one file, 50-cloud-init.yaml, readable only by root (-rw-------). It holds something like dhcp4: true under your interface, often with a match: macaddress: block and a set-name: line. Write down the MAC address and the name; you will copy both into the new file. A server installed from the ISO may have a differently named file instead; use that name wherever this tutorial says 50-cloud-init.yaml.
netplan get prints the configuration Netplan will actually use, after it has merged every file:
sudo netplan getCheck it worked
You have noted four things: the interface name, its current address and prefix length, the gateway, and the name of the YAML file in /etc/netplan. If you want the static address to be the one the server has now, the values from ip -br addr and ip route show default are the ones to keep.
Step 2 · Stop cloud-init rewriting the file
50-cloud-init.yaml may start with a comment saying changes to it will not persist; not every cloud-init release writes it, but the behaviour is the same either way. cloud-init can render the file again from the provider's metadata at boot, which would quietly undo your work. Tell cloud-init to leave networking alone:
echo 'network: {config: disabled}' | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfgThe file name only has to end in .cfg and sort after cloud-init's own files; 99- makes sure it wins. cloud-init keeps doing its other jobs, such as SSH keys and hostname.
Check it worked
cat /etc/cloud/cloud.cfg.d/99-disable-network-config.cfgIt prints network: {config: disabled}. From the next boot on, cloud-init no longer writes 50-cloud-init.yaml.
Step 3 · Write the static configuration in a scratch directory
Do not edit /etc/netplan yet. Build the new file in a throwaway directory that mirrors the real layout, so you can render and inspect it with no effect on the network:
mkdir -p ~/netplan-demo/etc/netplan
install -m 600 /dev/null ~/netplan-demo/etc/netplan/60-static.yaml
cat > ~/netplan-demo/etc/netplan/60-static.yaml <<'EOF'
network:
version: 2
renderer: networkd
ethernets:
eth0:
match:
macaddress: "52:54:00:12:34:56"
set-name: eth0
dhcp4: false
dhcp6: false
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses: [1.1.1.1, 9.9.9.9]
search: [example.internal]
EOFWhat each part does:
matchandset-nametie the nameeth0to one network card by its MAC address. Copy both from your50-cloud-init.yaml, with your own MAC address. If you leave them out and the provider's name differs from the one the kernel picks, the interface comes up under another name after the next reboot, and nothing in this file applies to it.netplan trycannot catch that, because renaming happens at boot. If your old file has nomatchblock, leave both keys out.install -m 600creates the file private before anything is written to it. Netplan warns when a configuration file can be read by other users, because files can hold Wi-Fi passwords and other secrets.renderer: networkdhands the configuration to systemd-networkd, the default on Ubuntu Server.addressestakes the address with its prefix length./24means a netmask of255.255.255.0; the prefix is required.routeswithto: defaultandviasets the default gateway. Older guides usegateway4:, which is deprecated.nameserverssets the DNS servers and the search domain for this interface. systemd-resolved picks them up.
YAML is strict about indentation. Use spaces, never tabs, and keep each level two spaces deeper than its parent.
Check it worked
ls -l ~/netplan-demo/etc/netplan/
sudo netplan get --root-dir ~/netplan-demo ethernets.eth0The file shows -rw-------, and netplan get prints the eth0 settings back as YAML, with the match block, your address, route and nameservers. If you get an error instead, Netplan names the file, line and column it could not parse.
Step 4 · Render the file and read what Netplan generates
netplan generate --root-dir reads the YAML under a directory of your choosing and writes the systemd-networkd files under that same directory instead of /run. It also runs Netplan's systemd generator from inside the directory, so link the generator in and create the folders systemd would normally provide:
mkdir -p ~/netplan-demo/usr/lib/systemd/system-generators
ln -sf /usr/lib/systemd/system-generators/netplan ~/netplan-demo/usr/lib/systemd/system-generators/netplan
mkdir -p ~/netplan-demo/run/systemd/generator ~/netplan-demo/run/systemd/generator.early ~/netplan-demo/run/systemd/generator.late
sudo netplan generate --root-dir ~/netplan-demo
sudo ls ~/netplan-demo/run/systemd/network/
sudo cat ~/netplan-demo/run/systemd/network/10-netplan-eth0.networkThe listing shows 10-netplan-eth0.network, plus 10-netplan-eth0.link, which holds the rename from set-name. The .network content is something like:
[Match]
PermanentMACAddress=52:54:00:12:34:56
Name=eth0
[Network]
LinkLocalAddressing=ipv6
Address=203.0.113.10/24
DNS=1.1.1.1
DNS=9.9.9.9
Domains=example.internal
[Route]
Destination=0.0.0.0/0
Gateway=203.0.113.1This is exactly what systemd-networkd will read. Reading it is the quickest way to see whether Netplan understood you the way you meant. A missing Gateway= line, for example, means the route did not parse as you intended.
Check it worked
Now break a copy on purpose, to see what a mistake looks like. This one leaves out the prefix length:
mkdir -p ~/netplan-broken/etc/netplan
install -m 600 /dev/null ~/netplan-broken/etc/netplan/60-static.yaml
cat > ~/netplan-broken/etc/netplan/60-static.yaml <<'EOF'
network:
version: 2
ethernets:
eth0:
addresses:
- 203.0.113.10
EOF
if sudo netplan get --root-dir ~/netplan-broken; then echo "expected a parse error"; false; fi
rm -rf ~/netplan-brokenNetplan refuses the file with an error that names it and points at the address, something like address '203.0.113.10' is missing /prefixlength. Your real file produced no such error, so it is valid.
Step 5 · Add a second interface on a private network
Many providers give each server a second network card on a private network, so servers can talk to each other without going over the internet. That interface gets an address but no default gateway: the default route stays on the public side. If the private network reaches other subnets, add a route for each.
Rewrite the scratch file with both interfaces:
cat > ~/netplan-demo/etc/netplan/60-static.yaml <<'EOF'
network:
version: 2
renderer: networkd
ethernets:
eth0:
match:
macaddress: "52:54:00:12:34:56"
set-name: eth0
dhcp4: false
dhcp6: false
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses: [1.1.1.1, 9.9.9.9]
search: [example.internal]
eth1:
dhcp4: false
dhcp6: false
addresses:
- 10.10.0.11/24
routes:
- to: 10.20.0.0/16
via: 10.10.0.1
EOFTraffic to 10.10.0.0/24 goes straight out of eth1 because the server sits on that network. The extra route sends 10.20.0.0/16, another private subnet, through the private router at 10.10.0.1. Leave the routes block out if your private network is a single subnet.
Two default routes on two interfaces is a common mistake. The server then picks one by metric, and replies can leave by a different interface than the request came in on.
Check it worked
sudo netplan generate --root-dir ~/netplan-demo
sudo ls ~/netplan-demo/run/systemd/network/
sudo cat ~/netplan-demo/run/systemd/network/10-netplan-eth1.networkThere are now two .network files, 10-netplan-eth0.network and 10-netplan-eth1.network, next to the eth0 .link file. The eth1 file has Address=10.10.0.11/24 and a [Route] section with Destination=10.20.0.0/16 and Gateway=10.10.0.1, and no route to 0.0.0.0/0.
Step 6 · Install the file and apply it with netplan try
Back up the current configuration first, outside /etc/netplan so Netplan does not read the copy:
mkdir -p ~/netplan-backup
sudo cp -a /etc/netplan/. ~/netplan-backup/
sudo ls -l ~/netplan-backup/Copy your new file into place, owned by root and private:
sudo install -m 600 -o root -g root ~/netplan-demo/etc/netplan/60-static.yaml /etc/netplan/60-static.yamlNetplan reads every file in /etc/netplan in name order and merges them. Later files override single values, but mappings and lists from both files are combined, so anything else the old file sets, such as a match block, extra addresses or routes, would still apply. Remove it so 60-static.yaml is the whole configuration, and the only file you need to read to know what Netplan uses. You have a copy in ~/netplan-backup. This deletes the only file in /etc/netplan that cloud-init created, so check the backup listing above shows it before you run this:
sudo rm /etc/netplan/50-cloud-init.yaml
sudo ls -l /etc/netplan/
sudo netplan get ethernets/etc/netplan now holds only 60-static.yaml, and netplan get prints the eth0 and eth1 settings from it and nothing else.
Read this before the next command. A wrong address, prefix or gateway cuts the server off the network, and over SSH that means you are locked out. netplan try protects you: it applies the new configuration, then waits. If you press Enter in time, it keeps the change. If you do not, because the session froze, it reverts to the previous configuration after 120 seconds.
There is one trap. If the new address differs from the one your SSH session uses, the session drops even when the configuration is right, you cannot press Enter, and the change reverts. In that case run netplan try from your provider's web console instead of SSH. If you are keeping the server's current address, SSH is fine.
sudo netplan try --timeout 120It prints something like Do you want to keep these settings? Press ENTER before the timeout to accept the new configuration, with a countdown. Check the network from a second SSH session or the console (the next step shows what to run), then press Enter in the first one.
Check it worked
After you press Enter, netplan try prints Configuration accepted. If you let it time out instead, it prints something like Reverting. and the server is back on DHCP. The configuration in /etc/netplan is still the new one, so fix it and run netplan try again.
Step 7 · Confirm the live network and know how to go back
Ask Netplan for the live state of each interface, and for any difference between the YAML and the running system:
netplan status eth0
netplan status eth1
netplan status --diffnetplan status shows each interface's addresses, routes and DNS servers. --diff lists anything the YAML asks for that the system does not have, or the reverse; on a correct setup it reports no differences.
Then check with the tools you already know:
ip -br addr show eth0
ip route show default
resolvectl dns eth0
ping -c 3 203.0.113.1You want your static address on eth0 with no dynamic flag, default via 203.0.113.1 dev eth0 with no proto dhcp, your two DNS servers, and replies from the gateway.
If you ever need the old DHCP setup back, restore the backup, give networking back to cloud-init and apply it, again through netplan try:
sudo cp -a ~/netplan-backup/50-cloud-init.yaml /etc/netplan/
sudo rm /etc/netplan/60-static.yaml
sudo rm /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
sudo netplan tryDelete the scratch directory. Its rendered files are owned by root, so use sudo:
sudo rm -rf ~/netplan-demoKeep ~/netplan-backup until you have rebooted at least once.
Check it worked
Reboot with sudo reboot, wait a minute, and SSH back in to the static address. ip -br addr show eth0 still shows it, and sudo ls /etc/netplan/ still lists only 60-static.yaml, which proves cloud-init did not write its file back.
Next steps
The server now has a fixed public address, a private address, and a safe, repeatable way to change both.
- Read the live addresses, routes and neighbour tables in more depth:
/tutorials/see-addresses-and-routes-with-the-ip-command. - Work out prefix lengths and subnets for your private network:
/tutorials/understand-ip-addresses-subnets-and-cidr. - Link this server to another one over an encrypted tunnel:
/tutorials/connect-two-servers-with-a-wireguard-vpn. - Limit which ports each interface answers on:
/tutorials/set-up-ufw-firewall-ubuntu-26-04.
Related tutorials
- Write Nginx location blocks that match what you expectYou will build a practice Nginx 1.30 server where every location block labels its responses, predict which block answers each of twelve URLs, check your answers with curl, and fix the alias, regex-order and proxy_pass mistakes that catch most people out.
- Write a server firewall with nftablesYou will write one /etc/nftables.conf with a default-drop input chain, rate-limited SSH, named address sets, counters and logging, and load it over SSH behind a timer that undoes it if you lose your connection.
- Watch a TCP connection open and close with tcpdumpYou will capture a curl request to a local web server with tcpdump on Ubuntu 26.04, read its handshake, data and teardown line by line, see the reset a closed port sends, and save, filter and rotate captures as pcap files.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials