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 NetworkManagerorsudo yum install NetworkManager - Arch Linux:
sudo pacman -S networkmanager
Basic Usage
Displaying Network Connections
To list all available network connections:
| |
This will display a table with the connection name, UUID, type, and device.
Displaying Network Devices
To list all network devices:
| |
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):
| |
Deactivating a Connection
To deactivate a connection (e.g., MyWiFi):
| |
Editing Connections
You can modify existing connections using nmcli. For example, to edit the Wi-Fi password for a connection named MyWiFi:
| |
Then, you need to deactivate and activate the connection for the changes to take effect:
| |
Creating a New Wi-Fi Connection
To create a new Wi-Fi connection, you’ll need the SSID and password:
| |
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:
| |
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):
| |
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.
| |
Removing a Connection
To remove a connection (e.g., MyOldWiFi):
| |
Useful Tips and Tricks
- Tab Completion:
nmclisupports tab completion, making it easier to enter commands and connection names. - Help: Use
nmcli --helpornmcli <command> --helpfor 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/syslogor/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.