三二互联专业提供速度最快最稳定的美国服务器、香港服务器。中美直连,亚洲优化![ 代理登陆 ] [ 付款方式 ] [ 找回密码 ][ 电子协议责任书 ]
硬件资源保障

采用高配品牌服务器

主流强悍CPU配置

确保服务高速稳定运行

中美直连线路

中美直连亚洲优化

采用中国CN2骨干网络

保证速度飞快稳定高效

如何配置策略路由实现出口IP优先级?

美国、香港服务器

如何配置策略路由实现出口IP优先级?

04-27   来源:

 Linux 策略路由配置「出口 IP 优先级」

(Ubuntu / CentOS 通用,单网卡多 IP,指定主 IP 优先、备用 IP 兜底,实现多出口权重 + 优先级)
核心原理
多网卡 / 多 IP,独立路由表
ip rule 配置优先级 (priority) 数值越小、优先级越高
主路由表优先匹配,主 IP 故障自动切备用;
结合 metric 路由权重 + 策略路由优先级,完美控制出口顺序。
一、环境示例
网卡:ens33
网关:192.168.1.1
主 IP(高优先级):192.168.1.100
备用 IP(低优先级):192.168.1.101
二、第一步:自定义路由表
bash
运行
nano /etc/iproute2/rt_tables
写入:
plaintext
10  table_main    # 高优先级 主IP
20  table_backup  # 低优先级 备用IP
三、第二步:为每个 IP 配置独立路由
1)主 IP 路由表
bash
运行
# 主IP路由,metric 越小优先级越高
ip route add default via 192.168.1.1 dev ens33 src 192.168.1.100 metric 10 table table_main
2)备用 IP 路由表
bash
运行
ip route add default via 192.168.1.1 dev ens33 src 192.168.1.101 metric 20 table table_backup
四、第三步:关键 —— 配置策略路由优先级
rule priority 数字越小,匹配越优先
bash
运行
# 高优先级:先走 主IP 表
ip rule add priority 100 lookup table_main
# 低优先级:主路由失效才走 备用IP
ip rule add priority 200 lookup table_backup
五、第四步:本地默认路由 + 路由权重
bash
运行
# 删除原有默认路由
ip route del default
# 双出口+metric优先级:metric 低 = 优先使用
ip route add default \
via 192.168.1.1 dev ens33 src 192.168.1.100 metric 10 \
via 192.168.1.1 dev ens33 src 192.168.1.101 metric 20
六、参数解释(必懂)
metric
路由权重,数值越小越优先
metric 10 > metric 20
ip rule priority
规则匹配顺序,数字越小越优先
优先匹配主 IP 路由表
src
强制指定出站源 IP,锁定出口
七、查看与验证
1. 查看策略规则
bash
运行
ip rule list
2. 查看多路由表
bash
运行
ip route show table table_main
ip route show table table_backup
3. 测试出口 IP
bash
运行
curl ifconfig.me
默认优先使用 主 IP,主 IP 链路异常自动落到备用 IP。
八、扩展:多 IP 层级优先级(3 个 IP 示例)
plaintext
table_A  priority 100  metric 10   一级优先
table_B  priority 150  metric 15   二级
table_C  priority 200  metric 20   三级兜底
九、清空 / 恢复默认
bash
运行
# 删除策略规则
ip rule del priority 100
ip rule del priority 200
# 清空自定义路由
ip route flush table table_main
ip route flush table table_backup
# 恢复单IP默认路由
ip route add default via 192.168.1.1 dev ens33

三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快

上一篇:如何处理服务器被攻击后的日志文件? 下一篇:如何配置策略路由实现出口IP负载均衡?

美国GIA服务器三二互联版权所有 WWW.250.cc 2008-2015 All Rights Reserved
三二互联 - 专业的美国C3服务器香港vps、抗DOOS流量清洗、云备份系统、网站加速系统、美国GIA服务器和香港云服务器产品提供商
三二互联24小时在线工单系统为您提供全面、专业、周到的技术支持与服务
咨询热线:400-679-9994(免长话费)