How to use the command nmcli device (with examples)
The nmcli device
command is used to manage network interfaces and establish new Wi-Fi connections using NetworkManager. It provides various options and can be useful for network configuration and troubleshooting.
Use case 1: Print the statuses of all network interfaces
Code:
nmcli device status
Motivation: This use case is helpful when you want to quickly check the status of all network interfaces on your system. It provides information about whether a network interface is connected, the IP address assigned, and other relevant details.
Explanation: The status
argument is provided to the nmcli device
command to specify the task of printing the statuses of all network interfaces.
Example output:
DEVICE TYPE STATE CONNECTION
eth0 ethernet connected Wired connection 1
wlan0 wifi disconnected --
lo loopback unmanaged --
In the example output, you can see the names of the devices (eth0
, wlan0
, and lo
), their types (ethernet
, wifi
, and loopback
), their connection states (connected
, disconnected
, and unmanaged
), and the associated network connections if any (Wired connection 1
).
Use case 2: Print the available Wi-Fi access points
Code:
nmcli device wifi
Motivation: This use case is useful when you need to view the available Wi-Fi access points in your vicinity. It can help you identify different Wi-Fi networks, their signal strengths, and other relevant information.
Explanation: The wifi
argument is provided to the nmcli device
command to specify that we want to get information about available Wi-Fi access points.
Example output:
IN-USE SSID MODE CHAN RATE SIGNAL BARS SECURITY
MyWiFiNetwork Infra 1 135 Mbps 45 ▂▄▆_ WPA2
AnotherNetwork Infra 6 300 Mbps 75 ▂▄▆█ WPA1 WPA2
GuestNetwork Infra 11 54 Mbps 25 ▂___ Open
In the example output, you can see the SSID (network name), mode, channel, transmission rate, signal strength, signal bars, and security type.
Use case 3: Connect to a Wi-Fi network with the specified SSID (you will be prompted for a password)
Code:
nmcli --ask device wifi connect ssid
Motivation: This use case is helpful when you want to connect to a specific Wi-Fi network. By providing the SSID of the network, you can easily initiate a connection and input the password when prompted.
Explanation: The --ask
option is added to the command to prompt the user for the Wi-Fi password. The connect
argument is used to specify that we want to connect to a Wi-Fi network with the given SSID.
Example output:
Password: ******
Device 'wlan0' successfully activated with 'MyWiFiNetwork'.
In the example output, the user is prompted to enter the Wi-Fi password. Once entered correctly, the connection is established, and the name of the activated device along with the connected network’s SSID is displayed.
Use case 4: Print the password and QR code for the current Wi-Fi network
Code:
nmcli device wifi show-password
Motivation: This use case is useful when you want to view the password for the currently connected Wi-Fi network. It can be handy for sharing the password with others or for reference purposes.
Explanation: The show-password
argument is added to the nmcli device wifi
command to display the password for the current Wi-Fi network.
Example output:
MyWiFiNetwork:
password: MyPassword123
QR-Code: █████████████████████
In the example output, you can see the SSID of the current Wi-Fi network (MyWiFiNetwork
), its associated password (MyPassword123
), and a QR code representation of the network.
Conclusion:
The nmcli device
command provides a versatile set of options to manage network interfaces and handle Wi-Fi connections using NetworkManager. By utilizing these use cases, you can easily retrieve information, connect to Wi-Fi networks, and troubleshoot network-related issues.