Static network configuration instead of DHCP

Static configuration means the address, gateway and DNS servers are written in a file instead of being learned at boot. On a VPS the values never change, so either approach works; you switch to static when you need full control, extra addresses or custom routes. The safe order is: read the current values, disable cloud-init network management, write the new file, apply it with an automatic rollback.

Networking and security guide, updated 2026-12-03

DHCP or static: what is the difference on a VPS?

With DHCP the server asks the network for its IPv4 address and gateway every time the interface comes up. With static configuration the server already knows them from a file. On our servers the IPv4 address assigned to your server is fixed for its whole lifetime, so DHCP always hands out the same values. The reasons to go static are about control, not stability:

  • You want extra IPv6 addresses, policy routes or a custom MTU alongside the main address.
  • You run software such as a hypervisor bridge, WireGuard routing or a container network that needs a predictable setup.
  • You prefer that the network comes up identically even if a DHCP reply is slow at boot.

If none of these applies, keeping the default configuration is perfectly fine.

How do you read the current values first?

Never type a gateway from memory. Read what the running system uses and copy it:

ip -br a
ip route
ip -6 route
resolvectl status

Write down four things: the interface name (for example eth0), the IPv4 address with its prefix length, the IPv4 default gateway from the line default via ..., and the IPv6 default gateway from ip -6 route. On many networks the IPv6 gateway is a link-local address such as fe80::1; use exactly what your output shows. The IPv6 address and prefix are also on the server page.

The examples below use 203.0.113.10/32, gateway 203.0.113.1, and 2001:db8:1234:5678::1/64 with gateway fe80::1. Replace every one of them with your own values.

How do you stop cloud-init from managing the network?

Cloud-init writes the network configuration at first boot and may do so again later. Before you take over, switch that off:

cat > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg <<EOF
network:
  config: disabled
EOF

From now on, the files cloud-init produced stay untouched and you own them. On Ubuntu that is /etc/netplan/50-cloud-init.yaml; on Rocky, Alma, Fedora and CentOS Stream it is a NetworkManager connection; on some Debian builds it is under /etc/network/interfaces.d/.

What does a static netplan file look like?

On Ubuntu 24.04 and 22.04, and on Debian builds that use netplan, replace the content of the cloud-init file or write a new file and remove the old one:

# /etc/netplan/01-static.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
      accept-ra: false
      addresses:
        - 203.0.113.10/32
        - 2001:db8:1234:5678::1/64
      routes:
        - to: default
          via: 203.0.113.1
          on-link: true
        - to: default
          via: fe80::1
      nameservers:
        addresses: [9.9.9.9, 2620:fe::fe]

The on-link: true flag matters when the IPv4 address is a /32 or when the gateway sits outside the prefix: it tells the kernel the gateway is reachable directly on the link even though it is not in the subnet. Without it, the default route is rejected and the server goes offline. The IPv6 gateway is link-local, so it needs no flag.

Set the permissions with chmod 600 /etc/netplan/*.yaml and keep only one file that defines eth0, or the files are merged and DHCP may stay on.

How do you apply netplan without locking yourself out?

Use the built-in safety net:

netplan generate
netplan try --timeout 120

netplan generate catches syntax errors before anything changes. netplan try applies the configuration and waits. If your SSH session still works, press Enter to keep it. If the session froze, do nothing: after 120 seconds the previous configuration returns and you can connect again to fix the file.

There is no browser console on our servers, so this rollback is your main protection. Two extra habits help: run the change inside tmux so the command keeps running if your connection drops, and keep a second SSH session open while you test.

When everything works, run netplan apply once more and reboot to prove that the configuration survives a cold start.

How do you set a static address with nmcli?

On Rocky Linux, AlmaLinux, Fedora and CentOS Stream, change the existing connection rather than creating a new one:

nmcli con mod "System eth0" \
  ipv4.method manual \
  ipv4.addresses 203.0.113.10/32 \
  ipv4.gateway 203.0.113.1 \
  ipv4.dns "9.9.9.9" \
  ipv6.method manual \
  ipv6.addresses 2001:db8:1234:5678::1/64 \
  ipv6.gateway fe80::1 \
  ipv6.dns "2620:fe::fe"
nmcli device reapply eth0

NetworkManager handles a gateway outside a /32 by adding the needed on-link route on its own. To guard against mistakes, schedule an automatic reboot before you start with shutdown -r +5, and cancel it with shutdown -c once you see the new settings working. Combined with a configuration that is only reapplied and not yet saved for boot, that gives you a way back. Check the connection name with nmcli con show first.

What does a plain systemd-networkd setup look like?

On a minimal Debian system without netplan, or if you prefer fewer layers, you can write networkd files directly:

# /etc/systemd/network/10-eth0.network
[Match]
Name=eth0

[Network]
Address=203.0.113.10/32
Address=2001:db8:1234:5678::1/64
DNS=9.9.9.9
IPv6AcceptRA=no

[Route]
Gateway=203.0.113.1
GatewayOnLink=yes

[Route]
Gateway=fe80::1

Enable the service with systemctl enable systemd-networkd, then disable the previous manager (ifupdown by removing the interface stanza, or NetworkManager) so only one tool controls the interface. Apply with networkctl reload and check with networkctl status eth0.

How are DNS resolvers configured?

Ubuntu and Debian with netplan or networkd use systemd-resolved. In that case /etc/resolv.conf is a link to a stub file and points at 127.0.0.53; the real upstream servers come from your network file. Check them with resolvectl status and test with resolvectl query example.com.

On NetworkManager systems, /etc/resolv.conf is written by NetworkManager from the ipv4.dns and ipv6.dns values. If you edit that file by hand, it will be replaced on the next reapply.

Pick resolvers you trust, or run your own: the DNS guide shows a local Unbound resolver, after which you set 127.0.0.1 as the only nameserver.

What should you check after the switch?

Reboot and confirm each piece separately:

ip -br a
ip route; ip -6 route
ping -c3 203.0.113.1
ping -6 -c3 example.com
getent hosts example.com

If IPv4 works and IPv6 does not, the IPv6 gateway or the accept-ra setting is the usual suspect. If nothing works after reboot and you have no access, the quickest way back is a rebuild, which wipes the disk. That is why the rollback steps above matter, and why backups are worth having: see backups and restore.

Questions about this guide

Will a static configuration break if the server is rebuilt?

A rebuild wipes the disk and the new image starts with its default cloud-init configuration again. Your addresses stay the same, so you can reapply the same file.

Is DHCP on a VPS unreliable?

No. The address it hands out is fixed to your server. Static setup is about control and extra options, not about fixing DHCP.

What if I get the gateway wrong?

The server loses its default route and drops off the internet. With netplan try or a scheduled reboot the old configuration returns on its own.

Can I set a custom MTU?

Yes. Add mtu: 1400 under the interface in netplan or MTUBytes= in a networkd [Link] section. Read MTU and MSS before changing it.

Is there a console I can use if networking breaks?

No, servers are managed over SSH only, so always use a method with automatic rollback when changing the network.

Which DNS servers should I use?

Any public resolver you trust, or your own Unbound instance. Set at least two upstream servers or a local resolver so one outage does not stop name lookups.