netplan generate: `gateway4` has been deprecated, use default routes instead

When using netplan generate I kept on getting an error message:

gateway4 has been deprecated, use default routes instead.

For static IP address assignments in netplan, I’ve always used the structure:

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: false
      dhcp6: false
     addresses:
      - 192.168.10.10/24
     gateway4: 192.168.10.1
     nameservers:
      addresses: [192.168.10.1]

Obviously gateway4 is referencing gateway4: 192.168.10.1, but how do we fix it?

Asked By: erwin

||

The current syntax to replace gateway4 is routes with to and via.

For the netplan above based on the 192.168.10.0/24 network, it would be:

            routes:
                - to: default
                  via: 192.168.10.1

So the whole config would be:

network:
  version: 2
  renderer: networkd
  ethernets:
    eno1:
      dhcp4: false
      dhcp6: false
     addresses:
      - 192.168.10.10/24
     routes:
      - to: default
        via: 192.168.10.1
     nameservers:
       addresses: [192.168.10.1]

Currently there’s a very good reference full of practical examples at:
https://netplan.io/examples/

One other tip when working on netplan files, yamllint can save you a lot of trouble.

sudo apt install yamllint
sudo dnf install yamllint
sudo pacman -S yamllint

For example, I introduced a small formatting error:

      - to: default
      via: 192.168.10.1

Then when I run yamllint, I’ll get a line number that should help track down where the errors are.

yamllint /etc/netplan/01-netplan.yaml
28:9      error    syntax error: expected <block end>, but found '?' (syntax)

If you have any syntax errors (for example spacing issues) yamllint will give you the line numbers of your problems.

Hopefully this has made your netplan generate and netplan apply go smoothly!

sudo netplan generate
sudo netplan --debug apply
Answered By: erwin
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.