38

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?

erwin
  • 1,723
  • 1
  • 9
  • 18

1 Answers1

68

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
erwin
  • 1,723
  • 1
  • 9
  • 18
  • 2
    notice how 'to' is vertically aligned wth 'via'. You will get a parsing error if indentation is not correct. See [this question](https://stackoverflow.com/questions/42247535/yaml-how-many-spaces-per-indent). – simonpa71 Jun 30 '22 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 01 '22 at 06: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 01 '22 at 09:10
  • @erwin Can you please provide your answer with a simple `yamllint` usage ? – SebMa Jul 21 '22 at 09:44
  • @SebMa please see the updated version – erwin Jul 22 '22 at 10:08