How to Configure a DHCP or Static IP on Ubuntu Server
Set a static IP address or DHCP on Ubuntu Server with Netplan: find the interface and the file, the current syntax, and a safe way to apply it.
2 min readBy Smita
Reviewed by the AmazingRDP technical team

A Linux VPS comes with its own IP address already set up, and root access. See Linux VPS from $6.99
Ubuntu Server sets up its network with Netplan: you describe the network in a small text file and Netplan applies it. A new installation normally gets its address automatically (DHCP). A server that others must find, such as a web, file or database server, should have an address that does not change.
1. Find the interface name and the file
ip -br link
ls /etc/netplan/
The first command lists the network cards; yours is the one that is not lo, for example ens18 or enp3s0. Old guides say eth0, which is rarely right today. The second shows the Netplan file, usually 50-cloud-init.yaml or 00-installer-config.yaml. Copy it before you change it:
sudo cp /etc/netplan/50-cloud-init.yaml ~/netplan-backup.yaml
2a. A static address
Open the file with sudo nano /etc/netplan/50-cloud-init.yaml and write:
network:
version: 2
ethernets:
ens18:
dhcp4: false
addresses:
- 192.168.1.100/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
Use your interface name, address, gateway and DNS servers. /24 is the same as the mask 255.255.255.0. Two things trip people up:
- Spaces only. The file is YAML: indent with spaces, two per level, never with the Tab key.
gateway4is outdated. Older guides use the linegateway4: 192.168.1.1. Current Ubuntu versions warn about it; use theroutesblock shown above.
2b. Back to DHCP
network:
version: 2
ethernets:
ens18:
dhcp4: true
3. Apply it safely
sudo chmod 600 /etc/netplan/*.yaml
sudo netplan try
netplan try applies the settings and waits two minutes for you to press Enter. If you do not, because the new settings cut your SSH connection, it puts the old settings back by itself. Always use it on a remote server. When you are sure, or on a machine you sit at:
sudo netplan apply
4. Check
ip -br addr
ip route
resolvectl status
ping -c 3 1.1.1.1
These show the address, the default route, the DNS servers in use, and whether the internet is reachable.
If the settings come back after a restart
On cloud images, a service called cloud-init writes the Netplan file again at every start. Tell it to leave the network alone:
echo "network: {config: disabled}" | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
Common errors
- "Invalid YAML" or "expected mapping". A tab, a wrong indent or a missing colon. The message names the line.
- The address works, the internet does not. The gateway is wrong or missing.
- Addresses work, names do not. The
nameserversblock is missing. - "Permissions are too open". Run the
chmod 600line above.
On a VPS or dedicated server
The address, mask and gateway are given by the provider; you cannot choose your own. Our Linux VPS plans arrive with the network already set. Change it only to add an extra address you were assigned, and keep the provider's console at hand in case SSH stops answering. Related guides: why Ubuntu freezes and how to fix it, clone Ubuntu Server with Clonezilla and copy and paste in Ubuntu Server.
Questions
How do I set a static IP on Ubuntu Server?
Edit the file in /etc/netplan/, set dhcp4: false, add the address, a default route and name servers, then run sudo netplan try.
How do I find my network interface name?
Run ip -br link. The interface is the entry that is not lo.
What replaced gateway4 in Netplan?
A routes block with to: default and via: followed by the gateway address.
What happens if I make a mistake on a remote server?
With sudo netplan try, the old settings return after two minutes unless you confirm.