Skip to content

Configure static IPs, VLANs and an LACP bond with Netplan on Ubuntu

Learn how to set a static IP, VLANs and an LACP bond with Netplan on Ubuntu, using the current routes syntax instead of the deprecated gateway4 key. You will also use netplan try, which rolls back a bad change automatically so you never lock yourself out of a remote server.

This guide shows Ubuntu server admins how to configure networking with Netplan, the tool Ubuntu uses to manage network settings. You write YAML files in /etc/netplan/, and Netplan turns them into settings for the backend that does the work: systemd-networkd on servers, or NetworkManager on desktops. Three things cause most of the frustration. The gateway4: key that most tutorials have used since 2018 is deprecated, so Netplan now prints a warning each time it reads it. Indentation mistakes are rejected with error messages that point at the wrong line. And running netplan apply with a bad config on a remote server can drop your SSH session with no way back in. This guide covers static IPv4 and IPv6 addresses with proper routes: entries, plus DNS through systemd-resolved, Ubuntu’s built-in name-resolution service. It also covers 802.1Q VLANs (tagged virtual networks that share one physical link) on an LACP bond, which combines several network ports into one faster, redundant link. Finally, you will use the netplan try rollback timer, so a mistake costs you 120 seconds instead of a trip to the datacentre.

What you’ll accomplish

  • Replace DHCP or a half-migrated config with one readable Netplan file that owns the whole host network.
  • Assign static IPv4 and IPv6 addresses with a default route written the modern way: routes: with to: default.
  • Bond two physical NICs into an LACP aggregate and carry two tagged VLANs over it.
  • Set per-link DNS servers and a search domain that systemd-resolved actually honours.
  • Apply every change with an automatic rollback in place, and know the recovery path for when it still goes wrong.

Prerequisites

  • Ubuntu 18.04 or newer. This is written against Netplan 0.106 and later, which ships from Ubuntu 22.04 onward; netplan status does not exist on older releases. Check with dpkg -s netplan.io.
  • root or sudo, and — this matters more than anything else here — a way back in that does not use the network you are about to change: serial console, hypervisor console, IPMI or iDRAC, or a cloud provider’s web console.
  • Two physical NICs on the same switch for the bond section, plus a switch-side port-channel with LACP enabled and the VLANs trunked to those ports. Linux cannot compensate for an unconfigured switch.
  • Honest version differences. Ubuntu Server defaults to the networkd renderer; Desktop to NetworkManager. gateway4: and gateway6: have been deprecated since Netplan 0.103, still warn on current releases, and are being removed — do not write new configs with them. On cloud images, cloud-init rewrites /etc/netplan/50-cloud-init.yaml, covered in step 8.

How it works

Netplan is not a network manager. It is a translator that runs once, reads every *.yaml in /etc/netplan/ in lexical filename order, merges them into one model, and writes native configuration for the backend named by renderer:. For networkd that means .network, .netdev and .link files under /run/systemd/network/; for NetworkManager, .nmconnection files under /run/NetworkManager/system-connections/. Both paths live on tmpfs and are regenerated from scratch on every run, so hand-editing them is pointless.

Diagram showing YAML files in /etc/netplan read in lexical order, merged by netplan generate, dispatched to either the networkd or NetworkManager renderer, and emitted as backend configuration under /run, with a lower band comparing what netplan generate, netplan apply and netplan try each do and risk

Merging is per key, not per file. A 99-static.yaml defining dhcp4: false on enp3s0f0 overrides that key in 50-cloud-init.yaml but leaves every other key in that file intact — which is how people end up with an interface holding both a static address and a DHCP lease. netplan get all prints the merged result.

The three commands map onto three levels of risk. netplan generate validates the YAML and writes backend files, changing nothing on the running system. netplan apply does that and reloads the backend, taking changes live with no safety net. netplan try applies the config, prints a prompt, and if you do not press Enter within 120 seconds it restores the previous /etc/netplan contents and re-applies them. On a remote host, netplan try is the command; netplan apply is for when you are at a console.

Bonds and VLANs stack. The physical NICs get no addresses and exist only to be enslaved. The bond aggregates them and, in 802.3ad mode, negotiates LACP with the switch. The VLAN interfaces hang off the bond, each tagged with its 802.1Q ID, and carry the addresses, routes and DNS.

Step-by-step

  1. Record the current state. You will want it if something goes sideways, and interface names are not guessable.

    ip -br link show
    ip -br address show
    ip route show

    Expected: one line per interface — enp3s0f0 DOWN a0:36:9f:1a:2b:3c <BROADCAST,MULTICAST>. Note the exact predictable names; a typo here produces a config that validates cleanly and does nothing. ip route show should contain a default via ... line. Copy all of it to your laptop, not the server.

  2. Back up the config and arm a dead-man rollback. netplan try is the primary safety net, but it does not protect against a config that stays reachable for 20 seconds and then falls over. A scheduled restore does.

    sudo cp -a /etc/netplan /root/netplan-backup
    sudo systemd-run --on-active=15min --unit=netplan-rollback \
      /bin/sh -c 'cp -a /root/netplan-backup/. /etc/netplan/ && netplan apply'

    Expected: systemd-run prints Running timer as unit: netplan-rollback.timer and Will run service as unit: netplan-rollback.service. You now have 15 minutes; lose the session and the box restores its old networking on its own. Step 7 cancels it.

  3. Clear the ground. Move old config aside so exactly one file defines this host’s network. Keeping 50-cloud-init.yaml next to a hand-written file is the most common source of “my static address keeps picking up a DHCP lease”.

    sudo mv /etc/netplan/50-cloud-init.yaml /root/netplan-backup/
    ls -l /etc/netplan/

    Expected: ls shows an empty directory, or only files you deliberately kept. Move 00-installer-config.yaml from the Ubuntu installer too, if it is there.

  4. Write the config. One file, mode 0600, a name that sorts last so nothing overrides it. Note the two-space indentation, the absence of tabs, and to: default in place of gateway4:.

    sudo tee /etc/netplan/99-corp-net.yaml >/dev/null <<'EOF'
    network:
      version: 2
      renderer: networkd
      ethernets:
        # physical ports carry no addresses; they only feed the bond
        enp3s0f0:
          dhcp4: false
          dhcp6: false
        enp3s0f1:
          dhcp4: false
          dhcp6: false
      bonds:
        bond0:
          interfaces: [enp3s0f0, enp3s0f1]
          parameters:
            mode: 802.3ad
            lacp-rate: fast
            mii-monitor-interval: 100
            transmit-hash-policy: layer3+4
      vlans:
        vlan100:
          id: 100
          link: bond0
          # static v6 means router advertisements must not also configure us
          accept-ra: false
          addresses:
            - 10.20.100.11/24
            - 2001:db8:100::11/64
          routes:
            - to: default
              via: 10.20.100.1
            - to: default
              via: 2001:db8:100::1
          nameservers:
            addresses: [10.20.100.53, 2001:db8:100::53]
            search: [corp.example.com]
        vlan200:
          id: 200
          link: bond0
          # storage VLAN: no gateway, and the boot must not wait for it
          optional: true
          addresses:
            - 10.20.200.11/24
    EOF
    sudo chmod 600 /etc/netplan/99-corp-net.yaml

    Expected: both commands return silently, and ls -l shows -rw-------. Skip the chmod and current releases print Permissions for /etc/netplan/99-corp-net.yaml are too open. on every invocation.

    Two keys deserve a note. optional: true stops systemd-networkd-wait-online blocking boot on that interface — without it a disconnected storage NIC adds two minutes to every reboot. And if your gateway sits outside your own subnet, as some hosting providers arrange deliberately, add on-link: true to that route entry.

  5. Validate the YAML without touching the network. This is free and catches most mistakes.

    sudo netplan generate

    Expected: complete silence and exit status 0. Anything else must be fixed first — a message like Error in network definition //etc/netplan/99-corp-net.yaml line 14 column 8: expected mapping means indentation, and the reported line is where the parser gave up, often past the real mistake. Then confirm what Netplan built:

    ls /run/systemd/network/
    netplan get all

    Expected: ls shows a 10-netplan-<device>.network file per device plus a .netdev for each virtual one — 10-netplan-bond0.netdev, 10-netplan-vlan100.netdev and so on. netplan get all prints the merged model; read it and check no stray keys survived from a file you thought you had removed.

    Diagram of two physical NICs feeding a bond0 LACP aggregate in 802.3ad mode with no address of its own, which in turn carries two 802.1Q tagged VLAN interfaces, vlan100 with the default route and vlan200 for storage, plus a note that the switch must have a matching port-channel and trunk

  6. Apply it with the rollback timer. Never netplan apply a change like this over SSH.

    sudo netplan try --timeout 120

    Expected: the config goes live and you get Do you want to keep these settings?, then Press ENTER before the timeout to accept the new configuration and Changes will revert in 120 seconds. Open a second SSH session to the new address before pressing anything. If it connects, press Enter in the first one and you get Configuration accepted. If it does not, do nothing — the countdown expires and the old config is restored and re-applied.

  7. Disarm the dead-man switch, once a fresh connection is confirmed working.

    sudo systemctl stop netplan-rollback.timer
    systemctl list-timers netplan-rollback.timer

    Expected: the second command prints a header and 0 timers listed. Forget this and your config silently reverts mid-afternoon.

  8. Stop cloud-init from rewriting your work. On any cloud or MAAS-provisioned image, cloud-init regenerates /etc/netplan/50-cloud-init.yaml on boot. You deleted the file; cloud-init will put it back.

    echo 'network: {config: disabled}' | \
      sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

    Expected: the file is echoed back. On bare metal there is no cloud-init and this is a no-op — check with command -v cloud-init, which prints nothing if absent.

  9. Reboot and confirm it survives. A config that works after netplan try but not after a reboot will fail at the worst moment.

    sudo reboot

    Once it is back, run the verification below. If it does not come back, that is what the console is for.

Validate it works

Start with addressing and routes, both families:

ip -br address show
ip route show
ip -6 route show

Expected: bond0 shows UP with no address, vlan100@bond0 shows UP 10.20.100.11/24 2001:db8:100::11/64, vlan200@bond0 its storage address. ip route show contains default via 10.20.100.1 dev vlan100 proto static, and ip -6 route show contains default via 2001:db8:100::1 dev vlan100 proto static metric 1024 pref medium. A missing default line means the routes: block is indented under the wrong device.

Confirm the bond negotiated LACP rather than quietly running on one link:

cat /proc/net/bonding/bond0

Expected, abridged:

Bonding Mode: IEEE 802.3ad Dynamic link aggregation
Transmit Hash Policy: layer3+4 (1)
MII Status: up
802.3ad info
LACP active: on
LACP rate: fast
Active Aggregator Info:
	Number of ports: 2
	Partner Mac Address: 00:23:04:ee:be:64

Number of ports: 2 and a non-zero Partner Mac Address prove the switch is talking LACP back to you. Number of ports: 1, or a partner address of 00:00:00:00:00:00, means the switch side is not configured and you are running on a single link.

Check the VLAN tagging is real, not just a naming convention:

ip -d link show vlan100

Expected: the output contains vlan protocol 802.1Q id 100 <REORDER_HDR>. If id is wrong, traffic leaves the host tagged for a VLAN the switch is not trunking, and simply disappears.

Verify DNS is bound to the link, not just written into a file:

resolvectl status vlan100
resolvectl query db.corp.example.com

Expected: Current Scopes: DNS, +DefaultRoute in the protocol line, DNS Servers: 10.20.100.53 2001:db8:100::53 and DNS Domain: corp.example.com. The query should resolve and report -- Information acquired via protocol DNS. A stub resolver that is not wired up correctly is its own topic — see our systemd-resolved configuration guide.

Finally, Netplan’s own summary, on 22.04 and newer:

netplan status --all

Expected: Online state: online, the DNS addresses and search domain at the top, then a block per interface — bond0 bond UP (networkd: bond0), vlan100 vlan UP (networkd: vlan100) with its addresses. Anything marked (unmanaged) that you expected Netplan to own never made it into the YAML.

Troubleshooting

  • The YAML is rejected and the line number is useless. Netplan reports where the parser failed, not where you erred, so an error at line 22 is frequently an indentation slip at line 19. Three causes cover nearly all of it: a literal tab anywhere in the file (YAML forbids them — find them with grep -Pn '\t' /etc/netplan/*.yaml), a list written without the leading - , and a key indented one level off so it attaches to the parent instead of the device.

  • gateway4 produces a warning and no default route. A config copied from an older tutorial gives you WARNING:root:gateway4 has been deprecated, use default routes instead. and, on current releases, no default route. Replace the key with a routes: list containing - to: default and via: set to your gateway. Same for gateway6:. There is no compatibility shim worth relying on.

  • You applied a bad config and lost the session. This is why step 2 exists, but if you are here without it: get on the console — hypervisor, IPMI, serial or cloud web console — log in locally and restore. sudo mv /etc/netplan/99-corp-net.yaml /root/ then sudo netplan apply puts you back on whatever the remaining files describe. If /etc/netplan/ is now empty, the fastest route to a reachable shell is a temporary DHCP config: sudo netplan set ethernets.enp3s0f0.dhcp4=true then sudo netplan apply, which writes a valid file and brings the interface up. If the console is unavailable too, boot the recovery kernel from GRUB, pick root, run mount -o remount,rw /, and fix the file there.

  • The bond comes up but throughput is stuck at one link’s worth. Almost always the switch, not Linux. Number of ports: 1 in /proc/net/bonding/bond0 means only one member joined the aggregator; a partner MAC of all zeros means no LACP frames are coming back. Get the port-channel configured for both ports. Once LACP is up, remember that transmit-hash-policy distributes flows, not packets — a single TCP stream never exceeds one member link’s bandwidth, so test with parallel connections.

  • DNS settings are ignored. Two causes. Either nameservers: sits on a device with no default route — systemd-resolved only promotes a link’s DNS servers to global use when that link owns the default route, which is why the block belongs on vlan100 — or /etc/resolv.conf is a real file rather than a symlink to /run/systemd/resolve/stub-resolv.conf. Check with ls -l /etc/resolv.conf and restore the symlink if another tool replaced it.

When this is the wrong approach

Netplan owns the host’s static topology and is the wrong tool for anything that changes at runtime. On a laptop that roams between networks, use NetworkManager directly; putting Netplan in front makes nmcli results confusing for no gain. Inside a container, networking belongs to the runtime. On Kubernetes nodes the CNI plugin owns pod networking — Netplan configures the node’s uplinks and stops there.

It is also the wrong layer for dynamic routing. Netplan handles static routes and, through routing-policy:, source-based policy routing, but BGP or OSPF calls for FRRouting on top of a Netplan-configured interface. Provider images that expect DHCP plus metadata-driven configuration are best left alone; fighting cloud-init on an autoscaled instance creates failures that only appear when the instance is replaced.

One more boundary: a new VLAN interface is new attack surface, and Netplan has no opinion about filtering. See UFW firewall basics for the per-interface rules that belong alongside this config.

The authoritative references are the Netplan documentation, which carries the full YAML schema for every device type, and the Ubuntu Server documentation.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.