How to Set a Static IP in Ubuntu with Netplan
Subnet calculator (CIDR)
192.168.1.10/24 — you get the netmask, network address, broadcast and host range.To set a static IP on Ubuntu, edit the YAML file in /etc/netplan/, specify the address, gateway and DNS servers, then apply it with sudo netplan apply. Below is the full sequence, including a safe way to verify the config so you do not lock yourself out of the server.
Step 1. Identify the interface name
Netplan is configured per network interface name, so find it first:
ip -br addr
Typical names are eth0, ens3, enp1s0. Note the name of the active interface that currently holds an address.
Step 2. Locate the netplan config file
ls /etc/netplan/
There is usually a single file: 01-netcfg.yaml, 50-cloud-init.yaml or 00-installer-config.yaml. Make a backup copy first:
sudo cp /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml.bak
Step 3. Describe the static address
Open the file in an editor:
sudo nano /etc/netplan/50-cloud-init.yaml
Bring the contents to this shape (replace the interface name and addresses with your own):
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: false
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
What the parameters mean:
| Parameter | Purpose |
|---|---|
dhcp4: false |
disables automatic address assignment |
addresses |
IP with the prefix length (/24 = 255.255.255.0) |
routes → via |
default gateway address |
nameservers |
DNS servers |
Important: YAML is indentation-sensitive — use spaces, never tabs.
Step 4. Check the syntax
Validate the config before applying it:
sudo netplan generate
If there are no errors, the command exits silently.
Step 5. Apply with a safety net
On a remote server use try — it rolls the changes back after 120 seconds unless you confirm them:
sudo netplan try
If the connection survived, press Enter to confirm. To apply for good:
sudo netplan apply
Step 6. Verify the result
ip -br addr show ens3
ip route
ping -c 3 1.1.1.1
Make sure the address is assigned, the gateway is in the routing table and the ping goes through.
Multiple addresses and a separate private network
Netplan lets you assign several addresses to a machine — for example a public one and a private one for traffic between your own servers:
network:
version: 2
ethernets:
ens3:
dhcp4: false
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses: [1.1.1.1, 8.8.8.8]
ens4:
dhcp4: false
addresses:
- 10.0.0.5/24
Here ens3 is the public interface with the gateway, and ens4 is the private network with no default route — the gateway belongs on one interface only.
Config permissions and indentation errors
Since Ubuntu 22.04 netplan warns about overly permissive file permissions on the config. Tighten them:
sudo chmod 600 /etc/netplan/50-cloud-init.yaml
If netplan generate reports indentation errors, check that only spaces are used. A quick way to find tabs:
grep -nP '\t' /etc/netplan/50-cloud-init.yaml
Frequently asked questions
What does /24 in the address mean?
It is the subnet mask prefix length. /24 = 255.255.255.0, /16 = 255.255.0.0. Take the value from the details your provider gave you.
I lost the connection after apply — what now?
Connect through the server’s KVM console, restore the backup (cp ...bak) and run netplan apply. This is exactly why you should always use netplan try on a remote machine.
networkd or NetworkManager — which one?
Servers use networkd, which is the default renderer. NetworkManager is more common on desktops.
How do I add a second IP to an interface?
Add another line under addresses, for example - 203.0.113.11/24. Netplan supports multiple addresses per interface.
Do the changes survive a reboot? Yes, the netplan config is applied at system start, so the static address is permanent.
Deploying a server and need a static public IP out of the box? Pick a Linux VPS in the VPS hosting section or order server administration.