Linux策略路由

版权声明:原创文章,请持怀疑态度阅读,欢迎转载,但请注明文章出处。 https://blog.csdn.net/qq_29344757/article/details/82960719

前面讲的路由规则都是基于目标IP地址为匹配依据设置的路由规则,策略路由则更加灵活,它可以根据多个参数来配置路由。假设如下的使用场景:
在这里插入图片描述
在192.168.10.0子网中,除了主机192.168.10.123要访问因特网时是通过电信运营商(192.168.20.0/24)之外,其他主机都使用移动运营商(192.168.30.0/24)。这就需要:
(1)在路由器Route3配置普通路由,使得数据包从192.168.80.123返回至192.168.10.0/24或者192.168.10.123/32时知道走那些路线:
假设Route3的eth1接的是192.168.20.0/24网段,eth2接的是192.168.30.0/24网段:

# route add -net 192.168.10.123 netmask 255.255.255.255 gw 192.168.20.10 dev eth1
# route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.30.10 dev eth2

这样就实现了从因特网回给192.168.10.123和给192.168.10.0子网其他主机走的分别是电信线路和移动线路。

(2)在路由器Route2中配置策略路由:
a. 创建路由表:
如下命令可用于查看系统中存在多少张路由表:

# cat /etc/iproute2/rt_tables

输出结果如下:

#
# reserved values
#
255		local		    #本地路由表
254		main		#主路由表,在前面我们增加的路由规则都设置于此
253		default		#存放默认路由规则。注意增加默认规则是若没有指定路由表那还是存在于main表中
0		unspec
#
# local
#
#1	inr.ruhep

在/etc/iproute2/rt_tables中增加电信、联通的路由表:

# vi /etc/iproute2/rt_tables

增加后:

#
# reserved values
#
255 local
254 main
253 default
0   unspec
251 telecom #电信
252 mobile  #移动
#
# local
#
#1  inr.ruhep

Linux的route -n命令默认查看的是main路由表,指定查看某张路由表的命令为(以查看main表为例):

# ip route show table main
# ip route show table 254

往指定路由表中增加路由规则,使用的命令是"ip route add"而不可以是前面的"route add":

# ip route add 192.168.80.0/24 via 192.168.20.20 table 251 
# ip route add 192.168.80.0/24 via 192.168.30.20 table 252
# ip route flush 251
# ip route flush 252

b. 如上将目的地址为192.168.80.0/24的路由规则都存放到251和252表中。接下来要创建策略,即什么时候使用251表中的路由规则,什么时候使用252表中的规则。
显示当前的策略:

# ip rule show
或者 # ip rule ls
0:	from all lookup local 
32766:	from all lookup main 
32767:	from all lookup default

可见先匹配编号小的策略。创建策略(等级pref越低越先匹配):

# ip rule add from 192.168.10.123/32 table 251 pref 10
# ip rule add from 192.168.10.10/24 table 252 pref 100

配置完成后,在windows平台中可以用pathping命令ping目的地址,获知中间经过的路由器。
另外,丢弃源地址为某特定地址的策略示例为:

# ip rule add from 192.168.10.124/32  prohibit pref 9

删除策略的命令为ip rule del,示例:

ip rule del from 192.168.10.10/24 table 252 pref 100

猜你喜欢

转载自blog.csdn.net/qq_29344757/article/details/82960719