XiaTools

How to Fix Incorrect Gateway IP Warnings in Netplan YAML Configurations

Updated 11 Oct 2026

The Netplan gateway subnet mismatch error occurs when your defined default gateway IP address does not fall within the exact network range of your static IP configuration. To resolve this, you must verify your network prefix length and ensure the gateway address is a valid host inside that exact subnet. Whether you are configuring Ubuntu Server for the first time or updating an existing deployment, getting your YAML indentation and IP logic right is critical for maintaining network reachability.

Understanding the Netplan Gateway Subnet Mismatch

Netplan is the default network configuration abstraction tool used in modern Ubuntu distributions. It reads YAML files located in /etc/netplan/ and renders the backend configuration for network managers like systemd-networkd or NetworkManager. When you apply a configuration using netplan apply, the utility performs strict validation checks on your routing and addressing.

A gateway subnet mismatch happens when the routing engine detects that the gateway IP cannot be reached directly via the local subnet mask assigned to the interface. For instance, if your static IP is 192.0.2.50/24 but you accidentally assign a gateway of 192.0.3.1, the system throws a validation or routing error because 192.0.3.1 sits outside the local 192.0.2.0/24 broadcast domain.

Why Validation Fails

Unlike older networking scripts that might silently accept invalid routing tables, Netplan enforces standards-compliant networking. If your subnet mask is too restrictive or your gateway address has a typo, the configuration will fail to apply, or worse, leave your server completely disconnected upon reboot. Before writing your YAML, you should always check your network parameters using the XiaTools Subnet Calculator to accurately determine valid host ranges, broadcast addresses, and usable gateway IPs for your assigned network prefix.

Diagnosing the Error

When a Netplan configuration fails due to a gateway and subnet mismatch, the symptoms usually appear immediately in your terminal or system logs.

Checking Netplan Output

When you run the apply command, Netplan will output an error indicating that the gateway is unreachable or invalid:

sudo netplan apply

Sample output:

Error: Invalid gateway '192.0.3.1' for interface 'eth0': must be a neighbor on the same subnet

Inspecting System Logs

If the error occurs during boot or via background daemon applications, you can check the system journal for detailed logs:

journalctl -u systemd-networkd -b

Look for error lines mentioning route additions, invalid gateways, or interface configuration failures.

Step-by-Step Guide to Fix Netplan Gateway Subnet Mismatch

Fixing this issue requires locating your configuration file, correcting the IP addresses, validating the syntax, and applying the changes safely.

Step 1: Locate Your Netplan YAML File

Netplan configuration files are typically named with a two-digit prefix and a .yaml extension inside the /etc/netplan/ directory.

ls -l /etc/netplan/

You might see a file named 01-netcfg.yaml or 50-cloud-init.yaml. Open this file with your preferred text editor using administrative privileges:

sudo nano /etc/netplan/01-netcfg.yaml

Step 2: Review Your IP and Gateway Configuration

Examine the network block. A typical misconfigured file might look like this:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.0.2.50/24
      gateway4: 192.0.3.1
      nameservers:
        addresses:
          - 192.0.2.2
          - 8.8.8.8

In this example, the address is 192.0.2.50/24 (which covers 192.0.2.1 through 192.0.2.254), but the gateway is set to 192.0.3.1, triggering the mismatch error.

Step 3: Correct the YAML Syntax and Gateway

Update the gateway IP to fall within the 192.0.2.0/24 range, such as 192.0.2.1:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses:
        - 192.0.2.50/24
      routes:
        - to: default
          via: 192.0.2.1
      nameservers:
        addresses:
          - 192.0.2.2
          - 8.8.8.8

Note: Modern Netplan versions prefer the explicit routes block for default gateways over the deprecated gateway4 directive, though both are parsed depending on the version.

Step 4: Test and Apply Safely

Never run netplan apply blindly on remote servers without a safety net. Use the try command instead, which automatically reverts changes if you lose connectivity within a specified timeout period:

sudo netplan try --timeout 30

If your configuration is correct, press ENTER to accept the changes. If the configuration is faulty or locks you out, Netplan will automatically restore the previous working network state after 30 seconds.

Comparison of Gateway Configuration Methods

Method Syntax Style Compatibility Recommendation
Legacy gateway4 gateway4: 192.0.2.1 Older Ubuntu versions Avoid on new setups
Explicit Routes routes: [{to: default, via: 192.0.2.1}] All modern versions Recommended standard
Policy Routing Custom route tables and metrics Advanced multi-gateway setups Use only when needed

Common Mistakes and How to Fix Them

Even experienced engineers occasionally run into subtle Netplan formatting pitfalls. Watch out for these frequent issues:

  • Tab Characters in YAML: YAML specifications strictly forbid the use of tab characters for indentation. Always use spaces (typically 2 or 4 spaces per level). If your text editor inserts tabs, configure it to convert tabs to spaces.
  • Incorrect CIDR Prefix Length: Typing /28 instead of /24 changes your usable host range dramatically. Always calculate your network boundaries beforehand.
  • Mixing IPv4 and IPv6 Syntax: Ensure your gateway IP matches the IP family of your address block. Do not assign an IPv6 gateway inside an IPv4-only route block.
  • Forgetting Network Manager Backends: If you are running desktop Ubuntu, ensure your renderer matches NetworkManager, whereas headless servers should use networkd.

Netplan Troubleshooting Checklist

Use this quick checklist whenever you encounter routing or gateway errors in Netplan:

  1. Verify that all indentation in your YAML file uses consistent spaces, not tabs.
  2. Confirm that your static IP address and gateway IP belong to the exact same subnet.
  3. Use ip addr show and ip route show to inspect current active kernel routes.
  4. Always test configuration changes using sudo netplan try instead of sudo netplan apply on production remote servers.
  5. Check system journal logs with journalctl if network services fail to start.

Frequently asked questions

Why does Netplan reject my gateway IP address?

Netplan rejects gateway IPs that do not reside within the subnet defined by your interface's IP address and prefix length. The system requires the gateway to be a direct neighbor on the local network segment so it can resolve its MAC address via ARP.

What is the difference between gateway4 and the routes block?

The gateway4 directive is a shorthand configuration method supported in older Netplan releases for IPv4 default gateways. Modern Netplan versions prefer the explicit routes block containing a destination of default and a via address, which provides greater flexibility for complex routing.

How can I test my Netplan configuration without locking myself out of a remote server?

Always use the sudo netplan try command followed by a timeout value like --timeout 30. This command applies the network settings temporarily and asks you to confirm them; if you lose SSH connectivity and do not confirm, it automatically reverts to the working configuration.

Can I configure multiple gateways on a single Netplan interface?

Yes, you can configure multiple routes inside the routes block by specifying different destination networks and metrics. However, you can only have one primary default gateway per routing table unless you implement advanced policy-based routing with custom tables.

Why does my YAML file throw syntax errors even though the IPs look correct?

YAML files are extremely sensitive to indentation and spacing. Using tab characters instead of spaces, or having inconsistent indentation levels across lines, will cause the YAML parser to fail before it even validates the IP addresses.

Related articles

Free tools