Network Configuration with NetworkManager: Managing Network Connectivity
NetworkManager is the standard network management service on modern Linux distributions, providing unified management of network interfaces, connections, and connectivity. Understanding how to configure networks using NetworkManager's command-line interface (nmcli) is essential for Linux system administrators. This article explores NetworkManager, nmcli, and network configuration techniques.
Understanding NetworkManager
NetworkManager is a daemon that manages network connectivity and configuration. It provides:
- Unified Interface: Single tool for all network configuration
- Automatic Management: Handles network detection and connection
- Multiple Interfaces: Supports Ethernet, Wi-Fi, VPN, and more
- Connection Profiles: Persistent network configurations
- Command-Line and GUI: Both
nmcliand graphical tools available
NetworkManager Command-Line Interface: nmcli
nmcli is the command-line tool for managing NetworkManager.
Basic nmcli Syntax
General Format: nmcli [options] <object> <command> [arguments]
Common Options:
-t: Terse output (machine-readable)-p: Pretty output (human-readable)-f: Specify fields to display-g: Get specific field values-m: Monitor mode-h: Help
Managing Connections
Viewing Connections
List All Connections:
nmcli connection show
# Brief format
nmcli connection
# Show specific connection
nmcli connection show "Wired connection 1"
Active Connections:
nmcli connection show --active
Creating Connections
Create Ethernet Connection:
# Create static IP connection
nmcli connection add type ethernet con-name "MyConnection" \
ifname eth0 ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 ipv4.dns "8.8.8.8" \
ipv4.method manual
# Create DHCP connection
nmcli connection add type ethernet con-name "DHCP-Connection" \
ifname eth0 ipv4.method auto
Create Wi-Fi Connection:
# Connect to open Wi-Fi
nmcli device wifi connect "NetworkName"
# Connect to secured Wi-Fi
nmcli device wifi connect "NetworkName" password "password"
# Create connection profile
nmcli connection add type wifi con-name "MyWiFi" \
ifname wlan0 ssid "NetworkName" \
wifi-sec.key-mgmt wpa-psk wifi-sec.psk "password"
Modifying Connections
nmcli connection modify: Modify connection settings
Common Modifications:
# Change IP address
nmcli connection modify "MyConnection" ipv4.addresses 192.168.1.200/24
# Change gateway
nmcli connection modify "MyConnection" ipv4.gateway 192.168.1.1
# Change DNS
nmcli connection modify "MyConnection" ipv4.dns "8.8.8.8 8.8.4.4"
# Change to DHCP
nmcli connection modify "MyConnection" ipv4.method auto
# Change to static
nmcli connection modify "MyConnection" ipv4.method manual
# Add additional IP address
nmcli connection modify "MyConnection" +ipv4.addresses 10.0.0.10/24
# Remove IP address
nmcli connection modify "MyConnection" -ipv4.addresses 10.0.0.10/24
Activating and Deactivating Connections
Activate Connection:
# Activate by name
nmcli connection up "MyConnection"
# Activate by UUID
nmcli connection up uuid <uuid>
Deactivate Connection:
nmcli connection down "MyConnection"
Delete Connection:
nmcli connection delete "MyConnection"
Managing Devices
Viewing Devices
List All Devices:
nmcli device status
# Detailed information
nmcli device show
# Show specific device
nmcli device show eth0
Device States:
connected: Device is connected and activedisconnected: Device is available but not connectedunavailable: Device is not availableunmanaged: Device is not managed by NetworkManager
Device Operations
Connect Device:
# Connect using existing connection
nmcli device connect eth0
Disconnect Device:
nmcli device disconnect eth0
Wi-Fi Operations:
# List available Wi-Fi networks
nmcli device wifi list
# Connect to Wi-Fi
nmcli device wifi connect "NetworkName" password "password"
# Rescan for networks
nmcli device wifi rescan
General NetworkManager Information
General Status
Show General Status:
nmcli general status
Show Hostname:
nmcli general hostname
# Set hostname
sudo nmcli general hostname newhostname
Show Permissions:
nmcli general permissions
Networking Control
Enable/Disable Networking
Disable Networking:
nmcli networking off
Enable Networking:
nmcli networking on
Check Connectivity:
nmcli networking connectivity check
# Show connectivity status
nmcli networking connectivity
Radio Management
Wi-Fi Radio Control
Turn Wi-Fi On/Off:
# Turn Wi-Fi on
nmcli radio wifi on
# Turn Wi-Fi off
nmcli radio wifi off
# Show Wi-Fi status
nmcli radio wifi
Turn All Radios On/Off:
nmcli radio all on
nmcli radio all off
Monitoring NetworkManager
Monitor Mode
Monitor NetworkManager Events:
# Monitor all events
nmcli monitor
# Monitor connectivity
nmcli monitor connectivity
NetworkManager Text User Interface: nmtui
nmtui provides a text-based graphical interface for NetworkManager.
Using nmtui
Launch nmtui:
sudo nmtui
Menu Options:
- Edit a connection
- Activate a connection
- Set system hostname
- Quit
Navigation:
- Use arrow keys to navigate
- Tab to move between fields
- Enter to select
- Esc to go back
IP Command (Alternative/Complementary)
The ip command is a powerful alternative for network configuration.
Basic ip Commands
Show Interfaces:
# Show all interfaces
ip link show
# Show specific interface
ip link show eth0
# Show IP addresses
ip addr show
ip a
Configure Interface:
# Bring interface up
sudo ip link set eth0 up
# Bring interface down
sudo ip link set eth0 down
# Set IP address
sudo ip addr add 192.168.1.100/24 dev eth0
# Remove IP address
sudo ip addr del 192.168.1.100/24 dev eth0
Routing:
# Show routing table
ip route show
ip r
# Add route
sudo ip route add 192.168.2.0/24 via 192.168.1.1
# Delete route
sudo ip route del 192.168.2.0/24
Network Configuration Files
Traditional Configuration (ifcfg files)
Location: /etc/sysconfig/network-scripts/ (RHEL/CentOS)
Example ifcfg-eth0:
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
NetworkManager Configuration Files
Location: /etc/NetworkManager/system-connections/
Format: INI-style configuration files
Note: Generally managed through nmcli or nmtui, not edited directly
Common Network Configuration Tasks
Static IP Configuration
Using nmcli:
# Create static connection
nmcli connection add type ethernet con-name "Static-Connection" \
ifname eth0 ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8 8.8.4.4" \
ipv4.method manual
# Activate connection
nmcli connection up "Static-Connection"
DHCP Configuration
Using nmcli:
# Create DHCP connection
nmcli connection add type ethernet con-name "DHCP-Connection" \
ifname eth0 ipv4.method auto
# Or modify existing
nmcli connection modify "MyConnection" ipv4.method auto
Multiple IP Addresses
Add Secondary IP:
nmcli connection modify "MyConnection" +ipv4.addresses 10.0.0.10/24
nmcli connection down "MyConnection"
nmcli connection up "MyConnection"
Bonding (NIC Teaming)
Create Bond Interface:
# Load bonding module
sudo modprobe bonding
# Create bond connection
nmcli connection add type bond con-name bond0 ifname bond0 \
mode active-backup
# Add slaves
nmcli connection add type ethernet con-name bond0-slave1 \
ifname eth0 master bond0 slave-type bond
nmcli connection add type ethernet con-name bond0-slave2 \
ifname eth1 master bond0 slave-type bond
Troubleshooting Network Issues
Diagnostic Commands
Check Connection Status:
nmcli connection show --active
nmcli device status
Check Interface Configuration:
nmcli device show eth0
ip addr show eth0
Test Connectivity:
# Ping test
ping -c 4 8.8.8.8
# DNS test
nslookup google.com
dig google.com
View NetworkManager Logs:
journalctl -u NetworkManager
journalctl -u NetworkManager -f # Follow logs
Common Issues
Connection Not Activating:
- Check device status:
nmcli device status - Verify connection exists:
nmcli connection show - Check logs:
journalctl -u NetworkManager
IP Address Not Applied:
- Verify connection is active:
nmcli connection show --active - Check for conflicts:
ip addr show - Restart NetworkManager:
sudo systemctl restart NetworkManager
DNS Not Working:
- Check DNS configuration:
nmcli connection show "MyConnection" | grep dns - Test DNS:
nslookup google.com - Check
/etc/resolv.conf
Best Practices
Connection Management
- Use Descriptive Names: Name connections clearly
- Document Configurations: Keep records of network settings
- Test Changes: Test network changes before production
- Backup Configurations: Backup
/etc/NetworkManager/system-connections/
Security
- Secure Wi-Fi: Use WPA2/WPA3 for wireless
- Limit Access: Use firewall rules appropriately
- Monitor Connections: Regularly review active connections
- Update Regularly: Keep NetworkManager updated
Conclusion
NetworkManager and nmcli provide powerful tools for managing network connectivity on Linux systems. From basic IP configuration to advanced bonding and Wi-Fi management, NetworkManager offers a unified interface for all network operations. Understanding nmcli commands and NetworkManager concepts is essential for modern Linux administration.
By mastering NetworkManager, you can efficiently configure static and dynamic IP addresses, manage Wi-Fi connections, create network bonds, and troubleshoot connectivity issues. Whether you're working on servers or desktops, NetworkManager provides the tools needed for reliable network management.
In the next article, we'll explore package management, covering RPM, YUM, DNF, and APT for installing and managing software on Linux systems. Stay tuned!