使用ip rule查询路由策略, 使用ip route修改静态路由表

一、使用ip rule查看路由策略数据库

ip rule list
使用ip rule查询路由策略, 使用ip route修改静态路由表_第1张图片

在 Linux 系统启动时,内核会为路由策略数据库配置三条缺省的规则:

  • rule 0 匹配任何条件 查询路由表local(ID 255) 路由表local是一个特殊的路由表,包含对于本地和广播地址的高优先级控制路由。rule 0非常特殊,不能被删除或者覆盖。

  • rule 32766 匹配任何条件 查询路由表main(ID 254) 路由表main(ID 254)是一个通常的表,包含所有的无策略路由。系统管理员可以删除或者使用另外的规则覆盖这条规则。

  • rule 32767 匹配任何条件 查询路由表default(ID 253) 路由表default(ID 253)是一个空表,它是为一些后续处理保留的。对于前面的缺省策略没有匹配到的数据包,系统使用这个策略进行处理。这个规则也可以删除。

二、ip route命令查看或编辑静态路由表

ip route list table main

linux 系统中,可以自定义从 1-252个路由表,其中,linux系统维护了4个路由表:

  • 0#表: 系统保留表
  • 253#表: defult table 没特别指定的默认路由都放在改表
  • 254#表: main table 没指明路由表的所有路由放在该表
  • 255#表: local table 保存本地接口地址,广播地址、NAT地址 由系统维护,用户不得更改

( 路由表序号和表名的对应关系在 /etc/iproute2/rt_tables 文件中,可手动编辑。路由表序号和表名添加完毕后, ip rule add from all table 表名, 然后ip rule list查看 )

参考博客:

  • Linux Advanced Routing & Traffic Control HOWTO
    https://lartc.org/howto/

4.1. Simple source policy routing

https://lartc.org/howto/lartc.rpdb.html

Let's take a real example once again, I have 2 (actually 3, about time I returned them) cable modems, connected to a Linux NAT ('masquerading') router. People living here pay me to use the Internet. Suppose one of my house mates only visits hotmail and wants to pay less. This is fine with me, but they'll end up using the low-end cable modem.

The 'fast' cable modem is known as 212.64.94.251 and is a PPP link to 212.64.94.1. The 'slow' cable modem is known by various ip addresses, 212.64.78.148 in this example and is a link to 195.96.98.253.

The local table:

[ahu@home ahu]$ ip route list table local
broadcast 127.255.255.255 dev lo  proto kernel  scope link  src 127.0.0.1 
local 10.0.0.1 dev eth0  proto kernel  scope host  src 10.0.0.1 
broadcast 10.0.0.0 dev eth0  proto kernel  scope link  src 10.0.0.1 
local 212.64.94.251 dev ppp0  proto kernel  scope host  src 212.64.94.251 
broadcast 10.255.255.255 dev eth0  proto kernel  scope link  src 10.0.0.1 
broadcast 127.0.0.0 dev lo  proto kernel  scope link  src 127.0.0.1 
local 212.64.78.148 dev ppp2  proto kernel  scope host  src 212.64.78.148 
local 127.0.0.1 dev lo  proto kernel  scope host  src 127.0.0.1 
local 127.0.0.0/8 dev lo  proto kernel  scope host  src 127.0.0.1

Lots of obvious things, but things that need to be specified somewhere. Well, here they are. The default table is empty.

Let's view the 'main' table:

[ahu@home ahu]$ ip route list table main 
195.96.98.253 dev ppp2  proto kernel  scope link  src 212.64.78.148 
212.64.94.1 dev ppp0  proto kernel  scope link  src 212.64.94.251 
10.0.0.0/8 dev eth0  proto kernel  scope link  src 10.0.0.1 
127.0.0.0/8 dev lo  scope link 
default via 212.64.94.1 dev ppp0

We now generate a new rule which we call 'John', for our hypothetical house mate. Although we can work with pure numbers, it's far easier if we add our tables to /etc/iproute2/rt_tables.

$ su
# echo 200 John >> /etc/iproute2/rt_tables
# ip rule add from 10.0.0.10 table John
# ip rule ls
0:  from all lookup local 
32765:  from 10.0.0.10 lookup John
32766:  from all lookup main 
32767:  from all lookup default

Now all that is left is to generate John's table, and flush the route cache:

# ip route add default via 195.96.98.253 dev ppp2 table John
# ip route flush cache

And we are done. It is left as an exercise for the reader to implement this in ip-up.

你可能感兴趣的:(使用ip rule查询路由策略, 使用ip route修改静态路由表)