【Linux】策略路由的理解

一、linux内置的三张路由表


linux默认三种路由表,存放在/etc/iproute2/rt_tables
[root@f8s home]# ip rule show
0:      from all lookup local 
32766:  from all lookup main 
32767:  from all lookup default 

local: 本地接口地址,广播地址,已及NAT地址都放在这个表。该路由表由系统自动维护,管理员不能直接修改
main: 如果没有指明路由所属的表,所有的路由都默认都放在这个表里,我们平常执行的 route -n就是读取这张表的信息
default:默认的路由都放在这张表


二、规则和路由的关系


规则(ip rule)控制使用那种路由表,ip table往路由表中设置路由信息。下面我们用例子说明:


1. 首先创建一个虚拟网卡eth2.300

vconfig add eth2 300
ifconfig eth2.300 192.168.100.50 netmask 255.255.255.0 up

[root@f8s home]# ifconfig 
eth2      Link encap:Ethernet  HWaddr 00:0C:29:87:80:CD  
          inet addr:192.168.255.128  Bcast:192.168.255.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe87:80cd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:90005 errors:0 dropped:0 overruns:0 frame:0
          TX packets:87454 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:15158203 (14.4 MiB)  TX bytes:13314917 (12.6 MiB)
          Interrupt:18 Base address:0x2000 

eth2.300  Link encap:Ethernet  HWaddr 00:0C:29:87:80:CD  
          inet addr:192.168.100.50  Bcast:192.168.100.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe87:80cd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:27 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:3972 (3.8 KiB)

2. 然后创建一条规则, 从地址192.168.100.50发送的数据,都走路由表100

ip rule add from 192.168.100.50/32 table 100

[root@f8s home]# ip rule show
0:      from all lookup local 
32765:  from 192.168.100.50 lookup 100 
32766:  from all lookup main 
32767:  from all lookup default 

3. 最后往路由表100,添加路由信息

ip route add 192.168.100.0/24 dev eth2.300 src 192.168.100.50 table 100

[root@f8s home]# ip route show table 100
192.168.100.0/24 dev eth2.300  scope link  src 192.168.100.50 


你可能感兴趣的:(网络技术)