Network Configuration with Netplan

Level: Intermediate Module: TCP/IP & Networking 6 min read Lesson 15 of 66

Overview

  • What you’ll learn: How Netplan works as Ubuntu’s network configuration layer, YAML syntax for defining interfaces, static and DHCP addressing, routes, DNS, bonds, VLANs, and how to apply and troubleshoot configurations.
  • Prerequisites: Lesson 14 – Routing and Switching Basics
  • Estimated reading time: 20 minutes

Introduction

Network configuration on Linux has historically been managed through a variety of tools and file formats — from /etc/network/interfaces on Debian to NetworkManager on desktop systems. Ubuntu 17.10 introduced Netplan, a unified, declarative YAML-based configuration layer that sits above the underlying network daemons (systemd-networkd or NetworkManager).

Netplan simplifies network configuration by providing a single, consistent syntax regardless of which backend renderer is used. You describe the desired network state in YAML files, and Netplan generates the appropriate configuration for the backend. This approach is especially powerful for cloud deployments and automated provisioning.

In this lesson, you will learn the Netplan architecture, master the YAML configuration syntax, configure common networking scenarios, and learn how to apply, test, and troubleshoot your configurations.

Netplan Architecture

Netplan operates as a translation layer between your YAML configuration files and the underlying network renderer. Configuration files are stored in /etc/netplan/ and are processed in lexicographic order. Netplan supports two backend renderers: systemd-networkd (default on Ubuntu Server) and NetworkManager (default on Ubuntu Desktop).

# List existing Netplan configuration files
$ ls /etc/netplan/
01-netcfg.yaml

# View the current configuration
$ cat /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: true

# View what Netplan will generate (dry run)
$ sudo netplan generate

# Show the current network state as Netplan sees it
$ sudo netplan status
     Online state: online
    DNS Addresses: 192.168.1.1
       DNS Search: localdomain

●  1: lo -- Software Loopback (Unmanaged)
●  2: ens33 -- Ethernet (networkd: ens33)
      MAC Address: 00:0c:29:ab:cd:ef
        Addresses: 192.168.1.10/24
      DNS Servers: 192.168.1.1
           Routes: default via 192.168.1.1

Static IP Configuration

The most common Netplan task is assigning a static IP address. You specify the interface, disable DHCP, and provide the address, gateway, and DNS servers.

# /etc/netplan/01-netcfg.yaml — Static IP configuration
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no
      addresses:
        - 192.168.1.10/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        search: [example.com]
        addresses: [8.8.8.8, 8.8.4.4]

# Apply the configuration
$ sudo netplan apply

# Verify the new IP address
$ ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500
    inet 192.168.1.10/24 brd 192.168.1.255 scope global ens33

# Verify the default route
$ ip route show
default via 192.168.1.1 dev ens33 proto static
192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.10

# Verify DNS resolution
$ resolvectl status ens33
Link 2 (ens33)
    Current Scopes: DNS
    DNS Servers: 8.8.8.8 8.8.4.4
    DNS Domain: example.com

Multiple Interfaces and Routes

Servers often have multiple network interfaces — for example, one for public traffic and another for a private management network. Netplan makes it easy to configure each interface independently with its own addresses, routes, and DNS settings.

# /etc/netplan/01-netcfg.yaml — Multiple interfaces
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no
      addresses:
        - 192.168.1.10/24
      routes:
        - to: default
          via: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8]
    ens34:
      dhcp4: no
      addresses:
        - 10.0.0.10/24
      routes:
        - to: 10.10.0.0/16
          via: 10.0.0.1
        - to: 172.16.0.0/12
          via: 10.0.0.254

# Apply and verify
$ sudo netplan apply
$ ip route show
default via 192.168.1.1 dev ens33 proto static
10.0.0.0/24 dev ens34 proto kernel scope link src 10.0.0.10
10.10.0.0/16 via 10.0.0.1 dev ens34 proto static
172.16.0.0/12 via 10.0.0.254 dev ens34 proto static
192.168.1.0/24 dev ens33 proto kernel scope link src 192.168.1.10

Bonding and VLANs

For high availability and network segmentation, Netplan supports interface bonding (combining multiple physical interfaces into one logical interface) and VLANs (virtual LANs that segment traffic on the same physical network).

# /etc/netplan/01-netcfg.yaml — Bond configuration
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no
    ens34:
      dhcp4: no
  bonds:
    bond0:
      interfaces: [ens33, ens34]
      addresses:
        - 192.168.1.10/24
      routes:
        - to: default
          via: 192.168.1.1
      parameters:
        mode: 802.3ad
        lacp-rate: fast
        mii-monitor-interval: 100

# VLAN configuration
network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      dhcp4: no
  vlans:
    vlan100:
      id: 100
      link: ens33
      addresses:
        - 10.100.0.10/24
    vlan200:
      id: 200
      link: ens33
      addresses:
        - 10.200.0.10/24

Testing and Troubleshooting

Netplan provides a safe way to test configurations before committing them. The netplan try command applies changes temporarily and automatically reverts if you do not confirm within a timeout period.

# Test a configuration with automatic rollback (default 120 seconds)
$ sudo netplan try
Do you want to keep these settings?

Press ENTER before the timeout to accept the new configuration

Changes will revert in 118 seconds

# Validate YAML syntax without applying
$ sudo netplan generate
# (No output means success; errors are printed if YAML is invalid)

# Debug mode — see exactly what Netplan generates
$ sudo netplan --debug generate
DEBUG: Processing /etc/netplan/01-netcfg.yaml
DEBUG: Generating output files...

# Check systemd-networkd status
$ networkctl status ens33
● 2: ens33
       Link File: /usr/lib/systemd/network/99-default.link
    Network File: /run/systemd/network/10-netplan-ens33.network
            Type: ether
           State: routable (configured)
         Address: 192.168.1.10
         Gateway: 192.168.1.1
             DNS: 8.8.8.8

Key Takeaways

  • Netplan is Ubuntu’s declarative YAML-based network configuration system that generates configs for systemd-networkd or NetworkManager backends.
  • Configuration files in /etc/netplan/ define interfaces, addresses, routes, DNS, bonds, and VLANs in a clean, readable format.
  • Use sudo netplan apply to activate changes, and sudo netplan try to test with automatic rollback for safety.
  • Multiple interfaces can be configured independently, each with their own static addresses, routes, and nameservers.
  • Always validate with sudo netplan generate before applying, and use networkctl status to verify the result.

What’s Next

In Lesson 16, you will learn DNS Fundamentals — how the Domain Name System translates human-readable names to IP addresses, the DNS hierarchy, record types, and tools for querying DNS.

繁體中文

概述

  • 學習目標:Netplan 作為 Ubuntu 網路設定層的運作方式、定義介面的 YAML 語法、靜態和 DHCP 定址、路由、DNS、綁定、VLAN,以及如何套用和排除設定問題。
  • 先決條件:第 14 課 – 路由與交換基礎
  • 預計閱讀時間:20 分鐘

簡介

Linux 上的網路設定歷來透過各種工具和檔案格式管理——從 Debian 上的 /etc/network/interfaces 到桌面系統的 NetworkManager。Ubuntu 17.10 引入了 Netplan,這是一個統一的、宣告式的基於 YAML 的設定層,位於底層網路守護程序(systemd-networkd 或 NetworkManager)之上。

Netplan 透過提供單一、一致的語法來簡化網路設定,無論使用哪個後端渲染器。您在 YAML 檔案中描述所需的網路狀態,Netplan 會為後端生成適當的設定。這種方法對於雲端部署和自動化佈建特別強大。

在本課程中,您將學習 Netplan 架構、掌握 YAML 設定語法、設定常見的網路場景,並學習如何套用、測試和排除設定問題。

Netplan 架構

Netplan 作為 YAML 設定檔和底層網路渲染器之間的轉換層運作。設定檔儲存在 /etc/netplan/ 中,並按字典序處理。Netplan 支援兩個後端渲染器:systemd-networkd(Ubuntu Server 的預設)和 NetworkManager(Ubuntu Desktop 的預設)。

靜態 IP 設定

最常見的 Netplan 任務是指定靜態 IP 位址。您指定介面、停用 DHCP,並提供位址、閘道和 DNS 伺服器。

多介面與路由

伺服器通常有多個網路介面——例如,一個用於公共流量,另一個用於私有管理網路。Netplan 可以輕鬆地獨立設定每個介面,各自擁有自己的位址、路由和 DNS 設定。

綁定與 VLAN

為了高可用性和網路分段,Netplan 支援介面綁定(將多個實體介面合併為一個邏輯介面)和 VLAN(在同一實體網路上分段流量的虛擬區域網路)。

測試與排除問題

Netplan 提供了一種安全的方式來在提交設定之前測試它們。netplan try 命令會暫時套用變更,如果您在逾時期限內未確認,則自動還原。

重要觀念

  • Netplan 是 Ubuntu 基於 YAML 的宣告式網路設定系統,為 systemd-networkd 或 NetworkManager 後端生成設定。
  • /etc/netplan/ 中的設定檔以簡潔、可讀的格式定義介面、位址、路由、DNS、綁定和 VLAN。
  • 使用 sudo netplan apply 啟動變更,使用 sudo netplan try 測試並自動回復以確保安全。
  • 多個介面可以獨立設定,各自擁有靜態位址、路由和名稱伺服器。
  • 套用前始終使用 sudo netplan generate 驗證,並使用 networkctl status 確認結果。

下一步

在第 16 課中,您將學習 DNS 基礎——網域名稱系統如何將人類可讀的名稱轉換為 IP 位址、DNS 層級結構、記錄類型,以及查詢 DNS 的工具。

日本語

概要

  • 学習内容:UbuntuのネットワークレイヤーとしてのNetplanの仕組み、インターフェースを定義するYAML構文、静的およびDHCPアドレッシング、ルート、DNS、ボンド、VLAN、設定の適用とトラブルシューティング方法。
  • 前提条件:レッスン14 – ルーティングとスイッチングの基礎
  • 推定読了時間:20分

はじめに

Linuxでのネットワーク設定は、歴史的にさまざまなツールとファイル形式を通じて管理されてきました——Debianの/etc/network/interfacesからデスクトップシステムのNetworkManagerまで。Ubuntu 17.10ではNetplanが導入されました。これは、基盤となるネットワークデーモン(systemd-networkdまたはNetworkManager)の上に位置する、統一された宣言型のYAMLベースの設定レイヤーです。

Netplanは、使用するバックエンドレンダラーに関係なく、単一の一貫した構文を提供することでネットワーク設定を簡素化します。YAMLファイルで望ましいネットワーク状態を記述すると、Netplanがバックエンド用の適切な設定を生成します。このアプローチはクラウドデプロイメントや自動プロビジョニングに特に強力です。

このレッスンでは、Netplanアーキテクチャを学び、YAML設定構文をマスターし、一般的なネットワークシナリオを設定し、設定の適用、テスト、トラブルシューティング方法を学びます。

Netplan アーキテクチャ

NetplanはYAML設定ファイルと基盤となるネットワークレンダラー間の変換レイヤーとして動作します。設定ファイルは/etc/netplan/に保存され、辞書順で処理されます。Netplanは2つのバックエンドレンダラーをサポートしています:systemd-networkd(Ubuntu Serverのデフォルト)とNetworkManager(Ubuntu Desktopのデフォルト)。

静的IP設定

最も一般的なNetplanタスクは静的IPアドレスの割り当てです。インターフェースを指定し、DHCPを無効にし、アドレス、ゲートウェイ、DNSサーバーを提供します。

複数インターフェースとルート

サーバーには複数のネットワークインターフェースがあることが多く、例えばパブリックトラフィック用と、プライベート管理ネットワーク用があります。Netplanでは各インターフェースを独自のアドレス、ルート、DNS設定で独立して設定できます。

ボンディングとVLAN

高可用性とネットワークセグメンテーションのために、Netplanはインターフェースボンディング(複数の物理インターフェースを1つの論理インターフェースに結合)とVLAN(同じ物理ネットワーク上のトラフィックをセグメント化する仮想LAN)をサポートしています。

テストとトラブルシューティング

Netplanは設定をコミットする前にテストする安全な方法を提供します。netplan tryコマンドは一時的に変更を適用し、タイムアウト期間内に確認しない場合は自動的にリバートします。

重要ポイント

  • NetplanはUbuntuの宣言型YAMLベースのネットワーク設定システムで、systemd-networkdまたはNetworkManagerバックエンド用の設定を生成します。
  • /etc/netplan/の設定ファイルは、インターフェース、アドレス、ルート、DNS、ボンド、VLANをクリーンで読みやすい形式で定義します。
  • sudo netplan applyで変更を有効にし、sudo netplan tryで安全のため自動ロールバック付きでテストします。
  • 複数のインターフェースを独立して設定でき、それぞれ独自の静的アドレス、ルート、ネームサーバーを持てます。
  • 適用前に必ずsudo netplan generateで検証し、networkctl statusで結果を確認してください。

次のステップ

レッスン16では、DNSの基礎を学びます——ドメインネームシステムが人間が読める名前をIPアドレスに変換する仕組み、DNS階層、レコードタイプ、DNSクエリツールについて学びます。

You Missed