rocky9.2系统双网关配置方法
背景:
因为某些需求一台服务器需要配置两个网段的ip,分别是内网ip和外网ip。要求两个ip都能正常联通,按照正常配置同一个系统只能配置一个网关,只能通一个网段的ip地址。下面记录两个网段ip都能正常联通的方法。
环境:
系统:rockylinux 9.2
内网ip:192.168.30.122/24 gw 192.168.30.254
外网ip:122.103.15.135/26 gw 122.103.15.129
添加路由表
为了给自定义路由表分配一个唯一的标识符(ID),并为这些标识符赋予一个易于识别的名称。这使得在配置和管理多路由表时更加方便和直观。
etc/iproute2/rt_tables
文件的作用
/etc/iproute2/rt_tables
文件用于定义路由表的名称和对应的数字标识符。默认情况下,Linux系统有几个预定义的路由表,例如 main
表(ID为254
)和 default
表(ID为253
)。通过在这个文件中添加自定义路由表,可以创建更多的路由表用于复杂的路由策略。
这里在/etc/iproute2/rt_tables
文件末尾添加1001 bond0和1002 bond1
# cat /etc/iproute2/rt_tables
#
# reserved values
#
255 local
254 main
253 default
0 unspec
#
# local
#
#1 inr.ruhep
1001 bond0
1002 bond1
网卡配置文件添加路由和规则
代码语言:javascript代码运行次数:0运行复制# cat /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
NAME=bond0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.30.122
NETMASK=255.255.255.0
GATEWAY=192.168.30.254
BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer2+3"
ip route add default via 192.168.30.254 dev bond0 table bond0
ip route add 192.168.30.0/24 dev bond0 proto kernel scope link src 192.168.30.122 table bond0
ip rule add from 192.168.30.122/32 table bond0
代码语言:javascript代码运行次数:0运行复制DEVICE=bond1
NAME=bond1
ONBOOT=yes
BOOTPROTO=static
IPADDR=120.133.16.135
NETMASK=255.255.255.192
GATEWAY=120.133.16.129
BONDING_OPTS="mode=4 miimon=100 xmit_hash_policy=layer2+3"
ip route add default via 122.103.15.129 dev bond1 table bond1
ip route add 122.103.15.128/26 dev bond1 proto kernel scope link src 122.103.15.135 table bond1
ip rule add from 122.103.15.135/32 table bond1
重启服务生效
代码语言:javascript代码运行次数:0运行复制# nmcli c reload
# nmcli networking off;nmcli networking on
查看路由和规则
代码语言:javascript代码运行次数:0运行复制# ip route show
default via 122.103.15.129 dev bond1 proto static metric 300
default via 192.168.30.254 dev bond0 proto static metric 301
192.168.30.0/24 dev bond0 proto kernel scope link src 192.168.30.122 metric 301
122.103.15.128/26 dev bond1 proto kernel scope link src 122.103.15.135 metric 300
# ip rule list
0: from all lookup local
32764: from 192.168.30.122 lookup bond0
32765: from 122.103.15.135 lookup bond1
32766: from all lookup main
32767: from all lookup default
测试结果
从本地电脑ping服务器外网ip和内网ip
代码语言:javascript代码运行次数:0运行复制$ ping 192.168.30.122
PING 192.168.30.122 (192.168.30.122): 56 data bytes
64 bytes from 192.168.30.122: icmp_seq=0 ttl=125 time=12.598 ms
64 bytes from 192.168.30.122: icmp_seq=1 ttl=125 time=4.551 ms
64 bytes from 192.168.30.122: icmp_seq=2 ttl=125 time=12.666 ms
64 bytes from 192.168.30.122: icmp_seq=3 ttl=125 time=12.120 ms
64 bytes from 192.168.30.122: icmp_seq=4 ttl=125 time=4.770 ms
64 bytes from 192.168.30.122: icmp_seq=5 ttl=125 time=6.109 ms
$ ping 122.103.15.135
PING 122.103.15.135 (122.103.15.135): 56 data bytes
64 bytes from 122.103.15.135: icmp_seq=0 ttl=125 time=1.076 ms
64 bytes from 122.103.15.135: icmp_seq=1 ttl=125 time=0.839 ms
64 bytes from 122.103.15.135: icmp_seq=2 ttl=125 time=0.937 ms
64 bytes from 122.103.15.135: icmp_seq=3 ttl=125 time=0.775 ms
64 bytes from 122.103.15.135: icmp_seq=4 ttl=125 time=1.136 ms
64 bytes from 122.103.15.135: icmp_seq=5 ttl=125 time=0.743 ms
测试成功,配置完成。
路由和规则解释
代码语言:javascript代码运行次数:0运行复制ip route add default via 192.168.30.254 dev bond0 table bond0
在 bond0
路由表中添加默认路由,通过设备 bond0
,网关为 192.168.30.254
。
ip route add 192.168.30.0/24 dev bond0 proto kernel scope link src 192.168.30.122 table bond0
在 bond0
路由表中添加到 192.168.30.0/24
网段的路由,使用设备 bond0
,源地址为 192.168.30.122
。
ip rule add from 192.168.30.122/32 table bond0
添加规则,使源地址为 192.168.30.122
的数据包使用 bond0
路由表。
ip route add default via 122.103.15.129 dev bond1 table bond1
在 bond1
路由表中添加默认路由,通过设备 bond1
,网关为 122.103.15.129
。
ip route add 122.103.15.128/26 dev bond1 proto kernel scope link src 122.103.15.135 table bond1
在 bond1
路由表中添加到 122.103.15.128/26
网段的路由,使用设备 bond1
,源地址为 122.103.15.135
。
ip rule add from 122.103.15.135/32 table bond1
添加规则,使源地址为 122.103.15.135
的数据包使用 bond1
路由表。
总结:
- 两个路由表 (
bond0
和bond1
) 分别配置了默认路由和特定网段的静态路由。 bond0
表用于处理源地址192.168.30.122
的数据包。bond1
表用于处理源地址122.103.15.135
的数据包。