Change the IP while preserving access

A server currently using DHCP needs a stable address for DNS and monitoring. Netplan generates Ubuntu network configuration from YAML for the selected network service. This guide identifies the interface, prefix, gateway and DNS, then tests access from a second client before accepting the change, because changing an IP over SSH can disconnect that session.

Interface and network-file ownership

Use Ubuntu 24.04 LTS with netplan.io and systemd-networkd. Confirm the actual interface instead of assuming ens160.

No particular VM allocation is required. IPAM, an active console and a test client matter. Match sample subnets and DNS to the actual network.

Inspect all of /etc/netplan. Do not manually overwrite cloud-init or automation output without changing its authoritative source. Multiple YAML files merge.

NetworkManager and rollback limitations

Use routes for the default route. Older gateway4 syntax is deprecated. Desktops might use NetworkManager. Try has rollback limitations around virtual links. After timeout, verify actual state and on-disk files.

YAML, renderer and resolver

YAML does not operate the network directly: Netplan translates it into settings for a renderer. Here networkd manages networking and resolved handles name resolution. A new file does not necessarily remove an older interface setting because YAML files merge. Inspect the combined configuration with netplan get before applying it.

Example: a VM in VLAN 30

The VM moves from DHCP to 10.20.30.10/24 via 10.20.30.1. Exclude the address from DHCP allocation or reserve it correctly. Validate VLANs and management ACLs before changing it.

Restricted files and IPv6 paths

Use mode 600 because some network YAML contains secrets. Static IP does not replace a firewall. Check DNS, management and IPv6 paths too. A ping response establishes only that ICMP path.

Write YAML and confirm netplan try

Start through the console so a possible SSH loss does not remove the recovery path. Record current addresses and routes, then back up the entire /etc/netplan directory. Keeping only the newly written file is insufficient to restore the previous configuration.

BASH
ip -br address
ip route
resolvectl status
sudo netplan get
sudo cp -a /etc/netplan /root/netplan-before-change

This configuration targets a single-NIC VM with one authoritative network file. Identify the active file and remove conflicting YAML ownership. If cloud-init generates it, transfer ownership deliberately or update the cloud configuration source.

BASH
sudo tee /etc/netplan/01-production.yaml >/dev/null <<'EOF'
network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:
      dhcp4: false
      addresses: [10.20.30.10/24]
      routes:
        - to: default
          via: 10.20.30.1
      nameservers:
        addresses: [10.20.30.53, 10.20.30.54]
        search: [corp.example.com]
EOF
sudo chmod 600 /etc/netplan/01-production.yaml
sudo netplan generate
sudo netplan try --timeout 120

Generate must complete without errors before try begins. While the try window is open, use a separate session on the server itself to check ens160 addressing, the default route, gateway reachability and internal DNS with the following commands. From the second client, establish a fresh SSH connection to the server's new IP; successful DNS or ping on that client does not validate the server's network settings. Confirm only when the address and default route match the plan and all checks pass. Test timeout rollback on the target environment and retain an independent console.

BASH
ip -br address show ens160
ip route get 10.20.30.53
resolvectl query files01.corp.example.com
ping -c 3 10.20.30.1

Replace files01.corp.example.com with an existing internal DNS record. A zone’s existence does not guarantee an A record at its root.

Tests after the change and reboot

Test SSH from management and the application from its consumer subnet. Record IP, routes and DNS. Repeat after a controlled reboot to detect cloud-init or automation overwrites.

Valid YAML with broken networking

generate identifies syntax/indentation errors. Use spaces instead of tabs. Extra default routes might come from merged files or remaining DHCP.

BASH
sudo netplan --debug generate
networkctl status ens160
sudo journalctl -u systemd-networkd -n 100 --no-pager
resolvectl status ens160

Correct IP with an unreachable gateway requires mask, VLAN and neighbor checks. Broken DNS with working routes calls for resolver and port-53 ACL checks. Settings reverting after boot require file-ownership investigation.

Verify link neighbor and address ownership

BASH
ip -br link show ens160
ip neigh show dev ens160
networkctl status ens160
sudo netplan get

UP describes link state, not a correct VLAN. Replace ens160 with the actual interface. If the gateway neighbor remains FAILED or INCOMPLETE after traffic, inspect mask, VLAN and switch configuration before changing DNS. Compare netplan get with the YAML and renderer output. A configuration that survives only until reboot may be regenerated by cloud-init or another owner.

Restore YAML through the console

From the console, move the new file out and restore prior YAML. Leave no overlapping extra file. Run generate before apply.

BASH
sudo mv /etc/netplan/01-production.yaml /root/01-production.yaml.failed
sudo cp -a /root/netplan-before-change/. /etc/netplan/
sudo netplan generate
sudo netplan apply

After rollback, retest routes, DNS, SSH and the application. Keep VLAN/IPAM records outside the VM. Netplan try does not replace an independent console.

Change one part at a time

Avoid changing NIC, VLAN and IP simultaneously. Check address conflicts first. During try, test login/DNS from a second client and confirm only after success.

Best Practices

  • Version-control configuration and review changes.
  • Use least privilege and explicit allow-lists.
  • Automate repeatable checks and monitor the expected state.
  • Test upgrades and restores before production rollout.

Conclusion

A reliable implementation of Set a Static IP on Ubuntu Server with Netplan is more than a successful first run. Keep the configuration documented, observable, recoverable, and aligned with the team's change-management process.

Frequently Asked Questions

Does netplan try guarantee recovery?

No. Review rollback limitations and retain an independent console.

Why do settings revert after reboot?

Cloud-init or a configuration management tool usually still generates the file.

Official Sources

Share

Meet AJInfrastructure & DevOps
Loading…