NetworkManager is a dynamic network control and configuration daemon that attempts to keep network connections up and active. nmcli
is a command-line tool used to interact with NetworkManager. This post will provide a comprehensive guide to using nmcli
for managing network connections in Linux.
Installation
nmcli
is usually installed by default on most Linux distributions that use NetworkManager. However, if it’s not installed, you can install it using your distribution’s package manager:
- Debian/Ubuntu:
sudo apt install network-manager
- Fedora/CentOS/RHEL:
sudo dnf install NetworkManager
orsudo yum install NetworkManager
- Arch Linux:
sudo pacman -S networkmanager
Basic Usage
Displaying Network Connections
To list all available network connections:
nmcli connection show
This will display a table with the connection name, UUID, type, and device.
Displaying Network Devices
To list all network devices:
nmcli device show
This will show information about each network device, including its state, type, and hardware address.
Activating a Connection
To activate a connection (e.g., MyWiFi
):
nmcli connection up MyWiFi
Deactivating a Connection
To deactivate a connection (e.g., MyWiFi
):
nmcli connection down MyWiFi
Editing Connections
You can modify existing connections using nmcli
. For example, to edit the Wi-Fi password for a connection named MyWiFi
:
nmcli connection modify MyWiFi wifi-sec.psk "your_new_password"
Then, you need to deactivate and activate the connection for the changes to take effect:
nmcli connection down MyWiFi
nmcli connection up MyWiFi
Creating a New Wi-Fi Connection
To create a new Wi-Fi connection, you’ll need the SSID and password:
nmcli device wifi connect "YourWiFiSSID" password "YourWiFiPassword" name "MyNewWiFi"
Replace "YourWiFiSSID"
, "YourWiFiPassword"
, and "MyNewWiFi"
with your actual Wi-Fi network’s SSID, password, and the desired connection name, respectively.
Creating a New Ethernet Connection
For Ethernet connections, you typically don’t need to specify a password. You can create a new Ethernet connection with a specific connection name:
nmcli connection add type ethernet con-name MyEthernet ifname eth0
Replace MyEthernet
with your desired connection name and eth0
with the interface name if it’s different.
Setting a Static IP Address
To configure a static IP address for a connection (e.g., MyEthernet
):
nmcli connection modify MyEthernet ipv4.method manual
nmcli connection modify MyEthernet ipv4.address 192.168.1.100/24
nmcli connection modify MyEthernet ipv4.gateway 192.168.1.1
nmcli connection modify MyEthernet ipv4.dns 8.8.8.8
Replace the IP address, gateway, and DNS server with your desired values. Again, you’ll need to deactivate and activate the connection for the changes to take effect.
nmcli connection down MyEthernet
nmcli connection up MyEthernet
Removing a Connection
To remove a connection (e.g., MyOldWiFi
):
nmcli connection delete MyOldWiFi
Useful Tips and Tricks
- Tab Completion:
nmcli
supports tab completion, making it easier to enter commands and connection names. - Help: Use
nmcli --help
ornmcli <command> --help
for detailed information about commands and options. - Profiles: NetworkManager saves connection profiles, so you can easily switch between different network configurations.
- Troubleshooting: If you encounter issues, check the NetworkManager logs (usually in
/var/log/syslog
or/var/log/messages
) for error messages.
Conclusion
nmcli
is a powerful and versatile tool for managing network connections in Linux. By mastering the commands outlined in this guide, you can easily configure and troubleshoot your network settings from the command line. This is especially useful for headless servers or when you prefer a command-line interface.