答案:通过ip route和ip rule可实现基于源地址的策略路由。首先用ip route配置静态路由,再通过ip rule指定特定流量查询自定义路由表,如将源IP为10.0.0.100的流量导向custom_table表,实现多网卡间的灵活路由控制,需配合rt_tables定义表、规则持久化配置以确保重启生效。

在Linux系统中,网络路由的配置不仅限于默认网关的设置,还涉及更复杂的策略路由场景。通过
ip route
ip rule
ip route
Linux中管理路由表主要使用
ip route
route
常用操作示例:
ip route add 192.168.10.0/24 via 10.0.0.1 dev eth0
ip route del 192.168.10.0/24
ip route show
ip route list
ip route add default via 192.168.1.1 dev eth0
这些路由默认写入主路由表(table main),系统启动后可通过配置文件持久化(如CentOS中修改
/etc/sysconfig/network-scripts/route-eth0
/etc/netplan/
/etc/network/interfaces
Linux支持多个路由表,编号范围0–255,其中0、254、255保留,254是main表,253是default表。策略路由通过
ip rule
查看当前规则:
ip rule show典型输出:
0: from all lookup local这些是系统默认规则,优先级从0到32767递增(数字越小优先级越高)。
假设服务器有两个网络接口:
目标:从10.0.0.100发出的数据包走eth1的网关。
步骤如下:
/etc/iproute2/rt_tables
ip route add 10.0.0.0/24 dev eth1 src 10.0.0.100 table custom_table
ip route add default via 10.0.0.1 dev eth1 table custom_table
ip rule add from 10.0.0.100 lookup custom_table
echo 1 > /proc/sys/net/ipv4/ip_forward
此时,从10.0.0.100发起的连接将使用custom_table中的路由,走eth1出口。
上述命令重启后失效,需写入配置文件。
CentOS/RHEL:
在
/etc/sysconfig/network-scripts/route-eth1
/etc/sysconfig/network-scripts/rule-eth1
Ubuntu/Debian(Netplan):
在YAML配置中使用
routing-policy
networkd-dispatcher
ip rule
基本上就这些。掌握
ip route
ip rule
以上就是如何在Linux中配置路由 Linux ip route策略路由的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号