30

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?

1 Answer 1

54

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
5
  • 2
    notice how 'to' is vertically aligned wth 'via'. You will get a parsing error if indentation is not correct. See this question.
    – simonpa71
    Jun 30, 2022 at 15:41
  • @simonpa71 So you think that the via line for my gateway is actually being ignored but the default value is somehow working for me anyway? That's very possible. I did check with yamllint and I'm not getting a warning, but that doesn't mean it's wrong. Unfortunately it's a production machine for me, so not convenient to experiment... If you have a precise correction very happy to make it here and help others save time! Thanks for your support!
    – erwin
    Jul 1, 2022 at 6:29
  • 1
    on the contrary, I'm sure your solution is correct and working fine. In my attempts, I made the mistake of aligning 'via' with the '-' (minus bfore the 'to') and got a bunch of errors. Your suggestion of using yamllint is also on spot, and would have saved me some trial and error.
    – simonpa71
    Jul 1, 2022 at 9:10
  • @erwin Can you please provide your answer with a simple yamllint usage ?
    – SebMa
    Jul 21, 2022 at 9:44
  • @SebMa please see the updated version
    – erwin
    Jul 22, 2022 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.