SkillPilotAI

nmcli and nmtui (2/2)

Part 2 of nmcli and nmtui: the device commands (status, up/down, reapply vs modify, lldp, checkpoint rollback), Wi-Fi and agents, the most used connection settings, the nmtui text menu, and the environment variables, exit codes and colours that matter in scripts.

26.1 Why is it used?

  • nmcli device works on the real interface (lan0) while connection works on saved profiles.
  • reapply pushes a profile change to a running device without dropping it; checkpoint rolls back a bad change automatically.
  • Linux only (client of NetworkManager). Lab: alpine:3.20 + NetworkManager 1.46, lan0 connected to gw 192.168.46.1 (dnsmasq DHCP).
nmcli device up lan0
  1. 1.nmcli device up lan0 (nmcli configures nothing itself)

  2. 2.Request goes to NetworkManager over D-Bus

  3. 3.NetworkManager picks a profile (lan0-dhcp)

  4. 4.DHCP discover goes out of lan0 to the gateway

  5. 5.dnsmasq on gw (192.168.46.1) sends the ack

  6. 6.NetworkManager sets 192.168.46.100/24 on lan0

26.2 nmcli device: status, show

Terminal
nmcli devicenmcli device statusnmcli device show lan0nmcli -f GENERAL,CONNECTIONS device show lan0nmcli -g IP4.ADDRESS device show lan0
Output
$ nmcli device statusDEVICE   TYPE      STATE                   CONNECTIONlan0     ethernet  connected               lan0-dhcplo       loopback  connected (externally)  lolan1     ethernet  unmanaged               --gre0     iptunnel  unmanaged               --$ nmcli device show lan0GENERAL.HWADDR:          02:46:00:00:00:10GENERAL.STATE:           100 (connected)GENERAL.CONNECTION:      lan0-dhcpIP4.ADDRESS[1]:          192.168.46.100/24IP4.GATEWAY:             192.168.46.1IP4.DNS[1]:              192.168.46.1$ nmcli -f GENERAL,CONNECTIONS device show lan0GENERAL.DRIVER:          vethGENERAL.NM-MANAGED:      yesGENERAL.AUTOCONNECT:     yesGENERAL.IS-SOFTWARE:     yesCONNECTIONS.AVAILABLE-CONNECTIONS[1]: ... | lan0-dhcp$ nmcli -g IP4.ADDRESS device show lan0192.168.46.100/24

26.3 nmcli device: set, up/down, connect/disconnect, monitor

  • set IF managed yes|no and autoconnect yes|no change how NetworkManager treats the device.
  • down (alias disconnect) disconnects and blocks auto-activation until a manual action; up (alias connect) activates the best profile.
  • device monitor prints state changes live; delete removes software devices (bridge, bond ...).
Terminal
nmcli device set lan1 managed yesnmcli device set lan1 autoconnect nonmcli device down lan0ip netns exec gw ping -c 1 -W 1 \  192.168.46.100 | tail -2nmcli device up lan0nmcli device disconnect lan0nmcli device connect lan0nmcli device monitor lan0
Output
$ nmcli device status | grep lanlan0  ethernet  connected  lan0-dhcplan1  ethernet  connecting (getting IP configuration)  Wired connection 2$ nmcli device down lan0Device 'lan0' successfully disconnected.$ ip netns exec gw ping -c 1 -W 1 192.168.46.100 | tail -21 packets transmitted, 0 packets received, 100% packet loss$ nmcli device up lan0Device 'lan0' successfully activated with '7630b205-...'.$ nmcli device disconnect lan0Device 'lan0' successfully disconnected.$ nmcli device connect lan0Device 'lan0' successfully activated with '7630b205-...'.$ nmcli device monitor lan0lan0: deactivatinglan0: disconnectedlan0: using connection 'lan0-dhcp'lan0: connecting (prepare)lan0: connecting (getting IP configuration)lan0: connected

26.4 reapply vs device modify

CommandChangesSurvives device up / reboot?
connection modifySaved profile onlyYes, but applies only after reapply / up
device reapplyPushes the profile to the running device, no dropYes (profile)
device modifyRunning device only, not the profileNo, gone after device up
Terminal
nmcli connection modify lan0-dhcp \  ipv4.dns 9.9.9.9nmcli -g IP4.DNS device show lan0nmcli device reapply lan0nmcli device modify lan0 \  +ipv6.addresses 2001:db8:46::a/64nmcli device modify lan0 ipv4.dns 1.1.1.1ip -br addr show lan0nmcli -g ipv4.dns,ipv6.addresses \  connection show lan0-dhcpnmcli device up lan0
Output
$ nmcli -g IP4.DNS device show lan0192.168.46.1$ nmcli device reapply lan0Connection successfully reapplied to device 'lan0'.$ nmcli -g IP4.DNS device show lan09.9.9.9 | 192.168.46.1$ ip -br addr show lan0lan0  UP  192.168.46.100/24 2001:db8:46::a/64 fe80::...$ nmcli -g IP4.DNS device show lan01.1.1.1 | 192.168.46.1$ nmcli -g ipv4.dns,ipv6.addresses connection show lan0-dhcp9.9.9.9 $ nmcli device up lan0$ ip -br addr show lan0lan0  UP  192.168.46.100/24 fe80::...$ nmcli -g IP4.DNS device show lan09.9.9.9 | 192.168.46.1

26.5 device delete, lldp

  • delete removes a software device (br9) from the kernel.
  • lldp lists LLDP neighbours (needs connection.lldp enable): chassis ID, port, system name and capabilities of the switch/router.
Terminal
nmcli connection add type bridge \  ifname br9 con-name br9 \  ipv4.method disabled ipv6.method disablednmcli device delete br9nmcli device status | grep -c br9# setup: connection.lldp enable (lan0-dhcp)nmcli device lldpnmcli device lldp list ifname lan0
Output
$ nmcli device delete br9Device 'br9' successfully removed.$ nmcli device status | grep -c br90$ nmcli device lldpNEIGHBOR[0].DEVICE:              lan0NEIGHBOR[0].CHASSIS-ID:          9A:8D:F9:1D:6C:40NEIGHBOR[0].PORT-DESCRIPTION:    eth0NEIGHBOR[0].SYSTEM-NAME:         nmlabNEIGHBOR[0].SYSTEM-DESCRIPTION:  gw: dnsmasq DHCP lab routerNEIGHBOR[0].SYSTEM-CAPABILITIES: 156 (mac-bridge,wlan-access-point,router,station-only)

26.6 nmcli device checkpoint: automatic rollback

Wrong IP over SSH, saved by checkpoint
  1. 1.nmcli device checkpoint --timeout 10 lan0 -- <change>

  2. 2.NetworkManager saves the current state of lan0

  3. 3.Wrong address 10.99.9.9/24 is applied; ping from gw fails

  4. 4.Nobody types 'Yes' within 10 s

  5. 5.NetworkManager rolls back: 192.168.46.100/24 (DHCP) returns

  6. 6.Ping from gw works again

Terminal
nmcli device checkpoint --timeout 10 lan0 \  -- sh -c 'nmcli connection modify \  lan0-dhcp ipv4.method manual \  ipv4.addresses 10.99.9.9/24 \  && nmcli device reapply lan0 && sleep 2 \  && ip -br -4 addr show lan0 \  && ip netns exec gw ping -c 1 -W 1 \  192.168.46.100 | tail -2' </dev/nullsleep 12ip -br -4 addr show lan0nmcli -g ipv4.method connection show lan0-dhcpip netns exec gw ping -c 1 -W 1 \  192.168.46.100 | tail -2
Output
Connection successfully reapplied to device 'lan0'.lan0@if9   UP   10.99.9.9/241 packets transmitted, 0 packets received, 100% packet lossType "Yes" to commit the changes:$ ip -br -4 addr show lan0lan0@if9   UP   192.168.46.100/24$ nmcli -g ipv4.method connection show lan0-dhcpauto$ ip netns exec gw ping -c 1 -W 1 192.168.46.100 | tail -21 packets transmitted, 1 packets received, 0% packet loss

26.7 nmcli device wifi and agent

  • The lab has no Wi-Fi card (mac80211_hwsim not built), so wifi commands return 'No Wi-Fi device found' and WIFI-HW is 'missing'.
  • wifi list [--rescan yes|no|auto] [ifname] [bssid]; connect SSID password P [hidden yes] [name ...] [private yes].
  • hotspot [ssid] [password] [band a|bg] [channel N] creates an AP; -s prints its password; device down wlan0 stops it.
  • rescan [ssid HIDDEN] requests a scan; show-password prints the secret and a QR code.
  • agent secret registers nmcli as a secret agent (asks for passwords); agent polkit needs a login session; agent all = both.
Terminal
zcat /proc/config.gz | \  grep -E 'CONFIG_MAC80211(_HWSIM)?[= ]'nmcli device wifi list --rescan yesnmcli device wifi connect MyNetwork \  password s3cretPassnmcli device wifi hotspot ssid MyHotspot \  password hotspot123nmcli device wifi rescannmcli device wifi show-password
Output
CONFIG_MAC80211=m# CONFIG_MAC80211_HWSIM is not set$ nmcli device wifi list --rescan yes(empty)$ nmcli device wifi connect MyNetwork ...Error: No Wi-Fi device found.$ nmcli device wifi hotspot ...Error: No Wi-Fi device found.$ nmcli device wifi show-passwordError: No Wi-Fi device found.
Terminal
timeout 3 nmcli agent secret </dev/nulltimeout 3 nmcli agent polkit </dev/nulltimeout 3 nmcli agent all </dev/null
Output
$ nmcli agent secretnmcli successfully registered as a NetworkManager's secret agent.$ nmcli agent polkitError: polkit agent failed: Could not retrieve session id:... org.freedesktop.login1 was not provided ...$ nmcli agent allnmcli successfully registered as a NetworkManager's secret agent.Error: polkit agent failed: Could not retrieve session id: ...

26.8 Common connection settings

SettingExample / meaning
connection.*id, autoconnect, autoconnect-priority 10, autoconnect-retries 0, zone internal, interface-name, master + slave-type
ipv4.*gateway, dns "8.8.8.8 1.1.1.1", dns-search, +routes "NET GW", route-metric 200, never-default, ignore-auto-dns, may-fail
ipv6.*method manual, addresses, addr-gen-mode stable-privacy, gateway, dns
ethernet.*mtu 1400, cloned-mac-address 02:11:22:33:44:55
wifi.* / wifi-sec.*ssid, band a, hidden yes, mtu, mode; key-mgmt wpa-psk, psk, proto rsn
others802-1x.eap peap, bond.options, vlan.id + vlan.parent, proxy.method
Terminal
nmcli connection modify lan1 connection.id \  office-lannmcli connection modify office-lan \  connection.autoconnect yes \  connection.autoconnect-priority 10 \  connection.autoconnect-retries 0 \  connection.zone internalnmcli connection modify office-lan \  ipv4.gateway 192.168.46.1 \  ipv4.dns "8.8.8.8 1.1.1.1" \  ipv4.dns-search example.comnmcli connection modify office-lan \  +ipv4.routes "10.20.0.0/16 192.168.46.254" \  ipv4.route-metric 200 ipv4.never-default yesnmcli connection modify office-lan \  ipv6.method manual \  ipv6.addresses 2001:db8:46::20/64 \  ipv6.addr-gen-mode stable-privacynmcli connection modify office-lan \  ethernet.mtu 1400 \  ethernet.cloned-mac-address \  02:11:22:33:44:55nmcli connection up office-lanip -br link show lan1ip -br addr show lan1ip route show dev lan1
Output
$ nmcli -f connection.id,...,ipv4.never-default connection show office-lanconnection.id:                   office-lanconnection.autoconnect-priority: 10connection.zone:                 internalipv4.dns:                        8.8.8.8,1.1.1.1ipv4.dns-search:                 example.comipv4.never-default:              yes$ ip -br link show lan1lan1@if10  UP  02:11:22:33:44:55 <BROADCAST,MULTICAST,UP,LOWER_UP>$ ip -br addr show lan1lan1@if10  UP  192.168.46.20/24 2001:db8:46::20/64 fe80::...$ ip route show dev lan110.20.0.0/16 via 192.168.46.254 proto static metric 200192.168.46.0/24 proto kernel scope link src 192.168.46.20 metric 200
Terminal
nmcli connection add type wifi ifname "*" \  con-name HomeWiFi ssid MyNetwork \  wifi-sec.key-mgmt wpa-psk \  wifi-sec.psk s3cretPassnmcli connection modify HomeWiFi \  wifi.band a wifi.hidden yes \  wifi.mtu 1350 wifi-sec.proto rsn
Output
802-11-wireless.ssid:               MyNetwork802-11-wireless.band:               a802-11-wireless.hidden:             yes802-11-wireless.mtu:                1350802-11-wireless-security.key-mgmt:  wpa-psk802-11-wireless-security.proto:     rsn

26.9 nmtui

  • nmtui is a text menu over the same daemon: Edit a connection, Activate a connection, Set system hostname, Radio.
  • Shortcuts: nmtui edit [NAME] = nmtui-edit, nmtui connect [NAME] = nmtui-connect, nmtui hostname = nmtui-hostname.
  • Handy on a server console without a GUI when you don't remember property names.
Terminal
nmtuinmtui edit lan0-dhcp     # = nmtui-edit lan0-dhcpnmtui connect lan0-dhcp  # = nmtui-connectnmtui hostname           # = nmtui-hostname
Output
$ nmtui┌─┤ NetworkManager TUI ├──┐│ Please select an option ││ Edit a connection       ││ Activate a connection   ││ Set system hostname     ││ Radio                   ││ Quit                    ││                    <OK> │└─────────────────────────┘$ nmtui connect lan0-dhcp│ Connection is already active   │

26.10 Environment variables and colours

  • LC_ALL=C / LANG / LC_MESSAGES control the language; use LC_ALL=C in scripts for stable English output.
  • NO_COLOR=1 turns colours off, CLICOLOR_FORCE=1 forces them in a pipe; PAGER and TERM=dumb control paging.
  • Colours are set in /etc/terminal-colors.d/nmcli.scheme (device-activated 1;34 ...); nmcli.disable turns them off.
  • Colour names: connection-*, connectivity-*, device-*, manager-*, permission-*, state-*, wifi-signal-*.
Terminal
LC_ALL=C nmcli -t device status | grep lanLC_MESSAGES=de_DE.UTF-8 nmcli generalNO_COLOR=1 nmcli device status \  | grep lan0 | cat -vCLICOLOR_FORCE=1 nmcli device status \  | grep lan0 | cat -vPAGER=cat nmcli connection showTERM=dumb nmcli connection show | head -2
Output
$ LC_ALL=C nmcli -t device status | grep lanlan0:ethernet:connected:lan0-dhcplan1:ethernet:connected:office-lan$ LC_MESSAGES=de_DE.UTF-8 nmcli generalSTATE      CONNECTIVITY  WIFI-HW  WIFIconnected  full          missing  enabled$ NO_COLOR=1 nmcli device status | grep lan0 | cat -vlan0     ethernet  connected               lan0-dhcp$ CLICOLOR_FORCE=1 nmcli device status | grep lan0 | cat -v^[[32mlan0^[[0m  ^[[32methernet^[[0m  ^[[32mconnected^[[0m ...$ TERM=dumb nmcli connection show | head -2NAME       UUID                                  TYPE      DEVICElan0-dhcp  7630b205-6058-3019-8b34-9c3569f27a9e  ethernet  lan0
Terminal
echo 'device-activated 1;34' \  >> /etc/terminal-colors.d/nmcli.schemeecho 'connection-activated 1;34' \  >> /etc/terminal-colors.d/nmcli.schemeecho 'state-connected-global 1;35' \  >> /etc/terminal-colors.d/nmcli.schemeCLICOLOR_FORCE=1 nmcli device status \  | grep lan0 | cat -vtouch /etc/terminal-colors.d/nmcli.disablenmcli -f DEVICE,STATE device
Output
^[[1;34mlan0^[[0m  ^[[1;34methernet^[[0m  ^[[1;34mconnected^[[0m ...$ touch /etc/terminal-colors.d/nmcli.disable$ nmcli -f DEVICE,STATE device   (real terminal)lan0     connectedlan1     connectedlo       connected (externally)

26.11 Exit status

CodeMeaning
0success
1unknown error
2invalid user input
3timeout expired (--wait)
4connection activation failed
5connection deactivation failed
6device disconnect failed
7connection deletion failed
8NetworkManager not running
10connection / device / AP not found
65--complete-args: file name expected
Terminal
nmcli connection up lan0-dhcp; echo $?nmcli device foo; echo $?nmcli --wait 1 connection up \  'Wired connection 2'; echo $?nmcli connection up HomeWiFi; echo $?nmcli device disconnect gre0; echo $?nmcli connection up nosuch; echo $?pkill NetworkManagernmcli general; echo $?
Output
Connection successfully activated (D-Bus active path: ...)0Error: argument 'foo' not understood. Try passing --help instead.2Error: Timeout expired (1 seconds)3Error: Connection activation failed: No suitable device found for this connection ...4Error: Device 'gre0' (...) disconnecting failed: This device is not active6Error: unknown connection 'nosuch'.10$ nmcli general; echo $?Error: NetworkManager is not running.8

26.12 Common mistakes

  • connection modify and the DNS is still old: modify changes only the profile; run device reapply or connection up (lab: 9.9.9.9 only after reapply).
  • Thinking device modify is permanent: it is gone after device up or a reboot.
  • Changing a remote server's IP without checkpoint and losing the SSH session.
  • Parsing localized, coloured output in scripts instead of using -g / -t with LC_ALL=C.

26.13 Interview tips

  • Safe remote change: nmcli device checkpoint (auto rollback if not confirmed), reapply after a profile change (no drop), check -g output and exit codes.
  • Permanent change = connection modify + device reapply; device modify is temporary.
  • Exit codes worth remembering: 8 = NetworkManager not running, 10 = not found, 3 = timeout.
  • nmtui = same daemon, menu interface.