nmcli cheat sheet

GNU/Linux systems using Network Manager use nmcli to manage the network, hence the name. It differs from ipconfig and iproute2 because it manages the hardware and the configuration files at the same time – in other words all changes are permanent. In Unix you made changes with ifconfig, and if you liked them, added the ifconfig lines to the startup file.

If you’re going to use Network Manager, use it for everything – don’t mix and match or the configuration files will end up out of sync with the actual network interfaces.

TaskUnix standardnmcli etc
List all interfacesifconfignmcli dev status (interfaces)
nmcli con show (profiles)
List a specific interfaceifconfig eth0nmcli dev show eth0
Bring an interface up or downifconfig eth0 up (or down)nmcli con up eth0 (or down)
or nmcli dev connect eth0 / nmcli dev disconnect eth0
Set an IP addressifconfig eth0 192.168.1.10/24nmcli con mod eth0 ipv4.method manual ipv4.addresses 192.168.1.10/24
Configure using DHCPdhclient eth0nmcli con mod eth0 ipv4.method auto
Set an IPv6 addressifconfig eth0 inet6 2001:db8::1/64nmcli con mod eth0 ipv6.method manual ipv6.addresses 2001:db8::1/64
Add an AppleTalk addressifconfig eth0 atalk 45.156Only inet supported by GNU/Linux
Set IP, netmask and broadcastifconfig eth0 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255ip addr add 192.168.1.10/24 broadcast 192.168.1.255 dev eth0
(not persistent, but Network Manager can’t set broadcast)
Add an aliasifconfig eth0 alias 192.168.1.2 netmask 255.255.255.0nmcli con mod eth0 +ipv4.addresses 192.168.1.2/24
Delete a specific IPifconfig eth0 delete 192.168.1.2nmcli con mod eth0 -ipv4.addresses 192.168.1.2/24
Rename an interfaceifconfig eth0 name wan1See notes
Show the routing tablenetstat -rnip route
Add a default routeroute add default 192.168.1.1nmcli con mod eth0 ipv4.gateway 192.168.1.1
Delete a default routeroute delete default 192.168.1.1nmcli con mod eth0 ipv4.gateway ""
Show DNS serverscat /etc/resolv.confnmcli dev show | grep DNS
(plain nmcli also lists them at the end)
Add a DNS serverEdit /etc/resolv.conf: nameserver 8.8.8.8nmcli con mod eth0 +ipv4.dns 8.8.8.8
(add ipv4.ignore-auto-dns yes to drop DHCP-supplied servers)
Delete all DNS serversEdit /etc/resolv.confnmcli con mod eth0 ipv4.dns ""
Set domain search orderEdit /etc/resolv.conf: search example.com local.example.comnmcli con mod eth0 ipv4.dns-search "example.com,local.example.com"
Show listening socketsnetstat -anss -tulnp
Show interface statusnetstat -inmcli dev status
(ip -s link gives more information, more like netstat
Set MTUifconfig eth0 mtu 9000nmcli con mod eth0 ethernet.mtu 9000
View the ARP cachearp -aip neigh show
Delete an ARP entryarp -d 192.168.1.65ip neigh del 192.168.1.65 dev eth0
Delete the ARP cachearp -d -aip neigh flush all

Notes

At present there are many things that can’t be done or set using nmcli, which might come as a surprise. Systems that don’t have ifconfig usually use ip to fill in the gaps. Note that the iproute2 system (the ip command) doesn’t make changes permanent, whereas nmcli updates the myryad configuration files at the same time as changing the settings. If you want to make a temporary change that doesn’t survive a reboot use ip instead.

There are a few things that nmcli doesn’t do – as far as I know and at time of writing. You can’t manipulate the arp cache with it, so have to use the arp command or “ip neigh xxx”.

One curious omission is that Network Manager can’t set an explicit broadcast address on an interface. It calculates it from the IP address and subnet mask. This is probably okay for most recent IP networks but 4.2BSD used the lowest address rather than the highest and SunOS and Ultrix copied it, so it’s necessary to override it to talk with older hosts. Early GNU/Linux used to calculate the broadcast address from the class, so over-riding was sometimes needed.

The same applies to non inet or inet6 protocols. GNU/Linux simply doesn’t support them so iproute2 and nmcli have no way of setting anything other than IPv4 or IPv6. If you want full-on networking you need BSD.

Network Manager has great difficulty renaming an interface. Create /etc/systemd/network/70-wan1.link containing the following lines, with the MAC address set to whatever the interface is using, and then reboot! If anyone knows a better way I’d love to hear it.

[Match]
MACAddress=xx:xx:xx:xx:xx:xx
[Link]
Name=wan1

You can also rename the interface with ip link set eth0 name wan1, but as that doesn’t make it permanent and Network Manager doesn’t know about it, the name might not sick. Perhaps edit the config file and then use ip to set it and hope rather than rebooting immediately.

Philosophy

Whilst iproute2 (the ip command) was a pointless and cumbersome replacement for the ifconfig command, Network Manager does something new. ip is a stateless tool, but NetworkManager is a daemon that manages the network on the system’s behalf. ip talks straight to the kernel over netlink. It changes what is running at that moment, keeps no record of what you did, and doesn’t know or care why an address is there. NetworkManager holds the desired state as connection profiles, each of which complete description of how an interface should be set up: addressing, routes, DNS, MTU, DHCP behaviour and so on. It then works continuously to make the kernel match the profile. Because a profile isn’t tied to a device, one interface can have several (office, home, a static fallback), and you switch between them with a single nmcli con up. When a cable is plugged in, a DHCP lease renews or a USB adapter appears, NetworkManager reapplies the right profile without any help. This is similar to rc.conf combined with /etc/rc.d/netif running all the time, rather than only at boot.

This is also why you shouldn’t make changes with ip on a host running Network Manager. Anything you do behind its back isn’t in a profile, so it vanishes on the next reboot or even network reconnect. Worse, Network Manager may overwrite it straight away or lose track of the interface’s actual state. Going through nmcli keeps one source of truth, and it covers the things iproute2 could never configure anyway, like DNS and /etc/resolv.conf, the DHCP client, Wi-Fi, VPNs, bonds, bridges and VLANs. On systems like RHEL it isn’t really optional anyway. The old network-scripts (ifup/ifdown) were deprecated in RHEL 8 and removed in RHEL 9, along with the ifcfg file format, which gave way to Network Manager’s own config files. More files is better, right? So nmcli is now the supported way to configure the network. ip is still fine for looking around, and for temporary changes you’re happy to lose.

Linux Network Cheat Sheet

This is an rewrite of a much older post.

Some Linux distributions use a system called iproute2 to configure network interfaces, which is a more limited version of the standard Unix system based around ifconfig. The original Linux rewrite of ifconfig was less capable, so rather than bringing it up to Unix standards it was thrown out and a new one written. This lists common tasks that are possible of both, and their equivalents. NB. “eth0” is any network interface in the format your system uses.

TaskUnix standardLinux iproute2
List all interfacesifconfigip addr
List specific interfaceifconfig eth0ip addr show dev eth0
Bring up/down interfaceifconfig eht0 up (or down)ip link set eth0 up (or down)
Set IP addressifconfig eth0 192.168.1.10/24ip addr add 192.168.1.10/24 dev eth0
Configure using DHCPdhclient eth0dhclient eth0
Set IPv6 address
(syntax works with add/delete/alias etc)
ifconfig eth0 inet6 2001:db8::1 prefixlen 64
or
ifconfig eth0 inet6 2001:db8::1/64
ip -6 addr add 2001:db8::1/64 dev eth0
Add appletalk addressifconfig eth0 atalk 45.156ip --appletalk addr add 45.156 dev eth0
Set IP + netmask + broadcast (where defaults not suitable)ifconfig eth0 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255ip addr add 192.168.1.10/24 broadcast 192.168.1.255 dev eth0
Add alias (not explicit on Linux)ifconfig eth0 alias 192.168.1.2 netmask 255.255.255.0ip addr add 192.168.1.2/24 dev eth0
Delete specific IP (not possible on old Linux)ifconfig eth0 delete 192.168.1.2ip addr del 192.168.1.2/24 dev eth0
Rename interfaceifconfig eht0 name wan1ip link set eth0 down
ip link set eth0 name wan1
ip link set wan1 up
Show routing tablenetstat -rnip route
Add default routeroute add default 192.168.1.1ip route add default via 192.168.1.1
Delete default routeroute delete default 192.168.1.1ip route del default via 192.168.1.1
Show DNS serverscat /etc/resolv.confresolvectl status
Add DNS serverEdit /etc/resolv.conf:
nameserver 8.8.8.8
resolvectl dns eth0 8.8.8.8
Delete all DNS serversEdit /etc/resolv.confresolvectl dns eth0 ""
Set domain search orderEdit /etc/resolv.conf:
search example.com local.example.com
resolvectl domain eth0 example.com local.example.com
Show listening socketsnetstat -anss -tulnp
Show interface statusnetstat -iip -s link
Set mtuifconfig eth0 mtu 9000ip link set dev eth0 mtu 9000
View ARP cachearp -aip neigh show
Delete ARP entryarp -d 192.168.1.65ip neigh del 192.168.1.65 dev eth0
Delete ARP cachearp -d -aip neigh flush all

Notes

Old Linux ifconfig couldn’t remove a specific IP address but could remove them all using ifconfig eth0 0.0.0.0. You can get the same effect on iproute2 using “ip addr flush dev eth0”. Unix doesn’t have a command that’s quite so destructive.

If you add an alias address on the same subnet as an existing IP address, give it a netmask of /32.

Old Linux produced a routing table using route -n (not -rn) and you’d need to use “gw” when adding or deleting one (e.g. route add default gw 192.168.1.1)

On Solaris you need to add a -p between “route” and “add” – e.g. route -p add default 192.168.1.1

Most versions of ifconfig on Unix systems accept CIDR notation as an alternative to specifying “netmask 255.255.255.255” – for example “192.168.1.1/24“

The ip command will usually infer which are IPv4 and IPv6 addresses but it can be made explicit using “ip -4” or “ip -6”. Likewise ifconfig normally doesn’t require “inet” or “inet6” to figure out which kind of IP address you’re giving it. -6 and inet6 have been used above to be 100% explicit. Linux supports fewer interface types than Unix.

It is not possible to delete or edit a single DNS server using the linux ip system, but you can delete them all and add back the ones you want.

On Linux, ip can only configure interfaces for IPv4 and IPv6 (i.e. it won’t support AppleTalk, ATM, Token Ring, or IPX etc.).

Making changes persist through boot

To configure an interface on boot on BSD use sysrc ifconfig_eth0="DHCP", for a DHCP address or edit rc.conf similarly. For a static address use ifconfig_bge0="192.168.1.123/24” and defaultrouter=”192.168.1.1” and add the nameservers to /etc/resolv.conf with a line like “nameserver 192.168.1.2” For Linux it’s much more complicated using other files or databases.

Debian/Ubuntu older versions

edit /etc/network/interfaces:

auto eth0
iface eth0 inet dhcp
For static address use:
auto eth0
iface eth0 inet static
address 192.168.1.123/24
gateway 192.168.1.1
dns-nameservers 192.168.1.2 192.168.1.3

Ubuntu with Netplan

edit /etc/netplan/01-netcfg.yaml (or similar):

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: true

For static address use:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: false
      addresses: [192.168.1.123/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.2, 192.168.1.3]

Red Hat/CentOS/Fedora (using network-scripts, older versions):

Edit /etc/sysconfig/network-scripts/ifcfg-eth0:

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

For static address use:

DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.123
PREFIX=24
GATEWAY=192.168.1.1
DNS1=192.168.1.2
DNS2=192.168.1.3

Systems with NetworkManager

(e.g., Fedora, Ubuntu desktop), try ip first but if you have to, use nmcli:

sudo nmcli con mod "Wired connection 1" ipv4.method auto
sudo nmcli con up "Wired connection 1"

For static address use:

sudo nmcli con mod "Wired connection 1" ipv4.method manual ipv4.addresses 192.168.1.123/24 ipv4.gateway 192.168.1.1 ipv4.dns "192.168.1.2 192.168.1.3"
sudo nmcli con up "Wired connection 1"

Replace “Wired connection 1” with your connection name (list with “nmcli con show”, get lots of stuff to tweak with “nmcli con show eth0”. You may also use the name of the ethernet interface when you find it.

Systems with systemd-networkd:

Create or edit /etc/systemd/network/20-wired.network:

[Match]
Name=eth0

[Network]
DHCP=yes

For static address use:

[Match]

Name=eth0

[Network]
Address=192.168.1.123/24
Gateway=192.168.1.1
DNS=192.168.1.2
DNS=192.168.1.3

Systemd Network Configuration

Unless you’ve been living in a very Linux-free environment for a while, you’ll know about systemd – the collection of daemons intended to replace the System V init system commonly found on Linux, with something more complicated. I’m not a fan of System V startup, but they might have done better by going for the Research Unix or BSD /etc/rc approach for robustness, simplicity and compatibility. But Linux, to many, is a launcher stub for graphical desktops running LibreOffice and games, and these probably work better with systemd syntax when controlled by a simple GUI.

Systemd is more than an init system – in fact it has daemons from everything from the keyboard to DNS resolution – and network interface configuration (networkd)

This nightmare came out of Red Hat, and Linux distributions like Debian, Ubuntu, openSUSE, Arch, and their derivatives have started using it. One result, amongst other things, is that it’s suddenly not possible to configure networks the way you used to using ifconfig and /etc/resolv.conf.

You can install the missing ifconfig and suchlike using a package called net-tools, which is present on most major Linux distributions and is installed in the appropriate way (dnf, apt, yum etc). This may be the best way to keep scripts working.

Otherwise, you might be hoping systemd-networkd has simplified things, with less to type. But I’m afraid not.

So for those who are struggling, here’s a cheat sheet.

Names

The first think you’ll have to remember is that systemd-networkd doesn’t call your Ethernet interfaces eth0:, eth1. It doesn’t even call them by their driver name+enum BSD style. Instead it mungs a name from indices provided by the firmware, PCIe slot number and even the MAC address. Look out for some very strange interface names.

The idea is that the NIC/port has a predictable name, which is great in theory. I can see two problems: Firstly this doesn’t really help you find the RJ45 any better unless you have a schematic. Secondly, if you pull the system from one host and put it in another it all goes to hell in a handcart anyway. On the plus side I guess it means that adding or removing a NIC isn’t going to change the name of the existing ports.

For what it’s worth, eno# is an onboard device, ens# is a PCI slot index, enp#s# is port number on NIC and PCI slot index. enx1122334455667788 is the MAC address but this behaviour seems to be turned off on most systems. If it can’t determine anything it will fall back to eth#.

There are ways of selecting the old behaviour using kernel parameters or knobbling the /etc/systemd/network/… something “default” depending on system but you should check that out in the man page. Oh, hang on, this is Linux there probably no man pages.

Cheat Sheet

OldNew
ifconfig eth0 192.168.1.2/24ip addr add 192.168.1.2/24 dev eth0
ifconfig eth0 192.168.1.2 deleteip addr del 192.168.1.2/24 dev eth0
ifconfig eth0 netmask 255.255.255.0? Set address and netmask together ?
ifconfig eth0 mtu 5000ip link set eht0 mtu 5000
ifconfig eth0 down (or up)ip link set eth0 down (or up)
ifconfigip a
netstatss
netstat -rip route show
routeip r
route add default 192.168.1.254ip route add default via 192.168.1.254
arp -aip n
ifconfig eht0 name wan1? Not possible from command line ?

The last entry in the table is about renaming an interface, which given the user-hostile names now generated is even more useful. I haven’t figured out how to do this from the command line, but the assumption is that all interface configuration is done in configuration files by default, which brings us neatly on to these.

Configuring at startup

At one time you could just edit /etc/network/interfaces, and it might still work (it does int he latest Debian, for example). In BSD stick simple definitions in rc.conf, but that’s too easy. Anyway, /etc/network/interfaces could look something like this:

auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.253

auto eth1
iface eth1 inet dhcp

After editing the configuration files(s) you could restart:

/etc/init.d/networking [start | stop | restart]

But some systemd Linux distributions are different. Systemd-networkd has a directory tree full of configuration stuff and I can only scratch the surface here.

Basically a load of *.network stored in /etc/systemd/network/ get run in sort order. It’s normal to prefix each file with two digits and a dash to set this order. I don’t think there’s any reason not to use a single file, but in the Linux world people don’t, often choosing to make the rest of the filename the NIC name, such as “04-enp0s5.network“, although the name you choose is only for your reference (or that of some GUI configuration tool).

To force every NIC to configure using dhcp create a file 02-dhcpall.network:

[Match]
Name=en*
[Network]
DHCP=yes

Note the wildcard on the NIC Name=*

On the other hand if you want to make one specific card static, have a file which you might want to call 01-enp5s2.network:

[Match]
Name=enp5s2
 
[Network]
Address=192.168.1.2/24
Gateway=192.168.1.254
DNS=192.168.1.254 8.8.8.8
Domains=example.com test.example.com

This should be fairly self-explanatory. You can specify multiple Address= lines (aliases) but for some reason DNS servers tend to be listed on one line, although multiple lines do work in my experience. I’ve used IPv4 in the examples but IPv6 works too.

Domains=example.com test.example.com is basically the DNS search domains (as normally found in resolv.conf). As systemd has its own resolver, systemd-resolved, it’s not just a matter of edit one file any longer, and is also less flexible.

You can restart systemd-networkd with:

systemctl restart systemd-networkd

If you haven’t made any mistakes you might still be connected to your server.