Linux nmcli command
What is nmcli
nmcli is the command-line tool for NetworkManager, used to configure and manage network connections in Linux systems. It is the official client of NetworkManager and provides more powerful and flexible network management capabilities than the graphical interface.
Main features of nmcli
- Full-featured support: covers almost all functions of NetworkManager
- Script-friendly: suitable for automation scripts and remote management
- No dependencies: no graphical interface required, pure command-line operation
- Real-time feedback: immediately displays command execution results
nmcli basic syntax
The basic command format of nmcli is as follows:
nmcli [OPTIONS] OBJECT { COMMAND | help }
Common OBJECT types
general: NetworkManager general status and operationsnetworking: overall network controlradio: wireless network switchconnection: network connection configurationdevice: network device management
Common OPTIONS options
| Option | Description |
|---|---|
-t |
Concise output (suitable for script processing) |
-p |
Pretty output (more readable) |
-h |
Show help information |
-v |
Show version information |
Detailed explanation of common nmcli commands
1. View network status
View the overall status of NetworkManager:
Example
Sample output:
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN connected full enabled enabled enabled enabled
2. Manage network connections
List all network connections:
Example
Activate/deactivate a connection:
Example
nmcli connection down "connection name"
3. Manage network devices
List all network devices:
Example
Sample output:
DEVICE TYPE STATE CONNECTION eth0 ethernet 已连接 有线连接 1 wlan0 wifi 已断开 -- lo loopback 未托管 --
View detailed device information:
Example
Wireless network management
Scan available WiFi networks
Example
Connect to a WiFi network
Example
Create a new WiFi connection profile
Example
nmcli connection modify "MyWiFi" wifi-sec.key-mgmt wpa-psk
nmcli connection modify "MyWiFi" wifi-sec.psk "password"
Wired network configuration
Create a static IP connection
Example
ip4 192.168.1.100/24 gw4 192.168.1.1
nmcli connection modify "static connection" ipv4.dns "8.8.8.8,8.8.4.4"
Modify an existing connection to DHCP
Example
Advanced usage
Create a VPN connection
Example
con-name "MyVPN" ifname tun0 \
vpn.data "remote=vpn.example.com, username=user, password=pass"
Create a bridge
Example
nmcli connection add type bridge-slave ifname eth0 master br0
Create a VLAN
Example
dev eth0 id 100 ip4 192.168.100.100/24
Common problem solving
1. Network device shows "unmanaged"
Example
cat /etc/NetworkManager/NetworkManager.conf
# If you see the unmanaged-devices configuration, you can comment out the relevant lines
# Then restart NetworkManager
systemctl restart NetworkManager
2. Forgot connection password
Example
3. Reset network configuration
Example
Practice exercises
- Use nmcli to connect to your WiFi network
- Create a wired connection with a static IP
- Scan and list all available WiFi networks
- View detailed information about the currently active connection
Summary
nmcli is a powerful tool for Linux system network management. Through this article, you should be able to:
- Understand the basic concepts and syntax structure of nmcli
- Use nmcli to manage wired and wireless network connections
- Configure static IP and DHCP connections
- Handle common network connection problems
Mastering nmcli allows you to efficiently manage networks in server environments without a graphical interface, and it is also one of the important skills for Linux system administrators.
Other extensions
Linux Command Encyclopedia