Network Manager Useful Commands

Preamble

Network Manager is great and is an unsung hero of our modern world.

Some Basics

See which connections you have already setup…

sudo nmcli c s

See details of a given connection…

sudo nmcli c s my_wifi_connection

Any of the attributes you see here can then be modified with…

sudo nmcli c m my_wifi_connection group.my_attribute value

See which WiFi networks are available using…

sudo nmcli dev wifi list

Establishing Connections with Static IPs

Identify Network Interface Names

First we need to determine the name of our network adapter.

sudo nmcli dev

…returns something similar to this.

DEVICE             TYPE      STATE                   CONNECTION      
wlp2s0             wifi      connected               Pancakes        
docker0            bridge    connected (externally)  docker0         
enp0s31f6          ethernet  unmanaged               --              
lo                 loopback  unmanaged               -- 

Here we can see we have an Ethernet and Wireless interface on enp0s31f6 and wlp2s0 respectively, depending on the type of connection you want to use you’ll need the relevant interface ID.

Wired Connections with a Static IP

To establish a wired connection…

sudo nmcli connection add type ethernet con-name "&%connection-name%&" ifname "&%if-name%&" ip4 "&%ipv4-address/subnet-mask%&"

Note the slash format subnet mask after the IP address, rather than the explicit style.

/24 = 255.255.255.0

/16 = 255.255.0.0

Example

sudo nmcli connection add type ethernet con-name my_wired_connection ifname enp0s31f6 ip4 172.31.1.137/24

WiFi Connection with a Static IP

The general form of the command used is as follows…

sudo nmcli con add con-name "&%connection-name%&" type wifi ifname "&%if-name%&" ssid "&%ssid-name%&" -- wifi-sec.key-mgmt wpa-psk wifi-sec.psk "&%pass-phrase%&" ipv4.method manual ipv4.address "&%ipv4-address/subnet-mask%&"

Same as with the wired example, note the slash format subnet mask (see above).

Example

sudo nmcli con add con-name my_wifi_connection type wifi ifname wlp2s0 ssid MY_WIFI_SSID -- wifi-sec.key-mgmt wpa-psk wifi-sec.psk my_wifi_password ipv4.method manual ipv4.address 172.31.1.137/24

Auto Connect

By default all network connections turn autoconnect on, so that if possible Network Manager will try and connect to that network. However in some cases this is not desirable and can be turned off using…

sudo nmcli c m my_wifi_connection connection.autoconnect no

If you want to use the auto connect functionality you might want to set the priority value if you have multiple networks available and you’d like to set preferred connections. Priority can have any integer value with 0 being the lowest priority and higher values having higher priority, being preferred.

sudo nmcli c m my_wifi_connection connection.autoconnect-priority 0

Somewhat linked to this is the number of retries that auto connect should attempt, this can be set as follows with -1 being infinite.

sudo nmcli c m my_wifi_connection connection.autoconnect-retries -1

You may also want to establish a secondary connection, such as a VPN after establishing an initial gateway connection. To do this you will first need the UUID of the secondary connection you wish to establish. You can get this using…

sudo nmcli c s

Which will return something like…

NAME             UUID                                  TYPE       DEVICE          
docker0          1f342a15-3531-4b4a-a0af-7c1536afbdfc  bridge     docker0         
eduroam          d0dbf8ea-781d-4cd1-992b-7ff9d9c9c763  wifi       --              
wg               9d5c8890-9359-4b03-b03f-8f3d13a5664c  wireguard  -- 

Here I have a wireless network, eduroam, along with a WireGuard VPN connection, wg, I want to automatically connect whenever eduroam connects.

To do this we can add wg as a secondary of eduroam as follows…

sudo nmcli c m eduroam connection.secondaries 9d5c8890-9359-4b03-b03f-8f3d13a5664c

Multiple IP Addresses

It is also possible to give yourself multiple IP addresses…

nmcli c m my_wifi_connection ipv4.addresses "172.31.2.10/16, 172.31.1.137/24"

Sharing an Internet Connection

Check out this guide on sharing an internet connection over a LAN if you’d like to try that.

Hey you!

Found this useful or interesting?

Consider donating to support.

Any question, comments, corrections or suggestions?

Reach out on the social links below through the buttons.