Back to Blog

Netplan on Ubuntu Server: static addresses, DHCP and VLAN tagging

Complete tutorial on netplan in Ubuntu. Learn YAML syntax, renderer selection, apply vs try, bridges and bonds.

Netplan on Ubuntu Server: static addresses, DHCP and VLAN tagging

Netplan on Ubuntu Server: Static Addresses, DHCP, and VLAN Tagging

Networking is a crucial aspect of system administration, especially in server environments. Ubuntu Server utilizes Netplan, a YAML-based configuration utility, to manage network settings. Netplan is essential for defining how your server interacts with networks, whether using static IP addresses, DHCP, or VLAN tagging. Understanding Netplan not only helps in configuring networks effectively but also ensures that your server is resilient and performs optimally in diverse environments.

This tutorial will cover the basics of Netplan, including its YAML syntax, renderer selection, and the difference between applying and testing configurations. We will also dive into practical examples of how to configure static addresses, DHCP, and VLAN tagging, as well as advanced setups using bridges and bonds. By the end of this tutorial, you will have a solid understanding of Netplan and the skills to manage your network configurations seamlessly.

Prerequisites

  • Ubuntu Version: This tutorial is applicable to Ubuntu Server 18.04 LTS and later.
  • Required Packages: Netplan is pre-installed in modern Ubuntu Server versions. No additional packages are required.
  • Permissions: Root or sudo privileges are necessary to modify network configurations.
  • Risks: Incorrect configurations can lead to loss of network connectivity. Always ensure you have console access to the server for recovery.

Core Concepts

  • Netplan: A network configuration utility that uses YAML files for defining network interfaces.
  • YAML Syntax: A human-readable data serialization language used for configuration files.
  • Renderer: The backend that Netplan uses to apply the configuration (e.g., Networkd or NetworkManager).
  • Apply vs Try: netplan apply applies configurations immediately, while netplan try allows you to test configurations temporarily.
  • Bridges: Used to connect multiple network interfaces, acting as a virtual switch.
  • Bonds: Combine multiple network interfaces to act as a single interface for redundancy or increased throughput.

Syntax/Commands

Command Description
netplan apply Applies the configuration changes immediately.
netplan try Tests the configuration, allowing fallback if failed.
netplan generate Generates network configuration files for the renderer.
netplan ip Displays the current network configuration and status.

Practical Examples

1. Configuring a Static IP Address

To set a static IP address, edit the Netplan configuration file located in /etc/netplan/. Here’s a basic example:

sudo nano /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    ens33:
      dhcp: no
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

After editing, apply the changes:

sudo netplan apply

2. Configuring DHCP

For dynamic IP assignment via DHCP, modify the same YAML file:

network:
  version: 2
  ethernets:
    ens33:
      dhcp: yes

Apply the configuration:

sudo netplan apply

3. Testing Configuration

To test the changes without applying them:

sudo netplan try

This command allows you to revert back if the configuration fails to apply.

4. Configuring VLAN Tagging

To configure VLAN tagging, you need to define a parent interface and a VLAN ID:

network:
  version: 2
  ethernets:
    ens33:
      dhcp4: no
    vlan.100:
      id: 100
      link: ens33
      addresses:
        - 192.168.1.101/24

Apply the settings:

sudo netplan apply

5. Setting Up a Bridge

Bridging allows multiple interfaces to act as a single network interface. Here’s how to set up a bridge:

network:
  version: 2
  ethernets:
    ens33:
      dhcp4: no
  bridges:
    br0:
      interfaces: [ens33]
      addresses: [192.168.1.102/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]

Apply the changes:

sudo netplan apply

6. Configuring Bonding

To configure bonding for redundancy, use the following example:

network:
  version: 2
  bonds:
    bond0:
      interfaces: [ens33, ens34]
      addresses: [192.168.1.103/24]
      gateway4: 192.168.1.1
      mode: active-backup

Apply the configuration:

sudo netplan apply

7. Combining VLAN and Bonding

You can also combine VLAN and bonding:

network:
  version: 2
  bonds:
    bond0:
      interfaces: [ens33, ens34]
      addresses: [192.168.1.104/24]
      mode: active-backup
  vlans:
    vlan.200:
      id: 200
      link: bond0
      addresses: [192.168.1.105/24]

Apply the changes:

sudo netplan apply

8. Viewing Current Network Configuration

You can view the current network configuration using:

ip addr show

This command will display all interfaces and their current settings.

Real-World Scenarios

Scenario 1: Web Server with Static IP

You have a web server that requires a static IP for consistent access. By setting a static IP using Netplan, you ensure that your web application remains accessible.

Scenario 2: DHCP for Workstations

In an office environment, workstations can be configured to use DHCP for automatic IP assignment, simplifying network management.

Scenario 3: VLANs for Network Segmentation

In a corporate environment, VLAN tagging can be used to segment traffic between departments, enhancing security and performance.

Best Practices

  1. Backup Configurations: Always back up your Netplan configurations before making changes.
  2. Test Configurations: Use netplan try to test configurations before applying them permanently.
  3. Use Comments: Comment your YAML files for clarity, especially if multiple admins are managing the server.
  4. Monitor Network: Use tools like netstat and iftop to monitor network performance after applying changes.
  5. Documentation: Keep documentation up to date regarding your network configurations to ease troubleshooting.

Common Errors

  1. Error: Error in network definition

    • Cause: YAML syntax error.
    • Fix: Check for indentation issues or incorrect syntax.
  2. Error: Cannot find interface

    • Cause: The specified interface does not exist.
    • Fix: Verify the interface name using ip link show.
  3. Error: Failed to apply configuration

    • Cause: Invalid configuration parameters.
    • Fix: Review the YAML file for errors.
  4. Error: Network unreachable

    • Cause: Incorrect gateway or subnet mask.
    • Fix: Ensure the gateway is reachable and the subnet mask is correct.

Related Commands

Command Description
ifup Brings a network interface up.
ifdown Brings a network interface down.
ip Shows/manages network interfaces and routes.
nmcli Command-line tool for managing NetworkManager.

Automation Script

Here’s a complete bash script to automate the configuration of a static IP address with Netplan:

#!/bin/bash

# Configuration variables
INTERFACE="ens33"
IP_ADDRESS="192.168.1.100/24"
GATEWAY="192.168.1.1"
NAMESERVERS=("8.8.8.8" "8.8.4.4")

# Create Netplan configuration file
cat <<EOL | sudo tee /etc/netplan/01-netcfg.yaml
network:
  version: 2
  ethernets:
    $INTERFACE:
      dhcp: no
      addresses:
        - $IP_ADDRESS
      gateway4: $GATEWAY
      nameservers:
        addresses:
          - ${NAMESERVERS[0]}
          - ${NAMESERVERS[1]}
EOL

# Apply the configuration
sudo netplan apply

# Display current configuration
ip addr show $INTERFACE

Conclusion

In this tutorial, we explored Netplan on Ubuntu Server, covering static IP configuration, DHCP, VLAN tagging, and advanced networking setups like bridges and bonding. Understanding how to use Netplan effectively is crucial for network management in server environments. With the practical examples provided, you should now be well-equipped to configure and troubleshoot your network settings.

Next Steps

Continue exploring advanced networking concepts, such as firewall management using ufw, or delve into monitoring your network performance with tools like netstat and iftop.

References