假设我的路由器有一个 LAN (192.168.0.1/24) 和一个 DMZ (192.168.1.1/24)。我的电脑有 2 个网卡,一个用于 LAN,一个用于 DMZ。
如何配置我的路由,使其使用两个网关而不是默认网关?
输出ifconfig
:
eth0 Link encap:Ethernet HWaddr 00:15:17:6f:94:44
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:458357 errors:0 dropped:0 overruns:0 frame:0
TX packets:250537 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:646967588 (616.9 MiB) TX bytes:33742301 (32.1 MiB)
Interrupt:16 Memory:fc3e0000-fc400000
eth1 Link encap:Ethernet HWaddr 00:1e:4f:13:31:b7
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5 errors:0 dropped:0 overruns:0 frame:0
TX packets:37 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:320 (320.0 B) TX bytes:2992 (2.9 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:18979 errors:0 dropped:0 overruns:0 frame:0
TX packets:18979 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:10059282 (9.5 MiB) TX bytes:10059282 (9.5 MiB)
tap103i0 Link encap:Ethernet HWaddr 3e:fb:b1:e0:40:da
inet6 addr: fe80::3cfb:b1ff:fee0:40da/64 Scope:Link
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:520 errors:0 dropped:0 overruns:0 frame:0
TX packets:766 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:500
RX bytes:44796 (43.7 KiB) TX bytes:702165 (685.7 KiB)
vmbr0 Link encap:Ethernet HWaddr 00:15:17:6f:94:44
inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::215:17ff:fe6f:9444/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:437651 errors:0 dropped:0 overruns:0 frame:0
TX packets:245697 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:637230824 (607.7 MiB) TX bytes:31203868 (29.7 MiB)
vmbr1 Link encap:Ethernet HWaddr 00:1e:4f:13:31:b7
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::21e:4fff:fe13:31b7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5 errors:0 dropped:0 overruns:0 frame:0
TX packets:34 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:230 (230.0 B) TX bytes:2412 (2.3 KiB)
输出netstat -rn
:
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 vmbr0
192.168.0.0 192.168.0.1 255.255.255.0 UG 0 0 0 vmbr0
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 vmbr0
192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 vmbr1
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 vmbr1
/etc/network/interfaces
:
auto lo
iface lo inet loopback
iface eth0 inet manual
iface eth1 inet manual
auto vmbr0
iface vmbr0 inet static
address 192.168.0.2
netmask 255.255.255.0
gateway 192.168.0.1
bridge_ports eth0
bridge_stp off
bridge_fd 0
post-up route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1
auto vmbr1
iface vmbr1 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.0.2
bridge_ports eth1
bridge_stp off
bridge_fd 0
post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
答案1
如果您需要根据正在使用的本地 IP 地址来使用不同的默认路由,这就是 iproute2 发挥作用的地方。
https://www.thomas-krenn.com/en/wiki/Two_Default_Gateways_on_One_System
答案2
您可以修改该/etc/network/interfaces
文件并将此行添加到每个网络接口节/块中以定义静态路由
# this is for vmbr0 block below
post-up route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1
# this is for vmbr1 block below
post-up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
并且您需要通过以下方式重新启动网络子系统/服务service networking restart
或者
您可以动态更改路由,但重新启动后您将丢失它们。它非常适合测试目的或短期使用。
route add -net 192.168.0.0/24 gw 192.168.0.1 dev vmbr0
route add -net 192.168.1.0/24 gw 192.168.1.1 dev vmbr1
这不需要您重新启动网络,尽管它可能会在必要时因网络切换而导致暂时中断或会话丢失。