NetworkManager & nmcli
Configure static and DHCP connections persistently with nmcli.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 9 minutes to read.
In short
On RHEL, NetworkManager owns network configuration and nmcli is its command-line interface. Settings live in connection profiles attached to devices, so you modify a profile and bring it up to apply changes persistently. Ubuntu Server uses netplan, which can drive NetworkManager or systemd-networkd.
Explained simply
On RHEL and many other distributions, a background service called NetworkManager looks after the network. It reads saved settings, applies them to the network cards, and keeps them applied after reboots and cable unplugs.
The saved settings are called connection profiles. A device is the real network card, such as ens160. A profile is a named set of settings (an address, a gateway, DNS servers) that can be put on a device. One device can have several profiles, but only one is active at a time.
nmcli is the command you use to talk to NetworkManager. You change a profile, then activate it. The change is saved to disk, so it is still there after a reboot, unlike changes made with the ip command.
Ubuntu Server uses netplan instead: you describe the network in a small YAML file and netplan hands it to the real network service. The ideas are the same: describe the desired state, then apply it.
Press Play on a diagram to walk through it one step at a time.
- Devices
- ens160 (ethernet)
- lo (loopback)
- Connection profile: static1
- connection.interface-name ens160
- ipv4.method manual
- ipv4.addresses 192.168.1.50/24
- ipv4.gateway and ipv4.dns
- connection.autoconnect yes
- Storage
- Keyfiles in /etc/NetworkManager/system-connections
- 1Inspect
nmcli device status and con show
- 2Modify profile
method manual, address, gateway, DNS
- 3Activate
nmcli con up NAME
- 4Verify live state
ip -br addr, ip route, resolvectl or cat /etc/resolv.conf
- 5Test
ping the gateway, resolve a name
In depth
Profiles, devices and where settings live
nmcli device status shows each device, its type, its state (connected, disconnected, unmanaged) and the active profile. nmcli connection show lists all profiles, and the DEVICE column is empty for inactive ones. A profile is tied to a device by connection.interface-name.
On RHEL 9 and newer, profiles are saved as keyfiles in /etc/NetworkManager/system-connections/, one NAME.nmconnection file per profile, in an INI-like format. The old ifcfg files in /etc/sysconfig/network-scripts are deprecated. Keyfiles must be owned by root with mode 600, or NetworkManager ignores them. If you edit a keyfile by hand, run nmcli connection reload so NetworkManager reads it again.
To see every setting of one profile, run nmcli connection show NAME. Lines in lowercase are saved settings; lines in capitals, such as IP4.ADDRESS, show what is currently applied.
- nmcli -f NAME,DEVICE,TYPE con show picks columns
- nmcli -g ipv4.addresses con show static1 prints one value
- nmcli general hostname shows or sets the hostname
Static, DHCP and DNS settings
ipv4.method decides how the address is set: auto means DHCP, manual means static, and disabled turns IPv4 off. For manual you set ipv4.addresses with a prefix, such as 192.168.1.50/24, plus ipv4.gateway and ipv4.dns. Several DNS servers go in one quoted, space-separated value. ipv4.dns-search adds search domains, so a short name like db01 is tried as db01.lab.example.com.
A DHCP profile can still carry your own DNS servers. By default they are added to the ones DHCP provides; set ipv4.ignore-auto-dns yes to use only yours. A plus or minus before a property name adds or removes one value instead of replacing the list, for example +ipv4.dns 9.9.9.9.
The same properties exist for IPv6 under ipv6.*, and ipv6.method also accepts auto (SLAAC) and ignore.
- ipv4.method auto | manual | disabled
- +ipv4.addresses adds a second address
- ipv4.ignore-auto-dns yes drops DHCP DNS
Applying changes safely, even over SSH
nmcli connection modify only saves settings. They reach the device when the profile is activated: nmcli connection up NAME. That separation lets you prepare everything first, then apply in one step. nmcli device reapply ens160 is another way to push changes to the running device, and it does not take the link down.
Changing the IP address you are connected through will cut your SSH session. Put the modify and up commands on one line joined with &&, so the second runs even after your session drops, and reconnect to the new address. Better still, have console access ready. nmcli connection down NAME deactivates a profile, and with autoconnect yes it will come back at the next boot.
If you get lost in property names, nmtui gives a simple menu, and nmcli connection edit NAME opens an interactive editor with tab completion.
- Admin to NetworkManager: con mod static1 ipv4.dns 9.9.9.91con mod static1 ipv4.dns 9.9.9.9
- NetworkManager to Keyfile: Save static1.nmconnection2Save static1.nmconnection
- NetworkManager to Admin: Saved; device unchanged3Saved; device unchanged
- Admin to NetworkManager: con up static14con up static1
- NetworkManager to ens160: Apply address, routes and DNS5Apply address, routes and DNS
netplan on Ubuntu
Ubuntu Server describes networking in YAML files in /etc/netplan/, such as 50-cloud-init.yaml. netplan does not configure interfaces itself. It translates the YAML for a renderer: systemd-networkd, the default on servers, or NetworkManager, the default on desktops.
Indentation matters in YAML, and the files should be readable by root only (mode 600), or netplan prints warnings. Use routes with to: default and via: instead of the older gateway4 key, which is deprecated.
Apply changes with netplan try: it applies the config and rolls back after 120 seconds unless you press Enter to confirm. That makes remote changes much safer. netplan apply applies without the safety net, and netplan get shows the merged settings. On cloud images, cloud-init may rewrite its file at boot; put your own changes in a separate file with a higher number, or disable cloud-init's network config.
- sudo netplan try
- sudo netplan apply
- networkctl status shows systemd-networkd state
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1List devices and connections~35m
- 2Create a static IPv4 connection~35m
- 3Modify DNS and gateway settings~35m
- 4Bring connections up and down~35m
- 5Know netplan on Ubuntu~35m
Key points
- nmcli device status shows devices; nmcli connection show lists profiles.
- Set ipv4.method manual along with addresses, gateway and DNS for a static IP.
- nmcli con up NAME applies changes; modify alone does not touch the live interface.
- nmtui is a text UI alternative if you forget the property names.
Key terms
Tap a card to flip it. 0/5 known
Examples
nmcli device status
sudo nmcli con add con-name static1 ifname ens160 type ethernet \
ipv4.method manual ipv4.addresses 192.168.1.50/24 \
ipv4.gateway 192.168.1.1 ipv4.dns "192.168.1.1 1.1.1.1"
sudo nmcli con up static1
sudo nmcli con mod static1 +ipv4.addresses 192.168.1.51/24
sudo nmcli con up static1 && ip -br addr show ens160Create a persistent static profile, then add a second address.
Switch the existing DHCP profile to a static address
A new RHEL VM got 192.168.1.137 from DHCP. It should keep the fixed address 192.168.1.60/24 with gateway .1 and two DNS servers. Step 1: find the profile name for ens160; installers often call it ens160 or Wired connection 1. Step 2: change it to manual with all settings in one command. Step 3: activate it, joined with && so the command finishes even when your SSH session to the old address drops. Step 4: reconnect to .60 and verify.
nmcli -f NAME,DEVICE con show --active
sudo nmcli con mod ens160 ipv4.method manual \
ipv4.addresses 192.168.1.60/24 ipv4.gateway 192.168.1.1 \
ipv4.dns "192.168.1.1 9.9.9.9" ipv4.dns-search lab.example.com \
&& sudo nmcli con up ens160
# reconnect: ssh admin@192.168.1.60
ip -br addr show ens160
nmcli -g ipv4.dns,ipv4.gateway con show ens160The same static setup with netplan
On Ubuntu Server, the same goal is one YAML file. Step 1: create /etc/netplan/60-static.yaml with the address, default route and DNS servers. Step 2: set mode 600. Step 3: run netplan try, check you can still reach the server, then press Enter to keep it. If you cannot reach it, wait 120 seconds and the old config comes back.
# /etc/netplan/60-static.yaml
network:
version: 2
ethernets:
ens160:
dhcp4: false
addresses: [192.168.1.60/24]
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [192.168.1.1, 9.9.9.9]
search: [lab.example.com]Quick reference
- nmcli dev status lists devices; nmcli con show lists profiles (active ones have a DEVICE)
- nmcli con mod NAME ipv4.method manual ipv4.addresses IP/PREFIX ipv4.gateway GW ipv4.dns "A B"
- nmcli con mod only saves; nmcli con up NAME applies the change
- Use +ipv4.dns or -ipv4.dns to add or remove one value instead of replacing all
- Keyfiles live in /etc/NetworkManager/system-connections/ (mode 600); run nmcli con reload after hand edits
- nmcli con add type ethernet con-name NAME ifname DEV creates a new profile
- Ubuntu: edit /etc/netplan/*.yaml, then sudo netplan try (auto rollback after 120 s)
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Show the device, type, and state of every network device, and the full IPv4 settings of the active profile on your main interface.
- Exercise 2medium
Add a second static address 192.168.1.61/24 and an extra DNS server 1.1.1.1 to profile static1 without removing the existing ones, and apply.
- Exercise 3hard
Create a new profile lab for ens192 with a static address, no gateway, a route to 10.0.0.0/8 via 192.168.50.1, that ignores DHCP DNS and does not start at boot. Then bring it up manually.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can I still edit /etc/resolv.conf by hand on RHEL?
- You can, but NetworkManager overwrites it when a connection changes. Set DNS with nmcli on the connection profile instead.
- What is the difference between nmcli and nmtui?
- They control the same NetworkManager. nmcli is for commands and scripts, nmtui is a simple text menu that is handy when you do not remember property names.
- Does Ubuntu Server use NetworkManager?
- Not by default. Ubuntu Server uses netplan with systemd-networkd as the renderer, while Ubuntu Desktop uses netplan with NetworkManager.
- Why is my device shown as unmanaged?
- NetworkManager has been told not to manage it, for example by a config file in /etc/NetworkManager/conf.d/ or because another tool owns it. Set it back with nmcli device set DEV managed yes.