nmcli ‐ #networking - five4nets/Linux-Knowledgebase GitHub Wiki
nmcli
Command in Linux
Tutorial: Using the nmcli
is a command-line tool for controlling NetworkManager in Linux, allowing users to manage network connections, interfaces, and devices. This tutorial covers nmcli
basics, common commands, and practical examples.
Prerequisites
- Linux system with NetworkManager installed (
sudo systemctl status NetworkManager
to verify). - Basic terminal knowledge.
- Root or sudo privileges for some commands.
nmcli
Overview of nmcli
interacts with NetworkManager to configure Wi-Fi, Ethernet, VPN, and other connections. It’s script-friendly, supports tab completion, and provides detailed network status.
Basic Syntax
nmcli [OPTIONS] OBJECT { COMMAND | help }
- OBJECTS:
connection
,device
,general
,radio
, etc. - COMMANDS:
show
,up
,down
,add
,modify
,delete
, etc.
nmcli
Commands and Examples
Common 1. Viewing Network Status
Display general NetworkManager status:
nmcli general status
Output Example:
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
connected full enabled enabled enabled disabled
List all network devices and their status:
nmcli device status
Output Example:
DEVICE TYPE STATE CONNECTION
wlan0 wifi connected MyWiFi
eth0 ethernet disconnected --
lo loopback unmanaged --
List all configured connections:
nmcli connection show
Output Example:
NAME UUID TYPE DEVICE
MyWiFi 123e4567-e89b-12d3-a456-426614174000 wifi wlan0
Wired 987e6543-e21b-34d5-b678-426614174001 ethernet --
2. Managing Wi-Fi Connections
Scan for available Wi-Fi networks:
nmcli device wifi list
Output Example:
IN-USE SSID MODE CHAN RATE SIGNAL BARS SECURITY
* MyWiFi Infra 6 54 Mbit/s 85 ▂▄▆█ WPA2
GuestWiFi Infra 11 54 Mbit/s 60 ▂▄▆_ WPA2
Connect to a Wi-Fi network (replace SSID
and password
):
nmcli device wifi connect SSID password "your_password"
Example:
nmcli device wifi connect MyWiFi password "Secret123"
Note: Add hidden yes
if the SSID is hidden:
nmcli device wifi connect MyWiFi password "Secret123" hidden yes
3. Creating a New Connection
Add a new Wi-Fi connection profile:
nmcli connection add type wifi con-name MyHomeWiFi ssid MyWiFi autoconnect yes ifname wlan0
nmcli connection modify MyHomeWiFi wifi-sec.key-mgmt wpa-psk wifi-sec.psk "Secret123"
con-name
: Connection name.ifname
: Interface name (e.g.,wlan0
).autoconnect
: Automatically connect at boot.
Add a static Ethernet connection:
nmcli connection add type ethernet con-name MyEth ifname eth0 autoconnect yes ip4 192.168.1.100/24 gw4 192.168.1.1
nmcli connection modify MyEth ipv4.dns "8.8.8.8,8.8.4.4"
4. Activating/Deactivating Connections
Activate a connection:
nmcli connection up MyHomeWiFi
Deactivate a connection:
nmcli connection down MyHomeWiFi
5. Modifying Connections
Change Wi-Fi password:
nmcli connection modify MyHomeWiFi wifi-sec.psk "NewSecret456"
Set a static IP address:
nmcli connection modify MyEth ipv4.addresses 192.168.1.101/24 ipv4.method manual
nmcli connection up MyEth
6. Deleting a Connection
Remove a connection profile:
nmcli connection delete MyHomeWiFi
7. Managing NetworkManager Radio
Enable/disable Wi-Fi radio:
nmcli radio wifi on
nmcli radio wifi off
Check radio status:
nmcli radio
Output Example:
WIFI-HW WIFI WWAN-HW WWAN
enabled enabled enabled disabled
8. Monitoring Network Activity
Monitor connection changes in real-time:
nmcli monitor
Output Example:
wlan0: connected to MyWiFi
Networkmanager is now in the 'connected' state
Advanced Example: Script for Wi-Fi Connection
Create a script to connect to a Wi-Fi network or fall back to a secondary one:
#!/bin/bash
SSID1="MyWiFi"
PASS1="Secret123"
SSID2="GuestWiFi"
PASS2="Guest456"
nmcli device wifi connect "$SSID1" password "$PASS1" || nmcli device wifi connect "$SSID2" password "$PASS2"
Save as wifi_connect.sh
, make executable (chmod +x wifi_connect.sh
), and run (./wifi_connect.sh
).
Troubleshooting
- Connection fails: Check interface status (
nmcli device status
) or logs (journalctl -u NetworkManager
). - Permission denied: Use
sudo
for privileged commands. - Wi-Fi not found: Ensure radio is enabled (
nmcli radio wifi on
) and rescan (nmcli device wifi list
).