Mastering nmcli: A Complete Linux Network Management Guide
This article provides a thorough walkthrough of the nmcli command-line tool on Linux, covering installation, basic usage, managing Ethernet, Wi‑Fi, and VPN connections, advanced batch configurations, scripting automation, and useful aliases, complete with practical code examples for each scenario.
nmcli is a powerful command-line utility bundled with NetworkManager that enables users to configure and control network connections on Linux systems, including Ethernet, Wi‑Fi, and VPN.
Installation and Basic Usage
Installation
nmcli is typically installed alongside NetworkManager. Verify its presence by running: nmcli --version If version information is displayed, nmcli is ready to use.
Starting and Stopping NetworkManager
Before using nmcli, ensure the NetworkManager service is active: sudo systemctl start NetworkManager To stop the service:
sudo systemctl stop NetworkManagerListing Network Connections
Display all configured connections and their states:
nmcli connection showManaging Network Connections
Creating a New Connection
Add an Ethernet connection named eth0: nmcli connection add type ethernet ifname eth0 Refer to the man nmcli pages for additional parameters.
Modifying an Existing Connection
Change the IPv4 address of a connection called "Wired connection 1":
nmcli connection modify "Wired connection 1" ipv4.address 192.168.1.2/24Deleting a Connection
Remove the same connection:
nmcli connection delete "Wired connection 1"Wi‑Fi Management
Connecting to a Wi‑Fi Network
Replace SSID and PASSWORD with the target network’s name and password:
nmcli device wifi connect SSID password PASSWORDListing Available Wi‑Fi Networks
nmcli device wifi listConfiguring a Wi‑Fi Connection
Create a named Wi‑Fi profile:
nmcli connection add type wifi ifname wlan0 con-name "My WiFi" ssid "MySSID"Additional options such as passwords can be added to the profile.
VPN Configuration
OpenVPN
nmcli connection import type openvpn file myvpn.ovpnPPTP
nmcli connection add type pptp con-name "My PPTP VPN" ifname ppp0 user USERNAME password PASSWORDL2TP
nmcli connection add type l2tp con-name "My L2TP VPN" ifname ppp0 user USERNAME password PASSWORDExample Commands
Create an Ethernet connection: nmcli connection add type ethernet ifname eth0 Modify IPv4 address:
nmcli connection modify "Wired connection 1" ipv4.address 192.168.1.2/24Connect to Wi‑Fi:
nmcli device wifi connect MySSID password MyPasswordImport OpenVPN configuration:
nmcli connection import type openvpn file myvpn.ovpnConfigure PPTP VPN:
nmcli connection add type pptp con-name "My PPTP VPN" ifname ppp0 user USERNAME password PASSWORDConfigure L2TP VPN:
nmcli connection add type l2tp con-name "My L2TP VPN" ifname ppp0 user USERNAME password PASSWORDAdvanced Usage
1. Batch Configuration of Connections
Create a text file (e.g., my_connections.txt) containing multiple connection definitions, then import them:
nmcli -f json connection import type keyfile file my_connections.txt2. Scripting and Automation
Combine nmcli with shell scripts to automate tasks such as checking Wi‑Fi status and connecting when disconnected:
#!/bin/bash
wifi_ssid="MySSID"
wifi_password="MyPassword"
nmcli connection show --active | grep -q "$wifi_ssid"
if [ $? -ne 0 ]; then
nmcli device wifi connect "$wifi_ssid" password "$wifi_password"
fi3. Command Aliases
Add shortcuts to ~/.bashrc for frequent operations:
alias mywifi='nmcli device wifi connect MySSID password MyPassword'Apply the changes with:
source ~/.bashrc4. Monitoring Network Status
A loop script can periodically verify the Wi‑Fi connection and attempt reconnection if it drops:
#!/bin/bash
wifi_ssid="MySSID"
while true; do
nmcli connection show --active | grep -q "$wifi_ssid"
if [ $? -ne 0 ]; then
echo "Wi‑Fi disconnected, attempting reconnection..."
nmcli device wifi connect "$wifi_ssid" password "$wifi_password"
fi
sleep 60
doneConclusion
nmcli offers a comprehensive, scriptable interface for managing Linux network connections. Whether configuring Ethernet, Wi‑Fi, or various VPN types, the commands and examples provided enable efficient and automated network administration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
