Netplan is a modern network configuration tool introduced in Ubuntu 17.10 and later adopted as the default for managing network interfaces in Ubuntu 18.04 and beyond. With its YAML-based configuration files, Netplan simplifies the process of managing complex network setups, providing a seamless interface to underlying tools like systemd-networkd and NetworkManager.
In this guide, we’ll walk you through the process of configuring network interfaces using Netplan, from understanding its core concepts to troubleshooting potential issues. By the end, you’ll be equipped to handle basic and advanced network configurations on Ubuntu systems.
Understanding Netplan
Netplan serves as a unified tool for network configuration, allowing administrators to manage networks using declarative YAML files. These configurations are applied by renderers like:
The key benefits of Netplan include:
-
Simplicity: YAML-based syntax reduces complexity.
-
Consistency: A single configuration file for all interfaces.
-
Flexibility: Supports both simple and advanced networking scenarios like VLANs and bridges.
Prerequisites
Before diving into Netplan, ensure you have the following:
-
A supported Ubuntu system (18.04 or later).
-
Administrative privileges (sudo access).
-
Basic knowledge of network interfaces and YAML syntax.
Locating Netplan Configuration Files
Netplan configuration files are stored in /etc/netplan/
. These files typically end with the .yaml
extension and may include filenames like 01-netcfg.yaml
or 50-cloud-init.yaml
.
Important Tips:
-
Backup existing configurations: Before making changes, create a backup with the command:
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak
-
YAML Syntax Rules: YAML is indentation-sensitive. Always use spaces (not tabs) for indentation.
Configuring Network Interfaces with Netplan
Here’s how you can configure different types of network interfaces using Netplan.
Step 1: Identify Network Interfaces
Before modifying configurations, identify available network interfaces using:
ip a
This command lists all network interfaces and their current states (e.g., enp0s3
, eth0
, wlan0
).
Step 2: Edit the Netplan Configuration File
Open the desired configuration file with a text editor. For example:
sudo nano /etc/netplan/01-netcfg.yaml
Step 3: Configure Common Scenarios
1. Dynamic Host Configuration Protocol (DHCP)
To configure an interface to obtain an IP address dynamically via DHCP:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: true
2. Static IP Address
To assign a static IP address to an interface:
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
3. Wi-Fi Configuration
For a Wi-Fi interface, include the SSID and passphrase:
network:
version: 2
renderer: networkd
wifis:
wlan0:
access-points:
"YourSSID":
password: "YourPassword"
dhcp4: true
4. Multiple Interfaces
To configure multiple interfaces in a single file:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: true
enp0s8:
addresses:
- 192.168.2.100/24
gateway4: 192.168.2.1
Applying and Testing Configurations
Once you’ve edited the configuration file, apply the changes using:
sudo netplan apply
Testing the Configuration
-
Verify Interface Status:
ip a
-
Check Connectivity:
ping -c 4 google.com
Safe Testing with netplan try
The netplan try
command temporarily applies changes for a set duration (e.g., 120 seconds). If you fail to confirm the configuration, it reverts to the previous state:
sudo netplan try
Advanced Configurations
VLANs
network:
version: 2
ethernets:
enp0s3:
dhcp4: no
vlans:
vlan10:
id: 10
link: enp0s3
addresses:
- 192.168.10.1/24
Bridging
network:
version: 2
bridges:
br0:
dhcp4: true
interfaces:
- enp0s3
Troubleshooting
Common Issues and Fixes
-
YAML Syntax Errors:
-
Connectivity Issues:
-
Roll Back Changes:
Conclusion
Netplan simplifies network configuration on Ubuntu, offering a consistent and intuitive approach to managing network interfaces. Whether you’re configuring basic DHCP or advanced setups like VLANs and bridges, Netplan’s YAML-based syntax makes it accessible to both novice and experienced administrators.
By mastering the steps outlined in this guide, you’ll be well-equipped to handle diverse networking scenarios, ensuring your Ubuntu systems are always optimally configured.