CentOS7.x配置三线策略路由
文章最后附上部署脚本
线路 | IP | 网关 | 网卡 |
---|---|---|---|
电信 | 192.168.10.1/24 | 192.168.10.254 | eth0 |
联通 | 10.2.8.1/24 | 10.2.8.254 | eth1 |
电信 | 172.10.41.1/24 | 172.10.41.254 | eth2 |
电信作为默认网关
1.配置网卡
[root@localhost ~]$ cat /etc/sysconfig/network-scripts/ifcfg-eth0 #配置电信线路的网卡配置文件TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=static #设置为静态DEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=eth0UUID=0339bfc5-84f0-4803-130b-fc72c1b3957cDEVICE=eth0ONBOOT=yes #开启网卡IPADDR=192.168.41.1 #电信IPNETMASK=255.255.255.0 #掩码GATEWAY=192.168.41.254 #网关DNS1=114.114.114.114 ----------------------------------------------------------[root@localhost ~]$ cat /etc/sysconfig/network-scripts/ifcfg-eth1TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=static #设置为静态DEFROUTE=noIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=eth1UUID=6af4f6ca-3de4-30e7-b2e9-0fc3b67fece5DEVICE=eth1ONBOOT=yes #开启网卡IPADDR=10.2.8.1 #联通IPNETMASK=255.255.255.0 #掩码#GATEWAY=10.2.8.254 #联通网关需要注释掉----------------------------------------------------------[root@localhost ~]$ cat /etc/sysconfig/network-scripts/ifcfg-eth2TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=static #设置为静态DEFROUTE=noIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=eth2UUID=6af4f6ca-3de4-30e7-b2e9-0fc3b67fece5DEVICE=eth2ONBOOT=yes #开启网卡IPADDR=172.10.41.1 #移动IPNETMASK=255.255.255.0 #掩码#GATEWAY=172.10.41.254 #移动网关需要注释掉
2.配置路由表
在表中添加
252 cnc
251 cmcc
250 ct数字越小,优先级越高
[root@localhost ~]$ cat /etc/iproute2/rt_tables ## reserved values#255 local254 main253 default252 cnc #新增联通路由表251 cmcc #新增移动路由表250 ct #新增电信路由表0 unspec## local##1 inr.ruhep
3.交互式命令行配置策略路由
#配置电信策略ip route flush table ct ip route add default via 192.168.41.254 dev eth0 src 192.168.41.1 table ctip rule add from 192.168.41.1 table ct#配置联通策略ip route flush table cncip route add default via 10.2.8.254 dev ens256 src 10.2.8.1 table cncip rule add from 10.2.8.1 table cnc#配置移动策略ip route flush table cmccip route add default via 172.10.41.254 dev ens224 src 172.10.41.1 table cmccip rule add from 172.10.41.1 table cmcc
4.将策略路由加入到开启自启文件中rc.local
[root@localhost ~]$ cat /etc/rc.local#!/bin/bash# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES## It is highly advisable to create own systemd services or udev rules# to run scripts during boot instead of using this file.## In contrast to previous versions due to parallel execution during boot# this script will NOT be run after all other services.## Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure# that this script will be executed during boot.touch /var/lock/subsys/localip route flush table ct ip route add default via 192.168.41.254 dev eth0 src 192.168.41.1 table ctip rule add from 192.168.41.1 table ctip route flush table cncip route add default via 10.2.8.254 dev ens256 src 10.2.8.1 table cncip rule add from 10.2.8.1 table cncip route flush table cmccip route add default via 172.10.41.254 dev ens224 src 172.10.41.1 table cmccip rule add from 172.10.41.1 table cmcc
5.给rc.local文件加执行权限
[root@localhost ~]$ chmod +x /etc/rc.local
6.测试三线策略路由是否配置成功
ping
命令加参数-l
可以指定源IP
[root@localhost ~]$ ping -l 192.168.41.1 8.8.8.8 #测试电信网络是否可以通往公共DNSPING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.64 bytes from 8.8.8.8: icmp_seq=1 ttl=114 time=85.1 ms64 bytes from 8.8.8.8: icmp_seq=2 ttl=114 time=71.2 ms64 bytes from 8.8.8.8: icmp_seq=3 ttl=114 time=66.0 ms64 bytes from 8.8.8.8: icmp_seq=4 ttl=114 time=94.8 ms --- 8.8.8.8 ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3439ms rtt min/avg/max/mdev = 66.042/79.320/94.844/11.382 ms --------------------------------------------------------------------------------------[root@localhost ~]$ ping -l 10.2.8.1 8.8.8.8 #测试联通网络是否可以通往公共DNSPING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.64 bytes from 8.8.8.8: icmp_seq=1 ttl=114 time=42.4 ms64 bytes from 8.8.8.8: icmp_seq=2 ttl=114 time=42.4 ms64 bytes from 8.8.8.8: icmp_seq=3 ttl=114 time=42.4 ms64 bytes from 8.8.8.8: icmp_seq=4 ttl=114 time=42.4 ms --- 8.8.8.8 ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3310ms rtt min/avg/max/mdev = 42.414/42.440/42.476/0.207 ms --------------------------------------------------------------------------------------[root@localhost ~]$ ping -l 172.10.41.1 8.8.8.8 #测试移动网络是否可以通往公共DNSPING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.64 bytes from 8.8.8.8: icmp_seq=1 ttl=114 time=71.8 ms64 bytes from 8.8.8.8: icmp_seq=2 ttl=114 time=70.2 ms64 bytes from 8.8.8.8: icmp_seq=3 ttl=114 time=53.9 ms64 bytes from 8.8.8.8: icmp_seq=4 ttl=114 time=70.4 ms --- 8.8.8.8 ping statistics ---4 packets transmitted, 4 received, 0% packet loss, time 3333ms rtt min/avg/max/mdev = 53.950/66.615/71.845/7.340 ms
7.一键刷策略路由脚本(需要修改成自己的主机IP,网关和对应的网卡)
!!!注意,如果用电信做默认网关,联通和移动的网卡配置文件中不要配置网关GATEWAY
#!/bin/bashct_ip=192.168.41.1 #电信IPct_gw=192.168.41.254 #电信网关ct_dev=eth0 #电信对应的网卡cnc_ip=10.2.8.1 #联通IPcnc_gw=10.2.8.254 #联通网关cnc_dev=eth1 #联通对应的网卡cmcc_ip=172.2.8.1 #移动IPcmcc_gw=172.2.8.254 #移动网关cmcc_dev=eth2 #移动对应的网卡#新增电信、联通、移动路由表cat >> /etc/iproute2/rt_tables <<EOF 252 cnc #新增联通路由表 251 cmcc #新增移动路由表 250 ct #新增电信路由表 EOFcat >> /etc/rc.local <<EOF
// 表写错了,把移动写成电信了 https://blog.csdn.net/m0_49679565/article/details/124752740
推荐本站淘宝优惠价购买喜欢的宝贝:
本文链接:https://hqyman.cn/post/9103.html 非本站原创文章欢迎转载,原创文章需保留本站地址!
打赏
微信支付宝扫一扫,打赏作者吧~


休息一下~~