How to Configure Networks Using NetworkManager and Netplan
Learn how to configure Linux networks using NetworkManager's nmcli command and Netplan's YAML-based configuration system. Covers both wired and wireless network setup with practical examples.
Network configuration is one of the most fundamental skills in Linux administration. Whether you're setting up a server, connecting to WiFi, or managing enterprise network connections, you'll encounter two primary tools: NetworkManager and Netplan. Let's explore how to use both effectively.
Understanding NetworkManager
NetworkManager is a dynamic network configuration daemon that automatically manages network connections. It's the default on most desktop Linux distributions and provides both GUI and command-line interfaces.
The primary command-line tool is nmcli (NetworkManager Command Line Interface). Let's start with basic operations:
# Check NetworkManager status
sudo systemctl status NetworkManager
# View all network connections
nmcli connection show
# Show active connections only
nmcli connection show --activeConfiguring Network Connections with nmcli
Creating a new wired connection is straightforward with nmcli:
# Create a new ethernet connection
nmcli connection add type ethernet con-name "office-lan" ifname eth0
# Configure static IP
nmcli connection modify "office-lan" ipv4.addresses 192.168.1.100/24
nmcli connection modify "office-lan" ipv4.gateway 192.168.1.1
nmcli connection modify "office-lan" ipv4.dns "8.8.8.8,8.8.4.4"
nmcli connection modify "office-lan" ipv4.method manual
# Activate the connection
nmcli connection up "office-lan"For WiFi networks, the process is similar but includes authentication details:
# Scan for available WiFi networks
nmcli device wifi list
# Connect to a WiFi network
nmcli device wifi connect "WiFi-Network-Name" password "your-password"
# Create a persistent WiFi connection
nmcli connection add type wifi con-name "home-wifi" ifname wlan0 ssid "WiFi-Network-Name"
nmcli connection modify "home-wifi" wifi-sec.key-mgmt wpa-psk
nmcli connection modify "home-wifi" wifi-sec.psk "your-password"Working with Netplan
Netplan is Ubuntu's network configuration abstraction layer, introduced in Ubuntu 17.10. It uses YAML files to define network configurations and can work with different renderers, including NetworkManager.
Netplan configuration files are stored in /etc/netplan/ and typically end with .yaml. Here's a basic static IP configuration:
# /etc/netplan/01-network-manager-all.yaml
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]Essential Netplan Commands
After modifying Netplan configuration files, you need to apply the changes:
# Test configuration without applying (30-second timeout)
sudo netplan try
# Apply configuration permanently
sudo netplan apply
# Generate backend configuration files
sudo netplan generate
# Show current Netplan configuration
sudo netplan getThe netplan try command is particularly useful as it applies changes temporarily, automatically reverting if you don't confirm within 30 seconds. This prevents you from getting locked out due to configuration errors.
WiFi Configuration with Netplan
Configuring WiFi through Netplan requires additional security parameters:
network:
version: 2
renderer: NetworkManager
wifis:
wlp2s0:
access-points:
"Your-WiFi-Name":
password: "your-wifi-password"
dhcp4: trueBest Practices and Troubleshooting
When working with network configuration, always verify your changes:
# Check interface status
ip addr show
# Verify connectivity
ping -c 4 8.8.8.8
# Check routing table
ip route show
# Test DNS resolution
nslookup google.comImportant tip: When using Netplan with NetworkManager as the renderer, some configurations might be better managed directly through nmcli to avoid conflicts.
What's Next
Now that you understand basic network configuration with NetworkManager and Netplan, the next logical step is learning about network troubleshooting tools and techniques. We'll explore commands like ss, netstat, and tcpdump to diagnose network connectivity issues and monitor network traffic effectively.