Sub topic 10
Changing the Network Without Locking Yourself Out
This chapter assumes that you can use a Linux shell, edit configuration files, use sudo, and understand IPv4 addresses, prefixes, and gateways. It also assumes that you are changing a server remotely over SSH. If possible, keep console or out-of-band access available before changing the network.
Plan the change first
A network change can disconnect your SSH session immediately. Prepare the complete configuration before applying it.
Record the current state:
ip -br address show
ip route show
hostnamectl
resolvectl statusSave a copy of any configuration file that you will edit:
sudo cp -a /etc/netplan/01-static.yaml \
/etc/netplan/01-static.yaml.before-changeThe filename may be different on your server. List the directory if needed:
ls -l /etc/netplan/Write down these values before changing anything:
- The interface name, such as
enp1s0 - The current address and prefix length
- The gateway address
- The DNS server addresses
- The connection or profile name if NetworkManager is in use
Check the new values with whoever manages the network. A wrong prefix length can make the gateway appear unreachable even when the address itself looks correct.
Use a reversible change method
Netplan provides netplan try, which applies a configuration temporarily. It waits for confirmation and rolls the change back automatically if you do not confirm it. This protects a remote session when the new configuration is invalid.
For example:
sudo chmod 600 /etc/netplan/01-static.yaml
sudo netplan tryRead the displayed configuration and confirm it only when the SSH session still works and the values are correct. If the session disconnects or you do not confirm before the timeout, Netplan restores the previous configuration.
After a successful test, apply the configuration:
sudo netplan applyDo not use netplan apply as the first test on a remote server unless you have console access or another reliable recovery method.
NetworkManager does not provide the same general confirmation workflow through nmcli. Before changing a profile, record its current settings:
nmcli connection show
nmcli connection show "Wired connection 1"Make one change at a time, and keep a second SSH session open. If the change fails, restore the previous values from your notes and reactivate the profile:
sudo nmcli connection up "Wired connection 1"A scheduled rollback is another option for high-risk changes. For example, an administrator can schedule a command that restores the old configuration several minutes later, then cancel that command after verifying the new network. Test this method on a nonproduction server first.
Verify in layers
Test the network from the lowest layer to the highest. This helps identify the failing part instead of treating every failure as an SSH problem.
| Test | What it checks |
|---|---|
| `ip -br address show dev enp1s0` | Address and interface state |
| `ip route get 9.9.9.9` | Route selection and gateway |
| `ping -c 3 192.168.1.1` | Reachability of the local gateway |
| `resolvectl query example.com` | Resolver operation |
| `ssh user@server-address` | New remote login |
First confirm that the interface is up and has the expected address. Next confirm that the route to an outside address uses the expected interface and gateway. Then test the gateway itself. A successful gateway test with a failed outside test often indicates a missing default route or an upstream firewall.
Test DNS separately from network reachability:
resolvectl query example.com
getent hosts example.comresolvectl query tests the system resolver service. getent hosts tests the system name-service path, including the order configured in /etc/nsswitch.conf. If an IP address works but a hostname does not, inspect DNS and name-service configuration rather than changing the route.
Change safely on a remote server
Use this sequence:
- Open a second SSH session.
- Confirm console or recovery access.
- Back up the current configuration.
- Validate the address, prefix, gateway, and DNS values.
- Apply the change with a rollback method.
- Test the interface, route, gateway, DNS, and a new SSH login.
- Keep the old session open until all tests pass.
Exercise
You changed the address successfully, but ping to the gateway fails. Which values should you check first?
Show answer
Check the interface name, address, prefix length, and gateway address. A wrong prefix can place the gateway outside the server's local network, and a wrong interface can leave the intended interface unconfigured.