How to Use the Command 'nmcli device' (with examples)
The nmcli device
command serves as a powerful tool for managing network interfaces on Linux-based systems using NetworkManager. It provides functionalities for querying the status of network interfaces, managing Wi-Fi connections, and even sharing details of current network connections. These operations allow users to fine-tune their connectivity and quickly resolve networking issues without needing a graphical interface. By offering both ease of use and versatility, nmcli device
proves invaluable for system administrators and tech-savvy individuals alike.
Use Case 1: Print the statuses of all network interfaces
Code:
nmcli device status
Motivation:
Understanding the status of all network interfaces on a device is crucial for diagnosing connectivity issues and performing network management tasks. It allows users to quickly identify active, disconnected, or unavailable interfaces, which can directly impact the system’s network performance. This command is particularly useful in troubleshooting scenarios where multiple interfaces (such as Ethernet, Wi-Fi, and Bluetooth) might interact, create conflicts, or undergo changes unnoticed by the user.
Explanation:
nmcli
: The command-line interface for interacting with NetworkManager, primarily used for controlling the network connections.device
: Subcommand that specifies the focus on physical and virtual network interfaces.status
: An argument that requests a summary of the current state of all network interfaces, displaying essential details, such as device name, type, and current state.
Example output:
DEVICE TYPE STATE CONNECTION
eth0 ethernet connected Wired connection 1
wlan0 wifi connected Home_WiFi
lo loopback unmanaged --
Use Case 2: Print the available Wi-Fi access points
Code:
nmcli device wifi
Motivation:
Listing available Wi-Fi networks is a common need for users looking to connect to a nearby wireless access point, whether at home, at work, or in public spaces. This command helps users scan for Wi-Fi networks within range, providing a list of detected access points along with valuable information like signal strength and security type. It is ideal for choosing the best network to connect to based on signal quality, which can affect internet speed and reliability.
Explanation:
nmcli
: The tool that provides access to NetworkManager functionalities via the command line.device
: Specifies that subsequent operations relate to network devices and their management.wifi
: Focuses the query specifically on wireless network interfaces and the available Wi-Fi networks they can detect.
Example output:
SSID MODE CHAN RATE SIGNAL BARS SECURITY
Home_WiFi Infra 6 130 Mbit/s 72 ▂▄▆_ WPA2
Guest_Network Infra 11 130 Mbit/s 62 ▂▄__ WPA2
CoffeeShop Infra 1 195 Mbit/s 52 ▂▄__ WEP
Use Case 3: Connect to a Wi-Fi network with the specified SSID
Code:
nmcli --ask device wifi connect ssid
Motivation:
Connecting to a specific Wi-Fi network is a regular task for users when joining wireless networks at home, work, or public places. This command not only connects to the specified network but does so interactively by prompting for a password if necessary. This approach enhances security and convenience by allowing users to enter credentials directly through the terminal, thus establishing a wireless connection seamlessly.
Explanation:
nmcli
: The powerful command-line interface for managing network connections.--ask
: A flag that prompts the user to input required information, such as a password, interactively.device
: This subcommand indicates that the operation concerns network interfaces, particularly Wi-Fi in this case.wifi
: Highlights that operations concern wireless network settings.connect
: Directs NetworkManager to establish a connection to a specified network, based on the SSID provided.ssid
: Stands for Service Set Identifier, the unique name that identifies the Wi-Fi network the user wishes to join.
Example output:
Password:
Device 'wlan0' successfully activated with '171b9ca0-c5a0-4c3e-908d-efd4b8be3000'.
Use Case 4: Print the password and QR code for the current Wi-Fi network
Code:
nmcli device wifi show-password
Motivation:
Sharing Wi-Fi credentials with others can often lead to security risks or inconvenience if done improperly. This command offers a secure method to display the current network’s password along with a QR code that can be easily scanned by others for quick access, without requiring direct input of the sensitive password. This is especially helpful in settings such as offices or events, where multiple users need to join the network without hassle.
Explanation:
nmcli
: The command interface that accesses and manages network functionalities facilitated by NetworkManager.device
: A directive that refers to network interfaces and their current configuration.wifi
: Specifies the wireless network interface and operations that affect Wi-Fi settings.show-password
: An instruction to reveal the network password in use by the device, often accompanied by a QR code for secure sharing.
Example output:
SSID: Home_WiFi
Password: superSecretPassword
Scan this QR code to connect to the network:
█████████████████████████████
█████████████████████████████
████ ▄▄▄▄▄ █▄ ▀ ███ ▄▄▄▄▄ ███
████ █ █ █▄▀▄ █ █ █ ███
████ █▄▄▄█ █▄ ▀▄█▀█ █▄▄▄█ ██
████▄▄▄▄▄▄▄█▄█▄█▄█▄▄▄▄▄▄█ ██
████ ▄▄▀▄▀▄ ▀ █▀▄▄▄ █ ▄▄▄ ▀█
█████████████████████████████
Conclusion:
The nmcli device
command offers a suite of tools for managing and exploring network connectivity directly from the terminal. With functionalities ranging from displaying the status of network interfaces to connecting to Wi-Fi networks and securely sharing credentials, it empowers users to maintain control over their network experience with ease and flexibility. Whether you’re diagnosing connectivity issues or optimizing your wireless settings, nmcli device
provides comprehensive solutions tailored to a diverse array of networking needs.