Sub topic 6
Setting a static IP with NetworkManager
This chapter assumes you can read ip output, know what a prefix length and a default gateway are, and can use sudo. It also assumes you have console or out-of-band access to the server, or a recovery plan, because changing the address of the interface you are logged in over can end your session.
The Netplan chapter covers Ubuntu-style YAML. This chapter covers the other common case: a server where NetworkManager owns the interface. That includes RHEL, Rocky, AlmaLinux, Fedora, and many desktop-derived or Ubuntu Desktop installs.
The working model: devices and connection profiles
NetworkManager separates two things:
- A device is the network interface the kernel exposes, such as
enp1s0. - A connection (also called a profile) is a saved set of settings: address method, addresses, gateway, DNS, and more.
A profile is applied to a device when it is activated. One device can have several profiles, but only one is active at a time. A profile can be tied to a specific interface through its connection.interface-name setting, or left unbound so it can match any device of the right type.
On current distributions, profiles are stored as keyfiles in /etc/NetworkManager/system-connections/, one *.nmconnection file per profile. RHEL 8 and older used ifcfg files in /etc/sysconfig/network-scripts/. In both cases, edit profiles with nmcli rather than by hand. nmcli validates your input and keeps the permissions correct. NetworkManager ignores keyfiles that are not owned by root with mode 600.
The key point from the diagram is that modify edits the saved profile only. The running interface keeps its old settings until you activate the profile again.
Inspect before you change
First confirm that NetworkManager is the tool in charge, and find the device and profile names.
systemctl is-active NetworkManager
nmcli device status
nmcli connection shownmcli device status lists each device with its state and the connection currently using it. The CONNECTION column gives the exact profile name to use in later commands. A device shown as unmanaged is not under NetworkManager's control, which usually means another tool (such as systemd-networkd through Netplan) owns it.
To see the settings of one profile, filter by field:
nmcli -f ipv4,connection.interface-name,connection.autoconnect connection show "Wired connection 1"Also record what the server uses now, so you can compare afterwards and revert if needed:
ip -br address show
ip route show default
resolvectl statusThe ipv4 properties that matter
| Property | Meaning |
|---|---|
| `ipv4.method` | How the address is obtained: `auto` (DHCP), `manual` (static), `link-local`, `shared`, or `disabled` |
| `ipv4.addresses` | One or more `address/prefix` entries, for example `192.168.1.50/24` |
| `ipv4.gateway` | The default gateway, which must be inside the configured subnet |
| `ipv4.dns` | DNS servers, in order of preference |
| `ipv4.dns-search` | Search domains appended to short names |
| `ipv4.ignore-auto-dns` | With `method auto`, ignore DNS servers offered by DHCP |
Property names use dots and are used verbatim in nmcli connection modify.
Convert an existing profile to static
This is the standard approach: edit the profile that already manages the interface.
nmcli connection show
sudo nmcli connection modify "Wired connection 1" \
ipv4.method manual \
ipv4.addresses 192.168.1.50/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "192.168.1.1 9.9.9.9" \
ipv4.dns-search example.com
sudo nmcli connection up "Wired connection 1"Several details are worth knowing:
- Set the method and the address in the same command. Switching
ipv4.methodtomanualwithout an address is rejected, because a static profile with no address is invalid. - Quote profile names that contain spaces.
Wired connection 1is the default name NetworkManager gives to an automatically created Ethernet profile. - Always include the prefix length. Without it,
nmcliassumes/32. A/32address has no on-link neighbors, so the gateway is unreachable and the route cannot be installed. - List DNS servers in one quoted string, separated by spaces, or separated by commas. Order matters, because the first server is tried first.
- The gateway must fall inside the address's subnet.
192.168.1.1is valid for192.168.1.50/24. With/26the same pair would also work, but a gateway of192.168.1.200would not.
The up command re-activates the profile on its device. When the address changes, a session connected through the old address drops. Reconnect using the new address. If you only changed settings that NetworkManager can update in place (DNS servers, for example), sudo nmcli device reapply enp1s0 applies them without a full deactivation.
Create a new static profile instead
Sometimes you want to keep the DHCP profile and add a static one beside it. This is safer on a remote machine because the old profile stays available as a fallback.
sudo nmcli connection add type ethernet ifname enp1s0 con-name static-enp1s0 \
ipv4.method manual \
ipv4.addresses 192.168.1.50/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "192.168.1.1 9.9.9.9" \
ipv4.dns-search example.com \
connection.autoconnect yesAdding the profile does not activate it. If both profiles have autoconnect yes, NetworkManager picks between them using connection.autoconnect-priority (higher wins), and the older default profile may win after a reboot. Set the old profile aside so the choice is not left to chance:
sudo nmcli connection modify "Wired connection 1" connection.autoconnect no
sudo nmcli connection modify static-enp1s0 connection.autoconnect-priority 100Changing the address without locking yourself out
Unlike Netplan's netplan try, nmcli has no built-in confirm-or-roll-back step. You can build one with a transient systemd timer that re-activates the known-good profile unless you cancel it. This example assumes you created static-enp1s0 as above and kept the DHCP profile as the fallback:
sudo systemd-run --unit=nm-revert --on-active=3min \
nmcli connection up "Wired connection 1"
sudo nmcli connection up static-enp1s0Your SSH session will drop if the address changed. Reconnect to the new address and verify (next section). If everything works, cancel the revert:
sudo systemctl stop nm-revert.timerIf you cannot reconnect, the timer fires after three minutes and puts the old profile back, which restores the old address.
Verify the result
Check four things: the address, the route, the DNS servers, and actual reachability.
ip -br address show dev enp1s0
ip route get 9.9.9.9
nmcli -g IP4.ADDRESS,IP4.GATEWAY,IP4.DNS device show enp1s0
resolvectl query example.com
ping -c 3 192.168.1.1ip route get 9.9.9.9 shows which gateway and source address the kernel would use to reach an outside host. It should name 192.168.1.1 and 192.168.1.50. The nmcli device show output is what NetworkManager believes it applied, so a mismatch with ip output points to another tool changing the interface.
Reboot-test a server before you rely on it. Static settings that pass up but come from a profile that does not autoconnect will disappear at the next boot. Confirm with:
nmcli -f connection.autoconnect,connection.interface-name connection show static-enp1s0How NetworkManager hands DNS to the system
NetworkManager does not resolve names. It collects DNS servers and search domains from active profiles and passes them to a resolver backend. The rc-manager setting in /etc/NetworkManager/NetworkManager.conf controls that. On systems running systemd-resolved, NetworkManager pushes the servers to it per interface, and /etc/resolv.conf normally points to the local stub resolver. On systems without it, NetworkManager can write /etc/resolv.conf itself. The Resolver chapter covers both paths. For this chapter, what matters is that your DNS settings belong in the profile, not in a hand-edited /etc/resolv.conf, which NetworkManager overwrites on the next activation.
Common problems
| Symptom | Likely cause | Check |
|---|---|---|
| Settings revert after reboot | Profile not set to autoconnect, or a second profile has a higher priority | `nmcli connection show`, compare `autoconnect-priority` |
| `up` fails with a device error | Profile bound to the wrong `interface-name` | `nmcli device status`, then fix `connection.interface-name` |
| Address is right, nothing outside responds | Missing or wrong gateway, or a wrong prefix length | `ip route show default`, `ip route get 9.9.9.9` |
| Pings to IPs work, names do not | DNS servers wrong or unreachable | `nmcli -g IP4.DNS device show enp1s0`, `resolvectl query` |
| Device shows `unmanaged` | Another tool (such as Netplan with `networkd`) owns it | `nmcli device status`, `/etc/netplan/` |
| Your edit disappears | Profile is generated by Netplan with the NetworkManager renderer | See below |
Adding or removing a second address
A server sometimes needs an extra address, for example for a service migration. The + prefix appends to a list property instead of replacing it, and - removes an entry:
sudo nmcli connection modify static-enp1s0 +ipv4.addresses 192.168.1.51/24
sudo nmcli connection modify static-enp1s0 -ipv4.addresses 192.168.1.51/24
sudo nmcli device reapply enp1s0Without the +, the command replaces the whole list, and you lose the primary address. This is one of the most common ways to cut yourself off from a remote server.
Exercises
Exercise
You run sudo nmcli connection modify "Wired connection 1" ipv4.addresses 192.168.1.50/24 on a server that is using DHCP, then check ip -br address. The address has not changed. Give two reasons, and say what you would add to the command and what you would run next.
Show answer
First, modify only changes the saved profile; the running interface keeps its settings until the profile is activated again, so you need sudo nmcli connection up "Wired connection 1". Second, ipv4.method is still auto, so the profile stays on DHCP. Add ipv4.method manual in the same command, along with ipv4.gateway and ipv4.dns, then run up.
Exercise
You must move a remote server to 192.168.1.50/24 through NetworkManager and you have no console access. Describe a safe procedure, using commands from this chapter.
Show answer
Create a new profile with nmcli connection add type ethernet ifname enp1s0 con-name static-enp1s0 ... and keep the old profile. Schedule a fallback with sudo systemd-run --unit=nm-revert --on-active=3min nmcli connection up "Wired connection 1". Activate the new profile with sudo nmcli connection up static-enp1s0, reconnect over the new address, and verify it with ip -br address, ip route get 9.9.9.9, and ping. If it works, run sudo systemctl stop nm-revert.timer. If it does not, the timer restores the old profile.
Exercise
You append a second address with nmcli connection modify static-enp1s0 ipv4.addresses 192.168.1.51/24 (no plus sign) and then reapply. What happens, and what is the correct command?
Show answer
Without +, the command replaces the entire address list, so 192.168.1.50/24 is removed and only .51 remains. A remote session that used .50 is lost. The correct command is sudo nmcli connection modify static-enp1s0 +ipv4.addresses 192.168.1.51/24.