Configure Static IPv4 and IPv6 Networking on AlmaLinux 9 and Rocky Linux 9
Configure a static IPv4 and IPv6 address, gateway and DNS servers on AlmaLinux 9 or Rocky Linux 9 with nmcli or a NetworkManager keyfile.
This guide gives a server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 a static IPv4 address and a static IPv6 address, with gateways and DNS servers, using NetworkManager. It shows the same configuration twice: with nmcli commands and as a keyfile you can template or copy between machines. CentOS 7 reached end of life in June 2024, and the method older tutorials describe for it (an ifcfg-eth0 file with NM_CONTROLLED=no, then disabling NetworkManager in favour of the network service) does not carry over: the RHEL 9 family has no legacy network service to fall back on, and NetworkManager is the supported way to configure interfaces.
Before you start
- You need
sudorights and the values assigned by your provider: IPv4 address and prefix length, IPv4 gateway, IPv6 address and prefix length, IPv6 gateway, and DNS servers. - Have out-of-band access ready. A wrong gateway or prefix cuts off SSH, and the only way back is your provider's web console (VNC or serial console). Open it and log in before you change anything.
- The examples use the interface
eth0, IPv4203.0.113.10/24with gateway203.0.113.1, IPv62001:db8:1::10/64with gateway2001:db8:1::1, and DNS servers198.51.100.53,198.51.100.54and2001:db8:1::53. Replace all of them.
Step 1: Inspect the current configuration
List the devices and the connection profiles NetworkManager has for them:
nmcli device status
nmcli -f NAME,UUID,TYPE,DEVICE connection showA profile name is not always the device name: cloud images often call it System eth0 or cloud-init eth0, and a fresh install may call it Wired connection 1. Note the name in the NAME column of the row for your device, then record the working settings so you can restore them:
ip -br addr
ip route
ip -6 route
cat /etc/resolv.confTo make the commands below copy-and-paste friendly, store the profile name in a variable:
CON="eth0"Step 2: Stop cloud-init from overwriting the network settings
Most VPS images use cloud-init, which regenerates the network configuration from the provider's metadata at boot and can undo manual changes. Check whether it is installed:
rpm -q cloud-initIf it is, tell it to leave networking alone:
sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg > /dev/null <<'EOF'
network:
config: disabled
EOFIf the package is not installed, skip this step.
Step 3: Set the static IPv4 configuration with nmcli
Switch the profile to manual addressing and supply the address, gateway and DNS servers in one command, so the profile is never left half-configured:
sudo nmcli connection modify "$CON" \
ipv4.method manual \
ipv4.addresses 203.0.113.10/24 \
ipv4.gateway 203.0.113.1 \
ipv4.dns "198.51.100.53 198.51.100.54"ipv4.method manual requires at least one address in ipv4.addresses, and the address is always written with its prefix length. Nothing changes on the live interface yet; modify only edits the stored profile.
Step 4: Set the static IPv6 configuration
sudo nmcli connection modify "$CON" \
ipv6.method manual \
ipv6.addresses 2001:db8:1::10/64 \
ipv6.gateway 2001:db8:1::1 \
ipv6.dns 2001:db8:1::53With ipv6.method manual, NetworkManager ignores router advertisements, so the default route exists only if you set ipv6.gateway. Some providers do not hand out a global gateway address and expect the route to come from router advertisements or to point at a link-local address such as fe80::1. In the first case keep automatic configuration and add your fixed address on top of it:
sudo nmcli connection modify "$CON" \
ipv6.method auto \
ipv6.addresses 2001:db8:1::10/64 \
ipv6.gateway ""In the second case use ipv6.method manual with ipv6.gateway fe80::1. Use whichever form your provider's network documentation specifies.
Step 5: Apply the profile
Reactivate the connection to apply the stored settings. An SSH session over this interface may pause for a moment, and it will not come back if a value is wrong, which is why the console should already be open:
sudo nmcli connection up "$CON"Step 6: Verify
ip -br addr show dev eth0
ip route
ip -6 route
nmcli device show eth0You should see both addresses on the interface, a default via 203.0.113.1 line for IPv4 and a default via 2001:db8:1::1 line for IPv6. Then test reachability at each level: the gateway, an external address and name resolution.
ping -c 3 203.0.113.1
ping -6 -c 3 2001:db8:1::1
ping -c 3 example.com
ping -6 -c 3 example.comFinish with a reboot while you still have the console open, and run the checks again. That is the only test that proves the configuration persists and that nothing rewrites it at boot.
The same configuration as a keyfile
NetworkManager stores profiles as keyfiles in /etc/NetworkManager/system-connections/, one .nmconnection file per profile. The commands above produced one; you can also write or edit it directly, which is convenient for configuration management:
sudo nano /etc/NetworkManager/system-connections/eth0.nmconnection[connection]
id=eth0
type=ethernet
interface-name=eth0
autoconnect=true
[ipv4]
method=manual
address1=203.0.113.10/24
gateway=203.0.113.1
dns=198.51.100.53;198.51.100.54;
[ipv6]
method=manual
address1=2001:db8:1::10/64
gateway=2001:db8:1::1
dns=2001:db8:1::53;If you are editing a file NetworkManager created, keep its existing uuid= line in the [connection] section. Further addresses go on address2=, address3= and so on, and DNS servers are separated by semicolons. NetworkManager ignores keyfiles that anyone other than root can read or write, so fix the ownership and mode, then make NetworkManager reread the directory and activate the profile:
sudo chown root:root /etc/NetworkManager/system-connections/eth0.nmconnection
sudo chmod 600 /etc/NetworkManager/system-connections/eth0.nmconnection
sudo nmcli connection reload
sudo nmcli connection up eth0Editing the file alone does nothing until you run nmcli connection reload.
Rolling back and troubleshooting
To return to automatic addressing from the console, clear the static values and reactivate:
sudo nmcli connection modify "$CON" ipv4.method auto ipv4.addresses "" ipv4.gateway "" ipv4.dns ""
sudo nmcli connection modify "$CON" ipv6.method auto ipv6.addresses "" ipv6.gateway "" ipv6.dns ""
sudo nmcli connection up "$CON"- The settings are gone after a reboot: cloud-init rewrote them (see Step 2), or a second profile for the same device took over.
nmcli connection showlists all profiles; delete the stale one withsudo nmcli connection delete "NAME". - IPv4 works, IPv6 does not: check
ip -6 routefor a default route and confirm that IPv6 is not switched off in the kernel withsysctl net.ipv6.conf.all.disable_ipv6(it should print0). Also make sure IPv6 is enabled for the server in your hosting control panel. - Activation fails:
sudo journalctl -u NetworkManager -n 50 --no-pagershows the reason, typically a gateway outside the address's subnet or a typing error in a keyfile. - Old
ifcfgfiles from a migrated system:sudo nmcli connection migrateconverts profiles stored in/etc/sysconfig/network-scripts/to keyfiles.
Frequently asked questions
Can I still disable NetworkManager and use ifcfg files as on CentOS 7?
No. The legacy network service and its scripts are not part of the RHEL 9 family, so there is nothing to hand the interface to. NetworkManager on release 9 still reads existing ifcfg files, but that format is deprecated there and no longer read at all in RHEL 10 and its rebuilds. Use keyfiles for anything new.
Do I need a static configuration on a VPS at all?
Often not. If the provider assigns your addresses by DHCP and router advertisements, they stay the same for the life of the server and the automatic profile is the least fragile choice. Static configuration is needed when the provider tells you to configure addresses yourself, when the image has no DHCP for IPv6, or when you want the server to keep working if the provider's DHCP service is unavailable.
How do I add a second address to the same interface?
Append it to the profile with a plus sign, for example sudo nmcli connection modify "$CON" +ipv4.addresses 203.0.113.11/24, then run sudo nmcli connection up "$CON". In a keyfile, add an address2= line. The first address in the list is the one the server uses as the source for outgoing connections by default.
Why is /etc/resolv.conf not the place to set DNS servers?
NetworkManager generates that file from the profiles of the active connections and overwrites manual edits the next time a connection is activated. Set ipv4.dns and ipv6.dns in the profile instead. When a profile stays on automatic addressing but you want your own resolvers, also set ipv4.ignore-auto-dns yes and ipv6.ignore-auto-dns yes.