如何在 Ubuntu 22.04 上为 2 个网络接口设置 iproute2 路由表

如何在 Ubuntu 22.04 上为 2 个网络接口设置 iproute2 路由表

我想在 Ubuntu 22.04 上同时使用 2 个网络接口(enp7s0 和 enp8s0)。两者都有自己独立的互联网连接。我的默认是 enp8s0,我先插入了 LAN 电缆。我想我需要为此设置一个路由表。

所以我编辑了

/etc/iproute2/rt_tables

如下

1 rt2

ip route add 192.168.0.0/24 dev enp7s0 src 192.168.0.101 table rt2
ip route add default via 192.168.0.1 dev enp7s0 table rt2
ip rule add from 192.168.0.101/32 table rt2
ip rule add to 192.168.0.101/32 table rt2

这是我的 ifconfig

enp7s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.101  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::9c8b:3fe5:795e:bd32  prefixlen 64  scopeid 0x20<link>
        ether d8:5e:d3:01:84:ed  txqueuelen 1000  (Ethernet)
        RX packets 19214  bytes 18384493 (18.3 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11885  bytes 1705118 (1.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xfc700000-fc71ffff  

enp8s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.16  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 2403:6200:89a6:96ee:7aba:94ac:f7e5:421c  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::a09b:20eb:6e61:72b8  prefixlen 64  scopeid 0x20<link>
        inet6 2403:6200:89a6:96ee:33b5:37f2:4af1:bcbb  prefixlen 64  scopeid 0x0<global>
        ether d8:5e:d3:01:84:ee  txqueuelen 1000  (Ethernet)
        RX packets 4578155424  bytes 397941134593 (397.9 GB)
        RX errors 0  dropped 25613  overruns 0  frame 0
        TX packets 9865356056  bytes 14456549029829 (14.4 TB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

但当我这样做

ip 规则显示

我收到这个错误

Database /etc/iproute2/rt_tables is corrupted at ip route add 192.168.0.0/24 dev enp7s0 src 192.168.0.101 table rt2

0:  from all lookup local
32766:  from all lookup main
32767:  from all lookup default

我该如何解决?

可能是我没有设置

/etc/网络/接口

根据我在谷歌上找到的内容,我必须添加以下几行

auto lo
iface lo inet loopback

allow-hotplug enp7s0
iface enp7s0 inet static
    address 192.168.0.101
    netmask 255.255.255.0
    gateway 192.168.0.1

allow-hotplug enp8s0
iface enp8s0 inet static
    address 192.168.1.16
    netmask 255.255.255.0

但它不再存在于 Ubuntu 22.04 上,我听说他们改成了

/etc/netplan

但我找到的唯一文件是

01-网络管理器-全部.yaml

里面只有

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

答案1

通过使用解决

sudo ip route add 192.168.0.0/24 dev enp7s0 src 192.168.0.101 table rt2
sudo ip route add default via 192.168.0.1 dev enp7s0 table rt2
sudo ip rule add from 192.168.0.101/32 table rt2
sudo ip rule add to 192.168.0.101/32 table rt2

相关内容