Operations 10 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering nmcli: A Complete Linux Network Management Guide

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 NetworkManager

Listing Network Connections

Display all configured connections and their states:

nmcli connection show

Managing 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/24

Deleting 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 PASSWORD

Listing Available Wi‑Fi Networks

nmcli device wifi list

Configuring 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.ovpn

PPTP

nmcli connection add type pptp con-name "My PPTP VPN" ifname ppp0 user USERNAME password PASSWORD

L2TP

nmcli connection add type l2tp con-name "My L2TP VPN" ifname ppp0 user USERNAME password PASSWORD

Example 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/24

Connect to Wi‑Fi:

nmcli device wifi connect MySSID password MyPassword

Import OpenVPN configuration:

nmcli connection import type openvpn file myvpn.ovpn

Configure PPTP VPN:

nmcli connection add type pptp con-name "My PPTP VPN" ifname ppp0 user USERNAME password PASSWORD

Configure L2TP VPN:

nmcli connection add type l2tp con-name "My L2TP VPN" ifname ppp0 user USERNAME password PASSWORD

Advanced 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.txt

2. 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"
fi

3. Command Aliases

Add shortcuts to ~/.bashrc for frequent operations:

alias mywifi='nmcli device wifi connect MySSID password MyPassword'

Apply the changes with:

source ~/.bashrc

4. 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
done

Conclusion

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.

nmcli illustration
nmcli illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLILinuxWi-Finetwork managementVPNnmcli
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.