Website Vidya
Linux & ServersStep-by-Step Guide

How to Configure Networking with Netplan on Ubuntu

Learn how to configure networking with netplan on ubuntu with static IPs, DHCP, DNS nameservers, gateway routes, YAML indentation, and netplan try.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Configure Networking with Netplan on Ubuntu
AI Executive SummaryWebsiteVidya AI

5 min read

Learn how to configure networking with netplan on ubuntu with static IPs, DHCP, DNS nameservers, gateway routes, YAML indentation, and netplan try.

Key Highlights
  • Prerequisites and Supported Releases
  • Understanding Netplan Architecture and File Hierarchy
  • Configuring a Static IPv4 Address with Netplan
Grounded in full engineering runbookWebsiteVidya AI Engine

Modern Ubuntu releases utilize Netplan as the default declarative network configuration abstraction utility.

First introduced to replace the legacy /etc/network/interfaces configuration system, Netplan reads human-readable YAML configuration files and generates the corresponding backend configuration files automatically.

Depending on whether you are running Ubuntu Server or Ubuntu Desktop, Netplan renders configuration directives for either systemd-networkd or NetworkManager.

Misconfiguring indentation or gateway syntax in a Netplan YAML file on a remote server can lead to sudden loss of network connectivity and lock you out of SSH.

Advertisement

In this guide, we explain how to configure networking with netplan on ubuntu safely, covering static IP addressing, DHCP, DNS nameservers, and fail-safe rollback workflows.

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

Advertisement
  • An active installation of Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • A user account with sudo administrative privileges.
  • Physical console access, out-of-band VNC, or a secure secondary connection in case of network misconfiguration.
  • Basic understanding of IP addresses, CIDR notation (e.g. /24), and default gateways.

Understanding Netplan Architecture and File Hierarchy

Netplan stores its configuration files inside /etc/netplan/ with the .yaml file extension.

When multiple files exist in this directory, Netplan loads them in lexicographical order (e.g., 01-netcfg.yaml followed by 50-cloud-init.yaml), allowing subsequent files to override earlier directives.

Before modifying network settings, identify your active network interface names using the ip link command:

# List active network interfaces:
ip link

# Output showing loopback (lo) and physical ethernet interface (ens18 or eth0):
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff

Configuring a Static IPv4 Address with Netplan

To assign a persistent static IP address to your network interface, create or edit a configuration file such as /etc/netplan/01-static-network.yaml.

Advertisement

In modern Netplan versions (Ubuntu 22.04 and 24.04), the legacy gateway4 directive is deprecated in favor of explicit routes blocks.

Strict YAML indentation using two spaces (never tabs) is mandatory:

# /etc/netplan/01-static-network.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses:
        - 192.168.1.150/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses:
          - 1.1.1.1
          - 8.8.8.8
        search:
          - internal.domain

Breakdown of Key Directives

version: 2 — Specifies Netplan version 2 schema.

renderer: networkd — Instructs Netplan to compile configuration for systemd-networkd (ideal for headless servers). On desktop systems, use renderer: NetworkManager.

addresses: [192.168.1.150/24] — Defines the static IPv4 address in standard CIDR notation.

routes: to: default via: 192.168.1.1 — Defines the default gateway for outbound traffic routing.

nameservers: addresses — Lists primary and secondary upstream DNS resolvers.

Configuring Dynamic IP Addressing (DHCP)

If your server resides in an environment where network leases are managed dynamically by a local DHCP router or cloud hypervisor, configure DHCP simply:

Set dhcp4: true under the designated interface block:

# /etc/netplan/01-dhcp-network.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: true
      dhcp6: false

Advanced Configurations: Multiple IP Addresses and VLANs

Netplan easily accommodates advanced enterprise network topologies, including multiple IP aliases on a single NIC, network bonding, and 802.1Q VLAN tagging.

To bind multiple secondary IP addresses to an interface, list additional CIDR blocks under the addresses array.

For VLAN tagging, declare a dedicated vlans section that references the underlying physical parent link:

# /etc/netplan/02-vlan-network.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
  vlans:
    vlan10:
      id: 10
      link: eth0
      addresses:
        - 10.10.10.50/24
      routes:
        - to: 10.10.0.0/16
          via: 10.10.10.1

Testing and Applying Changes Safely with netplan try

Never execute netplan apply directly over an active SSH session without first validating syntax.

Netplan provides the powerful netplan try command, which applies the new network configuration for a trial period (default 120 seconds).

If you do not confirm the changes by pressing Enter within the timeout window, Netplan automatically rolls back to the previous known-working state:

# Test configuration with automatic rollback protection:
sudo netplan try

# Terminal countdown prompt:
Do you want to keep these settings?

Press ENTER to accept the new configuration

Changes will revert in 120 seconds...
Configuration accepted.

Applying Validated Settings Permanently

Once confirmed, apply the configuration permanently to generate underlying backend files and reload systemd-networkd:

Verify the new IP assignment and default route using ip addr show and ip route show:

# Permanently apply network settings:
sudo netplan apply

# Verify assigned address:
ip addr show dev eth0

# Verify default gateway routing:
ip route show

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

Invalid YAML: tab character found at line X column Y

Cause: Netplan YAML parsers strictly reject tab characters for indentation.

Resolution: Open the file in your editor and convert all tabs into double spaces.

sed -i 's/\t/  /g' /etc/netplan/*.yaml && sudo netplan generate

gateway4 has been deprecated, use default routes instead

Cause: Using legacy gateway4: 192.168.1.1 syntax in Ubuntu 22.04 or 24.04.

Resolution: Replace gateway4: X.X.X.X with routes: - to: default via: X.X.X.X in the interface definition.

sudo netplan generate

Permissions on configuration file are too open

Cause: Netplan requires that configuration files in /etc/netplan/ have mode 600 or 644 owned by root.

Resolution: Set strict file permissions on all Netplan configuration files.

sudo chmod 600 /etc/netplan/*.yaml

Security Best Practices and Hardening

Always set file permissions on /etc/netplan/*.yaml to 600 (read and write only by root), especially if configurations define wireless pre-shared keys.

Ensure DNS nameservers use reputable, encrypted, or private internal resolvers to prevent DNS spoofing.

Always have out-of-band management (IPMI, iLO, or cloud VNC console) available when modifying default gateway routes remotely.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
Netplan configuration files are located in the /etc/netplan/ directory with a .yaml file extension, such as 01-netcfg.yaml or 50-cloud-init.yaml.
systemd-networkd is a lightweight, headless network daemon ideal for servers, containers, and cloud instances. NetworkManager is a feature-rich daemon designed for desktops and laptops supporting dynamic Wi-Fi networks and VPNs.
netplan try is designed to prevent accidental lockouts. If you change network settings and cannot confirm the prompt within 120 seconds (e.g. because your SSH session disconnected due to an invalid IP or route), Netplan restores the working configuration automatically.
You can add multiple IP addresses by listing them in the addresses block under the interface, for example: addresses: [192.168.1.150/24, 192.168.1.151/24].
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout