sodcalculator

10 Tech How-To

Configure Networking with Netplan on Ubuntu

Guide 10-57352 · Updated · Step by step

Netplan is Ubuntu’s default network configuration tool, using simple YAML files to manage network interfaces on modern releases (17.10 and later). This guide walks you through configuring static IPs, DHCP, and other common settings, then applying them safely. You’ll need a terminal with sudo access and a basic understanding of your network’s IP scheme.

What You’ll Need

  • A system running Ubuntu 17.10 or newer (20.04, 22.04, 24.04, etc.)
  • Sudo or root access to edit files and run commands
  • Knowledge of your network: desired static IP, subnet mask, gateway, and DNS servers
  • The name of your network interface (e.g., eth0, ens33, wlp2s0) – find it with ip link or ls /sys/class/net

Step-by-Step

  1. Identify your network interface. Run ip link or ls /sys/class/net to list interfaces. Note the one you want to configure (often named like ens33, eth0, or wlp2s0).
  2. Back up the current Netplan configuration. Netplan configs live in /etc/netplan/. Copy the existing YAML file (e.g., sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak). If the directory is empty, you’ll create a new file.
  3. Open the config file with a text editor. Use sudo nano /etc/netplan/01-netcfg.yaml (or the actual filename). If no file exists, create one with that name.
  4. Write the YAML configuration. Start with the required header and specify the renderer (networkd is standard for servers, NetworkManager for desktop). For a static IP, use a structure like this:
    network:
      version: 2
      renderer: networkd
      ethernets:
        ens33:
          dhcp4: no
          addresses:
            - 192.168.1.100/24
          routes:
            - to: default
              via: 192.168.1.1
          nameservers:
            addresses: [8.8.8.8, 8.8.4.4]

    For DHCP, simply set dhcp4: yes and omit addresses/routes/nameservers.

  5. Check the indentation carefully. YAML is strict: use spaces, not tabs. The example above uses two-space indentation. A single misaligned line will cause an error.
  6. Test the configuration before applying. Run sudo netplan try. This applies the config temporarily and waits for your confirmation (default 120 seconds). If you lose connectivity, it automatically reverts. Press Enter to accept or wait for rollback.
  7. Apply the configuration permanently. If netplan try succeeded, run sudo netplan apply to make it active immediately.
  8. Verify the settings. Check with ip addr show ens33 (replace interface name) and ip route to confirm IP and gateway. Test DNS with ping -c 4 google.com.

Common Mistakes

  • Incorrect YAML indentation: Using tabs or inconsistent spaces triggers a parse error. Always use spaces and align lists properly.
  • Wrong interface name: Using eth0 when your system uses ens33 will result in no changes. Double-check with ip link.
  • Missing gateway or DNS: Without a default route, you can’t reach the internet. Without DNS, hostnames won’t resolve.
  • Forgetting to set dhcp4: no: If you leave DHCP enabled while also specifying a static IP, the system may still get a DHCP address.
  • Editing the wrong file: There may be multiple YAML files in /etc/netplan/. Netplan merges them; editing the wrong one can cause conflicts. Check which files exist and edit the one that defines your interface.

Tips

  • Use sudo netplan generate to check for syntax errors without applying changes. It will report any issues.
  • For wireless connections, use wifis: instead of ethernets: and include an access-points: block with the SSID and password.
  • If you’re on a desktop with NetworkManager, set renderer: NetworkManager to avoid conflicts.
  • To add multiple IP addresses, list them under addresses: with each on a new line.
  • Use optional: true for interfaces that might not be present at boot (like USB adapters) to prevent boot delays.

Troubleshooting

  • “Invalid YAML” error: Re-check indentation and colons. Run sudo netplan generate to get a more specific error message.
  • No network after apply: If you’re connected via SSH, you’ll be disconnected. Use the console (physical or virtual) to run sudo netplan try again and let it time out to roll back. Then fix the config.
  • Interface not found: The name in the config doesn’t match any real interface. Run ip link and correct the name.
  • Can ping IP but not hostname: DNS is misconfigured. Check nameservers in the YAML and test with dig google.com or nslookup.
  • Gateway unreachable: Verify the gateway IP is correct and on the same subnet as your static IP. Check with ip route and try pinging the gateway.
  • Changes not persisting after reboot: Ensure you ran sudo netplan apply (not just try). Also confirm the YAML file is in /etc/netplan/ and has correct permissions (usually 600).

Frequently Asked Questions

Can I use Netplan on older Ubuntu versions?

Netplan is default only from 17.10 onward. On older versions, use /etc/network/interfaces or install netplan manually (not recommended).

How do I switch from DHCP to a static IP?

Edit the YAML file, set dhcp4: no, add your addresses, routes, and nameservers, then run sudo netplan apply.

What is the difference between netplan try and netplan apply?

try applies temporarily and rolls back after 120 seconds if you don’t confirm. apply makes changes permanent immediately. Always use try first when working remotely.

Why does my config file have a different name?

Netplan filenames are arbitrary; they just need to end in .yaml. Common names include 01-netcfg.yaml, 50-cloud-init.yaml, or 99-config.yaml. Use whatever exists.

How do I configure a VLAN or bond?

Netplan supports these via vlans: and bonds: sections. For example, a VLAN 10 on eth0: vlans: { vlan10: { id: 10, link: eth0 } }. Refer to the official Netplan documentation for full syntax.

Can I use Netplan with NetworkManager?

Yes, set renderer: NetworkManager at the top level. This is common on desktop installations where NetworkManager manages Wi-Fi and other connections.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →