路线和规则 Ubuntu 18.04.2

路线和规则 Ubuntu 18.04.2

使用虚拟 Hyper-V 服务器 Ubuntu 18.04 LTS,安装了带有五个 squid 网关接口的设备。

我确切地说是当使用代理服务时,通过位于 eth0 接口的 IP 连接到互联网时,它会通过位于 eth0 上的 IP 连接到其他接口。

要重新创建带有默认列表的新列表,请按照以下说明为所有接口和路由策略设置一个无 IP 连接的接口,以便使用之前创建的列表来创建它们。

ifconfig 命令的结果

eth0: flags=4163  mtu 1500
        inet 189.189.10.4  netmask 255.255.255.0  broadcast 189.189.10.255
        inet6 fe80::215:5dff:fe5a:425e  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:5e  txqueuelen 1000  (Ethernet)
        RX packets 216690  bytes 15705511 (15.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 62685  bytes 4658403 (4.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163  mtu 1500
        inet 189.189.20.4  netmask 255.255.255.0  broadcast 189.189.20.255
        inet6 fe80::215:5dff:fe5a:425f  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:5f  txqueuelen 1000  (Ethernet)
        RX packets 154552  bytes 9944248 (9.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32754  bytes 2319788 (2.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=4163  mtu 1500
        inet 189.189.30.4  netmask 255.255.255.0  broadcast 189.189.30.255
        inet6 fe80::215:5dff:fe5a:4260  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:60  txqueuelen 1000  (Ethernet)
        RX packets 154485  bytes 9940210 (9.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32836  bytes 2330587 (2.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=4163  mtu 1500
        inet 189.189.40.4  netmask 255.255.255.0  broadcast 189.189.40.255
        inet6 fe80::215:5dff:fe5a:4261  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:61  txqueuelen 1000  (Ethernet)
        RX packets 117812  bytes 11016717 (11.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 34004  bytes 2504658 (2.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth4: flags=4163  mtu 1500
        inet 189.189.50.4  netmask 255.255.255.0  broadcast 189.189.50.255
        inet6 fe80::215:5dff:fe5a:4262  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:62  txqueuelen 1000  (Ethernet)
        RX packets 118058  bytes 7940984 (7.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35226  bytes 2705527 (2.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 3101  bytes 159988 (159.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3101  bytes 159988 (159.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

配置示例 /etc/netplan/01-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth1:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth2:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth3:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth4:
      dhcp4: yes

轮值表

ip route add default via 189.189.10.254 dev eth0 table 10
ip route add default via 189.189.20.254 dev eth1 table 20
ip route add default via 189.189.30.254 dev eth2 table 30
ip route add default via 189.189.40.254 dev eth3 table 40
ip route add default via 189.189.50.254 dev eth4 table 50

规则配置

ip rule add from 189.189.10.4 table 10
ip rule add from 189.189.20.4 table 20
ip rule add from 189.189.30.4 table 30
ip rule add from 189.189.40.4 table 40
ip rule add from 189.189.50.4 table 50

遇到的问题是,当服务器重新启动时,系统会触发注册表和创建定时器,然后执行保存并关闭虚拟机,然后重新启动定时器,创建一个永久运行的网络接口,该永久运行的接口是网关的接口,该接口位于默认定时器的第一个端口,例如“default via 189.189.50.254 dev eth4 proto dhcp src 189.189.50.4 metric 100”,正确的是,定时器和定时器的配置最终会持续更长时间所参考的参考资料和执行的程序均未得到结果。

结果 ip route show

default via 189.189.50.254 dev eth4 proto dhcp src 189.189.50.4 metric 100
default via 189.189.40.254 dev eth3 proto dhcp src 167.249.40.4 metric 100
default via 189.189.30.254 dev eth2 proto dhcp src 189.189.30.254 metric 100
default via 189.189.10.254 dev eth1 proto dhcp src 189.189.10.254 metric 100
default via 189.189.20.254 dev eth0 proto dhcp src 189.189.20.254 metric 100

观察:

  1. 所添加的 IP 均属于虚构问题。
  2. 操作系统通过 DHCP 分配 IP。
  3. 当旋转和规则组合配置时,会使用 Squid 的配置或弯曲操作进行配置。

---------------------------英文部分 (谷歌翻译)-------------------------------------

谷歌翻译:

它是一个 Ubuntu 18.04 LTS Hyper-V 虚拟服务器,有五个网络接口和安装了 squid。

我需要的是,当有人使用代理服务连接到 eth0 接口中的 IP 时,到互联网的输出是通过 eth0 中的 IP,依此类推到其他接口。

为了实现该行为,我创建了新的路由表,每个接口一个默认路由,并建立路由策略,如果连接在相应接口的 IP 中,则必须使用先前创建的相应路由表。

ifconfig 命令结果

eth0: flags=4163  mtu 1500
        inet 189.189.10.4  netmask 255.255.255.0  broadcast 189.189.10.255
        inet6 fe80::215:5dff:fe5a:425e  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:5e  txqueuelen 1000  (Ethernet)
        RX packets 216690  bytes 15705511 (15.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 62685  bytes 4658403 (4.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4163  mtu 1500
        inet 189.189.20.4  netmask 255.255.255.0  broadcast 189.189.20.255
        inet6 fe80::215:5dff:fe5a:425f  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:5f  txqueuelen 1000  (Ethernet)
        RX packets 154552  bytes 9944248 (9.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32754  bytes 2319788 (2.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth2: flags=4163  mtu 1500
        inet 189.189.30.4  netmask 255.255.255.0  broadcast 189.189.30.255
        inet6 fe80::215:5dff:fe5a:4260  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:60  txqueuelen 1000  (Ethernet)
        RX packets 154485  bytes 9940210 (9.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32836  bytes 2330587 (2.3 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth3: flags=4163  mtu 1500
        inet 189.189.40.4  netmask 255.255.255.0  broadcast 189.189.40.255
        inet6 fe80::215:5dff:fe5a:4261  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:61  txqueuelen 1000  (Ethernet)
        RX packets 117812  bytes 11016717 (11.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 34004  bytes 2504658 (2.5 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth4: flags=4163  mtu 1500
        inet 189.189.50.4  netmask 255.255.255.0  broadcast 189.189.50.255
        inet6 fe80::215:5dff:fe5a:4262  prefixlen 64  scopeid 0x20
        ether 00:15:5d:5a:42:62  txqueuelen 1000  (Ethernet)
        RX packets 118058  bytes 7940984 (7.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 35226  bytes 2705527 (2.7 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 3101  bytes 159988 (159.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3101  bytes 159988 (159.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

/etc/netplan/01-netcfg.yaml 上的内容

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth1:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth2:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth3:
      dhcp4: yes
network:
  version: 2
  renderer: networkd
  ethernets:
    eth4:
      dhcp4: yes

执行的 IP 路由命令

ip route add default via 189.189.10.254 dev eth0 table 10
ip route add default via 189.189.20.254 dev eth1 table 20
ip route add default via 189.189.30.254 dev eth2 table 30
ip route add default via 189.189.40.254 dev eth3 table 40
ip route add default via 189.189.50.254 dev eth4 table 50

执行的 IP 规则命令

ip rule add from 189.189.10.4 table 10
ip rule add from 189.189.20.4 table 20
ip rule add from 189.189.30.4 table 30
ip rule add from 189.189.40.4 table 40
ip rule add from 189.189.50.4 table 50

谷歌翻译:

我面临的问题是,当我重新启动服务器时,我创建的规则和路由会被删除,而当我执行保存并在虚拟机中启动后,路由也会被删除,并且只有一个网络接口仍在工作,并且永久运行的接口是网关位于默认路由表的第一个默认路由中的接口,在“默认通过 189.189.50.254 dev eth4 proto dhcp src 189.189.50.4 metric 100”的情况下,我需要的是我所做的路由设置和规则是持久的,但我查阅和执行的程序没有起作用。

Ip 路由显示命令

default via 189.189.50.254 dev eth4 proto dhcp src 189.189.50.4 metric 100
default via 189.189.40.254 dev eth3 proto dhcp src 167.249.40.4 metric 100
default via 189.189.30.254 dev eth2 proto dhcp src 189.189.30.254 metric 100
default via 189.189.10.254 dev eth1 proto dhcp src 189.189.10.254 metric 100
default via 189.189.20.254 dev eth0 proto dhcp src 189.189.20.254 metric 100

评论:

  1. 问题中添加的IP是虚构的。
  2. 必须通过 DHCP 分配 IP。
  3. 当路由和规则与 Squid 配置结合时,就可以实现预期的行为。

相关内容