一、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