因为某些需求一台服务器需要配置两个网段的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
# 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
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
# nmcli c reload
# nmcli networking off;nmcli networking on
# 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
$ 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
测试成功,配置完成。
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
的数据包。扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有