Linux nmcli command

Linux 命令大全Linux Command Encyclopedia


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 operations
  • networking: overall network control
  • radio: wireless network switch
  • connection: network connection configuration
  • device: 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

nmcli general status

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

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

Sample 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 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 "MyWiFi" ssid "SSID name"
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

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"

Modify 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 "MyVPN" ifname tun0 \
vpn.data "remote=vpn.example.com, username=user, password=pass"

Create a bridge

Example

nmcli connection add type bridge ifname br0 con-name "MyBridge"
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 solving

1. Network device shows "unmanaged"

Example

# Check NetworkManager.conf
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

nmcli connection show "connection name" | grep psk

3. Reset network configuration

Example

nmcli connection reload

Practice exercises

  1. Use nmcli to connect to your WiFi network
  2. Create a wired connection with a static IP
  3. Scan and list all available WiFi networks
  4. 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.

Linux 命令大全Linux Command Encyclopedia

Other extensions