Sub topic 10
Changing the network without locking yourself out
This chapter assumes you can already write a Netplan or NetworkManager static configuration (covered in the previous chapters) and that you work over SSH. It does not teach new settings. It teaches how to apply them to a remote server so that a typo costs you thirty seconds and not a trip to the data center.
Why remote network changes are risky
Most configuration changes fail in a way you can see. A network change fails in a way that cuts off your view. When you change the address, route, or interface of the connection you are using, three things can go wrong:
- The server comes up with the wrong address, so your SSH session has nothing to talk to.
- The address is right but the prefix length or gateway is wrong, so replies cannot find their way back to you.
- The configuration is rejected or only partly applied, and the interface ends up with no address at all.
In every case the server is still running. You just cannot reach it. The goal is to make sure there is always a way back: an automatic rollback, a second path in, or a console.
Prepare before you touch anything
Do these steps first. They take a few minutes and remove most of the risk.
- Confirm console access. Test that you can actually log in through the hypervisor console, cloud serial console, or out-of-band controller (IPMI, iDRAC, iLO). Do not assume it works. A console you have never tried is not a recovery plan.
- Record the current state. Save the output of the inspection commands from earlier in this tutorial so you know what "working" looked like.
- Back up the configuration. Copy the files you are about to edit.
- Open a second SSH session. Leave it connected and idle. An established session sometimes survives a brief change, and you will use a new one to test.
- Run inside tmux or screen. If your SSH connection drops, the commands you started keep running instead of being killed with your shell.
ip -br address show > ~/network-before.txt
ip route show >> ~/network-before.txt
resolvectl status >> ~/network-before.txt
sudo cp -a /etc/netplan /root/netplan.bak
tmux new -s netchangeChange one thing at a time. Do not alter the network, the firewall rules, and the SSH ListenAddress in the same maintenance window. If something breaks, you want one suspect.
Validate the file before applying it
Netplan can check your YAML and generate the backend configuration without changing the running network. A syntax error caught here never reaches the live interface.
sudo chmod 600 /etc/netplan/01-static.yaml
sudo netplan generateIf netplan generate prints nothing, the file parsed. This catches bad indentation, unknown keys, and a wrong value type. It does not catch a valid configuration that is wrong for your network, such as the wrong gateway. Only a live test catches that, which is the job of netplan try.
Use netplan try for automatic rollback
netplan try applies the new configuration and then waits for you to confirm it. If you do not confirm within the timeout, Netplan restores the previous configuration by itself. The default timeout is 120 seconds, and you can change it with --timeout.
sudo netplan try --timeout 60Netplan prints a prompt and a countdown. The decision rule is simple:
- Press Enter to accept the new configuration and keep it.
- Press Ctrl+C, or do nothing, to roll back.
- If your SSH session freezes because the change broke connectivity, you cannot press Enter. The timeout expires and the old configuration returns. You then reconnect to the old address.
That last case is why the command is safe. A broken configuration cannot be confirmed, so it never stays.
What netplan try does not cover
netplan try reverts network state that Netplan manages. It does not undo everything:
- It does not revert the hostname,
/etc/hosts, or/etc/resolv.confedits you made by hand. - It does not revert firewall changes.
- It needs an interactive terminal, which is another reason to run it in tmux.
- Established TCP connections can survive a rollback or a same-address change, but do not count on it.
Keep the scope of one netplan try to network configuration only.
Add the new address before removing the old one
When you renumber a server, the safest method is to hold both addresses for a while. Suppose the server currently uses 192.168.1.20/24 and must move to 192.168.1.50/24. Do not replace one with the other. List both:
network:
version: 2
renderer: networkd
ethernets:
enp1s0:
dhcp4: false
addresses:
- 192.168.1.20/24
- 192.168.1.50/24
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [192.168.1.1, 9.9.9.9]
search: [example.com]The procedure is:
- Apply this file with
sudo netplan try. Your current SSH session on.20keeps working, because that address never went away. - From a second terminal on your workstation, open a new SSH session to
192.168.1.50. - If that works, press Enter in the first session to confirm.
- Edit the file to remove
192.168.1.20/24and runsudo netplan tryagain, this time connected through.50.
At no point is the only working address the one you just typed. A server can hold several addresses on one interface, but it has only one default route in this setup. Keep a single default entry so the route table stays unambiguous.
Rollback when there is no built-in rollback
NetworkManager's nmcli has no equivalent of netplan try. A change to a profile is saved by nmcli connection modify, but it takes effect only when you run nmcli connection up. That gap is useful: you can stage the change, schedule a safety net, and then activate.
The safety net is a timed job that restores your backup unless you cancel it. systemd-run can create one without writing a unit file. This example works for the Netplan backup made earlier:
sudo systemd-run --on-active=5m --unit=netrollback \
/bin/sh -c 'rm -f /etc/netplan/*.yaml && cp -a /root/netplan.bak/. /etc/netplan/ && netplan apply'This creates a timer named netrollback.timer. Five minutes from now it replaces your configuration with the backup and applies it. After you have applied your change and verified it from a second session, cancel the timer:
sudo systemctl stop netrollback.timerFor NetworkManager, back up the profile before you modify it, then make the rollback command restore the old settings. A profile lives in /etc/NetworkManager/system-connections/, so copy that directory and have the timer copy it back, then run nmcli connection reload and nmcli connection up "Wired connection 1".
The at command works the same way if it is installed. The principle matters more than the tool: schedule the undo before you do the risky step, and cancel it only after you have proof that the step worked.
| Method | Reverts automatically | Works with | Main limit |
|---|---|---|---|
| `netplan try` | Yes, after the timeout | Netplan | Needs an interactive terminal. Covers only Netplan-managed state. |
| `systemd-run` timer | Yes, at the set time | Any tool | You write the restore command and must cancel it. |
| Two addresses in parallel | Not needed | Netplan or nmcli | Works only if the old address is still valid. |
| Console access | No, it is manual | Everything | Needs access set up in advance. |
Clean up conflicting configuration
A change can silently lose to another file. Netplan reads every YAML file in /etc/netplan/, in alphabetical order, and merges them. Cloud images often ship 50-cloud-init.yaml, which sets DHCP on the same interface. If your file is named 01-static.yaml, the cloud-init file is read later, and its values can override yours or add a second address.
Check the whole directory before you apply:
ls -l /etc/netplan/
sudo netplan getnetplan get prints the merged configuration, which is what Netplan will actually use. If cloud-init regenerates its file on every boot, tell it to stop managing the network:
echo 'network: {config: disabled}' | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfgDo this only on a server where you intend to own the network configuration yourself.
Also check which tool owns the interface, as you did earlier in the tutorial. Editing a Netplan file on a machine where NetworkManager controls the interface has no effect, and you may wrongly conclude the change failed.
Verify from outside the session you are changing
A successful apply is not proof. The test that matters is reaching the server through a path that did not exist before the change. Use these checks in order:
- From a second terminal, open a fresh SSH connection to the new address.
- On the server, confirm the address and route with
ip -br address show dev enp1s0andip route get 9.9.9.9. - Confirm the gateway answers:
ping -c 3 192.168.1.1. - Confirm name resolution:
resolvectl query example.com.
Only after the fresh connection works should you press Enter in netplan try or cancel your rollback timer. If the second session fails, do nothing and let the rollback run.
Common mistakes
- Confirming too early. Pressing Enter as soon as the prompt appears defeats the purpose. Test first, then confirm.
- Setting a timeout that is too short. A
--timeoutof 10 seconds does not give you time to open a second session. Use 60 seconds or more. - Wrong prefix length. Using
/32instead of/24leaves the gateway unreachable, because the server believes no other host is on its local network. - A wrong interface name. Names such as
enp1s0can differ between machines or change after hardware changes. Confirm the name withip -br linkimmediately before editing. - Editing the file you are reading from. If you saved a bad version over your only copy, you have no baseline. Always keep the backup.
- Forgetting that the rollback has a time limit. A
systemd-runtimer set for five minutes will fire during your verification if you take six. Set it with margin and re-arm it if needed.
Exercises
Exercise
You run sudo netplan try over SSH and the new configuration has a wrong gateway. Your session freezes. What happens, and what do you do?
Show answer
Nothing needs to be done during the countdown. You cannot press Enter on a frozen session, so the timeout expires and Netplan restores the previous configuration. Wait for the timeout (120 seconds by default), reconnect to the old address, correct the gateway in the YAML file, and run netplan try again.
Exercise
A server uses 192.168.1.20/24. You must move it to 192.168.1.50/24 over SSH. Describe the steps that keep a working path to the server at every moment.
Show answer
List both 192.168.1.20/24 and 192.168.1.50/24 under addresses and apply the file with netplan try. Your existing session on .20 stays up. From a second terminal, open a new SSH session to 192.168.1.50. If it works, press Enter to confirm. Then remove 192.168.1.20/24 from the file and run netplan try again, connected through .50, and confirm once the check passes.
Exercise
You use NetworkManager, which has no netplan try. How can you still get an automatic rollback, and when do you cancel it?
Show answer
Back up the profile, then schedule a timed restore with systemd-run (for example --on-active=5m --unit=netrollback) that copies the backup back and reactivates the connection. Run nmcli connection up to apply the change. After a fresh SSH session to the new address works and the checks pass, cancel the rollback with sudo systemctl stop netrollback.timer.