The Modern Way to Manage Linux Networks
Historically, Linux system administrators managed network interfaces by manually editing configuration files (like /etc/network/interfaces) or by using the deprecated ifconfig command. Modern Linux distributions—especially those running systemd—now rely on NetworkManager to handle networking dynamically.
To interact with NetworkManager from the terminal, you use the nmcli (NetworkManager Command Line Interface) utility. Whether you are configuring a static IP on a headless Ubuntu server, connecting a Raspberry Pi to Wi-Fi, or troubleshooting a broken route, nmcli is the most powerful tool for the job.
Step 1: View Current Network Status
To get a quick overview of all network devices (Ethernet, Wi-Fi, virtual bridges) and their current states, simply run:
nmcli device status
The output will list the DEVICE (e.g., eth0 or wlan0), its TYPE, its STATE (connected, disconnected, unmanaged), and the CONNECTION profile currently applied to it.
To see detailed information about a specific active connection, including its IP address, subnet mask, and DNS servers, run:
nmcli connection show
Step 2: Assign a Static IP Address
By default, most network interfaces are configured to use DHCP. To assign a static IP address to an interface (for example, a connection named Wired connection 1), you need to modify its IPv4 properties.
First, tell NetworkManager to change the IPv4 method from “auto” (DHCP) to “manual”:
sudo nmcli connection modify "Wired connection 1" ipv4.method manual
Next, assign the specific IP address and the subnet mask (using CIDR notation, such as /24):
sudo nmcli connection modify "Wired connection 1" ipv4.addresses 192.168.1.50/24
Set the default gateway (your router’s IP address):
sudo nmcli connection modify "Wired connection 1" ipv4.gateway 192.168.1.1
Set the DNS servers (e.g., Google’s public DNS):
sudo nmcli connection modify "Wired connection 1" ipv4.dns "8.8.8.8 8.8.4.4"
Step 3: Apply the Changes (Restarting the Interface)
Modifying a connection profile does not apply the changes instantly. To apply your new static IP configuration, you must bounce (restart) the connection profile.
Bring the connection down:
sudo nmcli connection down "Wired connection 1"
Bring the connection back up:
sudo nmcli connection up "Wired connection 1"
Run ip a or nmcli device show to verify that your interface is now using the new static IP address.
Step 4: Connecting to Wi-Fi via nmcli
If you are working on a laptop without a desktop environment, you can use nmcli to scan for and connect to wireless networks.
First, ensure the Wi-Fi radio is turned on:
nmcli radio wifi on
Scan for available Wi-Fi networks in your area:
nmcli device wifi list
Connect to a specific network (replace “Your_SSID” with the network name and “Your_Password” with the Wi-Fi password):
sudo nmcli device wifi connect "Your_SSID" password "Your_Password"
NetworkManager will automatically save this profile and attempt to reconnect to it upon future reboots, completely eliminating the need to manually configure wpa_supplicant files.