如何使用 netplan 在 Ubuntu Server 18.04 中设置默认网关

如何使用 netplan 在 Ubuntu Server 18.04 中设置默认网关

我在此服务器上有一个名为 enp2s0 的网络接口。此接口具有静态地址 192.168.2.xxx,网关为 192.168.2.1。现在,我将 yaml 文件中的静态地址更改为 192.168.1.xxx,并将网关也更改为 192.168.1.1,但每次重新启动服务器时,默认网关都会回到 192.168.2.1。我尝试启动路由命令以删除默认网关并设置新的网关,但如果我重新启动服务器,它会回到 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]

重启后 networkctl status enp2s0 的输出

 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

重启后路由命令的输出

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

将默认网关恢复为 192.168.1.1 后 networkctl status enp2s0 的结果

 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

恢复默认网关为 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

这是 netplan --debug apply 命令的输出

 ** (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']

怎么解决 ?

答案1

将您的更改/etc/network/interfaces为...

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

消除/etc/netplan/lo.yaml

更改您的/etc/netplan/01-netcfg.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']

到:

# 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# 生成配置文件

sudo netplan apply# 应用新配置

reboot#强制重启

相关内容