Setting a static IP on Ubuntu 18.04+ is editing /etc/netplan/*.yaml with the desired IP, gateway, and DNS, then sudo netplan apply. On 16.04 and earlier, /etc/network/interfaces is the config file. The team that uses Netplan YAML is on a modern Ubuntu - the older interfaces file is for legacy systems only.
Table of contents
- Find the interface name
- The Netplan YAML
- Apply the change
- Verify the new IP
- Pre-18.04: /etc/network/interfaces
- FAQ
Find the interface name
Before editing, find the network interface name:
ip -br link
# Output: lo UNKNOWN 00:00:00:00:00:00
# ens3 UP 52:54:00:12:34:56
# docker0 DOWN 02:42:ac:11:00:01
The ens3 (or enp0s3, eth0, depending on the driver) is the main interface. The team that has multiple interfaces (main + secondary, or main + bridge) picks the right one.
The Netplan YAML
On Ubuntu 18.04+, the default config is at /etc/netplan/01-netcfg.yaml or /etc/netplan/50-cloud-init.yaml (cloud-init managed).
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: no
addresses:
- 192.0.2.10/24
routes:
- to: default
via: 192.0.2.1
nameservers:
addresses:
- 1.1.1.1
- 8.8.8.8
The team that uses cloud-init’s YAML should NOT edit it manually - cloud-init overwrites the file on every boot. The fix is preserve_hostname: true style: either set network: config: disabled in cloud.cfg or use match: { mac: ... } to make cloud-init only manage specific interfaces.
Apply the change
Test the YAML syntax:
sudo netplan try
netplan try applies the config and rolls back after a timeout if you do not confirm. The team that uses netplan apply directly skips the safety net - typos in the YAML can lock the server out.
If netplan try is OK:
sudo netplan apply
The new IP is active immediately. The team that uses ip addr show confirms.
Verify the new IP
ip -br addr show ens3
# Output: ens3 UP 192.0.2.10/24
ip route show default
# Output: default via 192.0.2.1 dev ens3
The team that sees the new IP and the right default route has the right config. The team that sees no IP or no default route has a YAML error - netplan apply should have printed it.
Pre-18.04: /etc/network/interfaces
On Ubuntu 16.04 and earlier:
auto ens3
iface ens3 inet static
address 192.0.2.10
netmask 255.255.255.0
gateway 192.0.2.1
dns-nameservers 1.1.1.1 8.8.8.8
Apply:
sudo ifdown ens3 && sudo ifup ens3
The team that has a 16.04 system is on a legacy release - the upgrade path is to a newer LTS.
FAQ
Why did my server lose connectivity after netplan apply?
The YAML has an error - usually the wrong gateway or missing default route. Console access is the recovery path (cloud providers have a serial console or VNC). The team that uses netplan try first avoids this - it rolls back automatically.
Can I keep DHCP and have a static IP?
Yes, with dhcp4: yes plus a fallback dhcp4-overrides: { use-dns: false } or a manual dhcp-identifier: mac. The team that wants DHCP-assigned with a known fallback is fine.
What does the /24 mean in the IP?
The CIDR netmask - 24 bits means 255.255.255.0. /32 is a single IP. /16 is the larger /16 range. The team that picks /24 for a typical subnet has 254 usable IPs.
Why does cloud-init overwrite my Netplan config?
Cloud-init manages networking by default. The fix is to disable cloud-init’s network management: add network: { config: disabled } to /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg. The team that uses cloud-init’s set_hostname style approach lets cloud-init own the network and just declares what it should be.
Does the network restart require a reboot?
No. sudo netplan apply activates the new config without a reboot. The team that reboots anyway is being safe - the change is non-disruptive in most cases.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.
Useful related references: