Linux nmcli command
What is nmcli
nmcli is the command-line tool for NetworkManager, used to configure and manage network connections on 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 NetworkManager features
- Script-friendly: Suitable for automation scripts and remote management
- No dependency: 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: General NetworkManager status and operationsnetworking: Overall network controlradio: Wireless network switchconnection: Network connection configurationdevice: Network device management
Common OPTIONS
| Option | Description |
|---|---|
-t |
Concise output (suitable for script processing) |
-p |
Pretty output (more readable) |
-h |
Display help information |
-v |
Display version information |
Details of common nmcli commands
1. View network status
View the overall status of NetworkManager:
Example
nmcli general status
Example output:
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
connected full enabled enabled enabled enabled
2. Manage network connections
List all network connections:
Example
nmcli connection show
Activate/deactivate a connection:
Example
nmcli connection up
"connection name"
nmcli connection down
"connection name"
3. Manage network devices
List all network devices:
Example
nmcli device status
Example output:
DEVICE TYPE STATE CONNECTION
eth0 ethernet 已连接 有线连接 1
wlan0 wifi 已断开 --
lo loopback 未托管 --
View detailed device information:
Example
nmcli device show eth0
Wireless network management
Scan for available WiFi networks
Example
nmcli device wifi list
Connect to a WiFi network
Example
nmcli device wifi connect
"SSID name"
password
"password"
Create a new WiFi connection profile
Example
nmcli connection add
type
wifi ifname wlan0 con-name
"My WiFi"
ssid
"SSID name"
nmcli connection modify
"My WiFi"
wifi-sec.key-mgmt wpa-psk
nmcli connection modify
"My WiFi"
wifi-sec.psk
"password"
Wired network configuration
Create a static IP connection
Example
nmcli connection add
type
ethernet ifname eth0 con-name
"Static Connection"
\
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"
Change an existing connection to DHCP
Example
nmcli connection modify
"connection name"
ipv4.method auto
Advanced usage
Create a VPN connection
Example
nmcli connection add
type
vpn vpn-type openvpn \
con-name
"My VPN"
ifname tun0 \
vpn.data
"remote=vpn.example.com, username=user, password=pass"
Create a network bridge
Example
nmcli connection add
type
bridge ifname br0 con-name
"My Bridge"
nmcli connection add
type
bridge-slave ifname eth0 master br0
Create a VLAN
Example
nmcli connection add
type
vlan ifname eth0.100 con-name
"VLAN100"
\
dev eth0
id
100
ip4 192.168.100.100
/
24
Common Problem Solutions
1. Network device shows "unmanaged"
Example
Check NetworkManager.conf
cat
/
etc
/
NetworkManager
/
NetworkManager.conf
If you see an unmanaged-devices setting, you can comment out the relevant lines
Then restart NetworkManager
systemctl restart NetworkManager
2. Forget the connection password
Example
nmcli connection show
"connection name"
|
grep
psk
3. Reset the network configuration
Example
nmcli connection reload
Practical Exercise
- Use nmcli to connect to your WiFi network
- Create a wired connection with a static IP
- Scan for and list all available WiFi networks
- View details of the currently active connections
Summary
nmcli is a powerful tool for network management on Linux. Through this article, you should be able to:
- Understand the basic concepts and syntax of nmcli
- Use nmcli to manage wired and wireless network connections
- Configure static IP and DHCP connections
- Handle common network connection problems
Mastering nmcli lets you manage networks efficiently in server environments without a GUI, and it is one of the important skills for a Linux system administrator.
Linux Command Reference