How to set default gateway in Ubuntu Server 18.04 with netplan

31,787

Change your /etc/network/interfaces to...

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

Remove /etc/netplan/lo.yaml.

Change your /etc/netplan/01-netcfg.yaml file... and be aware of the specific indentation, and assure no tabs...

from:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
        enp2s0:
            addresses: [192.168.1.10/24]
            gateway4: 192.168.1.1
            nameservers:
                addresses: [1.1.1.1,1.0.0.1]
network:
    ethernets:
        lo:
            addresses: ['127.0.0.1/8']

to:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp2s0:
      addresses: [192.168.1.10/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [1.1.1.1, 1.0.0.1]

sudo netplan --debug generate # generate config files

sudo netplan apply # apply new config

reboot # mandatory reboot

Share:
31,787
Stefano
Author by

Stefano

Updated on September 18, 2022

Comments

  • Stefano
    Stefano over 1 year

    I have a single network interface on this server named enp2s0 This interface had a static address 192.168.2.xxx with gateway 192.168.2.1 Now I changed the static address in yaml file to 192.168.1.xxx and the gateway too in 192.168.1.1 but every time I restart the server the default gateway comes back to 192.168.2.1 I tried to launch route commmand to remove default gateway and set the new one but if I restart the server it comes back to 192.168.2.1

    This is the yaml config file
    
        # This file describes the network interfaces available on your system
    # For more information, see netplan(5).
    network:
      version: 2
      renderer: networkd
      ethernets:
            enp2s0:
                addresses: [192.168.1.10/24]
                gateway4: 192.168.1.1
                nameservers:
                    addresses: [1.1.1.1,1.0.0.1]
    

    Output of networkctl status enp2s0 after reboot

     2: enp2s0
           Link File: /lib/systemd/network/99-default.link
        Network File: /run/systemd/network/10-netplan-enp2s0.network
                Type: ether
               State: routable (configured)
                Path: pci-0000:02:00.0
              Driver: r8169
              Vendor: Realtek Semiconductor Co., Ltd.
               Model: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (M4A
          HW Address: 48:5b:39:02:41:c9 (ASUSTek COMPUTER INC.)
             Address: 192.168.1.10
                      192.168.2.100
                      fe80::4a5b:39ff:fe02:41c9
             Gateway: 192.168.2.1
                 DNS: 1.1.1.1
                      1.0.0.1
    

    Output of route command after reboot

    Tabella di routing IP del kernel
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         192.168.2.1     0.0.0.0         UG    202    0        0 enp2s0
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 enp2s0
    192.168.2.0     0.0.0.0         255.255.255.0   U     202    0        0 enp2s0
    

    Result of networkctl status enp2s0 after restored default gateway to 192.168.1.1

     2: enp2s0
           Link File: /lib/systemd/network/99-default.link
        Network File: /run/systemd/network/10-netplan-enp2s0.network
                Type: ether
               State: routable (configured)
                Path: pci-0000:02:00.0
              Driver: r8169
              Vendor: Realtek Semiconductor Co., Ltd.
               Model: RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (M4A
          HW Address: 48:5b:39:02:41:c9 (ASUSTek COMPUTER INC.)
             Address: 192.168.1.10
                      fe80::4a5b:39ff:fe02:41c9
             Gateway: 192.168.1.1
                 DNS: 1.1.1.1
                      1.0.0.1
    

    Output of route command after restored default gateway to 192.168.1.1

    Tabella di routing IP del kernel
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    default         192.168.1.1     0.0.0.0         UG    0      0        0 enp2s0
    192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 enp2s0
    

    This is the output of netplan --debug apply command

     ** (generate:4965): DEBUG: 16:17:35.743: Processing input file /etc/netplan/01-netcfg.yaml..
    ** (generate:4965): DEBUG: 16:17:35.744: starting new processing pass
    ** (generate:4965): DEBUG: 16:17:35.744: Processing input file /etc/netplan/lo.yaml..
    ** (generate:4965): DEBUG: 16:17:35.744: starting new processing pass
    ** (generate:4965): DEBUG: 16:17:35.744: enp2s0: setting default backend to 1
    ** (generate:4965): DEBUG: 16:17:35.744: Configuration is valid
    ** (generate:4965): DEBUG: 16:17:35.744: lo: setting default backend to 1
    ** (generate:4965): DEBUG: 16:17:35.744: Configuration is valid
    ** (generate:4965): DEBUG: 16:17:35.744: Generating output files..
    ** (generate:4965): DEBUG: 16:17:35.744: NetworkManager: definition enp2s0 is not for us (backend 1)
    ** (generate:4965): DEBUG: 16:17:35.744: NetworkManager: definition lo is not for us (backend 1)
    DEBUG:netplan generated networkd configuration changed, restarting networkd
    DEBUG:no netplan generated NM configuration exists
    DEBUG:enp2s0 not found in {}
    DEBUG:lo not found in {'enp2s0': {'addresses': ['192.168.1.10/24'], 'gateway4': '192.168.1.1', 'nameservers': {'addresses': ['1.1.1.1', '1.0.0.1']}}}
    DEBUG:Merged config:
    network:
      bonds: {}
      bridges: {}
      ethernets:
        enp2s0:
          addresses:
          - 192.168.1.10/24
          gateway4: 192.168.1.1
          nameservers:
            addresses:
            - 1.1.1.1
            - 1.0.0.1
        lo:
          addresses:
          - 127.0.0.1/8
      vlans: {}
      wifis: {}
    
    DEBUG:device lo operstate is unknown, not changing
    DEBUG:device enp2s0 operstate is up, not changing
    DEBUG:{}
    DEBUG:netplan triggering .link rules for lo
    DEBUG:netplan triggering .link rules for enp2s0
    

    @heynnema

    root@ubuntu:~# cat /etc/network/interfaces
    # ifupdown has been replaced by netplan(5) on this system.  See
    # /etc/netplan for current configuration.
    # To re-enable ifupdown on this system, you can run:
    #    sudo apt install ifupdown
    root@ubuntu:~# cat /etc/netplan/*.yaml
    # This file describes the network interfaces available on your system
    # For more information, see netplan(5).
    network:
      version: 2
      renderer: networkd
      ethernets:
            enp2s0:
                addresses: [192.168.1.10/24]
                gateway4: 192.168.1.1
                nameservers:
                    addresses: [1.1.1.1,1.0.0.1]
    network:
        ethernets:
            lo:
                addresses: ['127.0.0.1/8']
    

    How to solve ?