A Static IP in Netplan
This is what the file looks like. Do not apply it before you have read the next section: a wrong value cuts your SSH session.
network:
version: 2
ethernets:
eth0:
dhcp4: false
addresses:
- 203.0.113.10/24
routes:
- to: default
via: 203.0.113.1
nameservers:
addresses: [192.0.2.53, 198.51.100.53]
- eth0: the interface name. Yours may be
ens3orenp1s0; check withip -br link. - addresses: the address with its prefix length.
/24is explained in subnet masks and CIDR notation. - routes:
to: defaultwithviasets the gateway. The oldergateway4andgateway6keys are deprecated, and netplan warns about them. - nameservers: the DNS servers the system uses.
The values above are examples. On a cloud server the address, prefix, gateway, and DNS servers come from the provider, and they are often delivered by DHCP or written by cloud-init at boot. If you set them statically, copy them from your provider's control panel, never invent them.
Before You Change Anything
A wrong network setting cuts your SSH session.
- Find the server console in your provider's control panel. It keeps working when the network does not.
- Write down the current settings:
ip aandip r. - Copy the current files out of the folder:
sudo cp -a /etc/netplan /root/netplan-backup. Netplan reads every.yamlfile in/etc/netplan/, so do not keep backup copies with that ending there. - Plan to apply with
netplan try, never withnetplan applyfirst.
Find the Right File
ls -l /etc/netplan/
sudo cat /etc/netplan/*.yaml
Files are read in name order, and a later file overrides single values from an earlier one, while lists such as addresses are added together. On cloud images the file is often 50-cloud-init.yaml, written by cloud-init at boot. If you edit it while cloud-init still manages the network, your change can be overwritten. Turn that off first:
echo 'network: {config: disabled}' | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
After that, cloud-init leaves the network files alone, and you can edit 50-cloud-init.yaml or replace it with your own file. More about the tool is in what is cloud-init. Debian servers that use /etc/network/interfaces have no /etc/netplan/ folder; netplan does not apply there.
Edit and Check the File
sudo nano /etc/netplan/50-cloud-init.yaml
sudo chmod 600 /etc/netplan/50-cloud-init.yaml
sudo netplan generate
Netplan prints a warning that the permissions are too open when a file can be read by other users; its documentation recommends files owned by root with mode 600. netplan generate parses the files and reports errors, usually with the file name, line, and column, and writes the back-end configuration without applying it to the network.
Apply Safely with netplan try
sudo netplan try
netplan try applies the new configuration and asks you to press Enter to keep it. If you do not confirm within 120 seconds, it rolls back to the previous settings by itself. While it counts down, open a second SSH session to the new address. Confirm only when that works. Add --timeout 300 for more time. If a test times out or is canceled, check with ip a and ip r that the old settings really came back.
Indentation Errors
- Spaces only: YAML does not allow tabs for indentation.
- Consistent depth: each level uses the same number of spaces, usually two.
- Colon and space: write
dhcp4: false, notdhcp4:false. - List items: each address and route starts with a dash and a space, indented under its key.
An error that names a line and column almost always points at one of these.
Check the Result
ip -br a
ip r
ping -c 4 203.0.113.1
resolvectl status
The address and the default via route should match the file. resolvectl status shows the DNS servers in use on systems running systemd-resolved, as Ubuntu does. If names stop resolving, see temporary failure in name resolution. To add an IPv6 address to the same file, see how to configure IPv6 on a Linux server.