ip rule add to 168.95.1.1 table 10
ip rule add to 168.96.0.0/24 table 20
fwmark
将fwmark作为匹配条件时,必须搭配Netfilter一起使用, 这看起来很麻烦, 却是最灵活的匹配条件。如图10-8所示,某公司对外有三条ADSL,我们希望所有HT T P 协议经由第一条ADS L ,SMTP及POP3经由第二条ADSL,其余流量则经由第三条ADSL。可以使用如下的命令组合来达到这样的目的:
iptables -t mangle -A FORWARD -i eth3 -p tcp --dport 80 -j MARK --set-mark 1
iptables -t mangle -A FORWARD -i eth3 -p tcp --dport 25 -j MARK --set-mark 2
iptables -t mangle -A FORWARD -i eth3 -p tcp --dport 110 -j MARK --set-mark 2
iptables -t mangle -A FORWARD -i eth3 -j MARK --set-mark 3
ip rule add fwmark 1 table 1
ip rule add fwmark 2 table 2
ip rule add fwmark 3 table 3
[root@localhost ~]# ip rule show
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
[root@localhost ~]#
[root@localhost ~]# ip rule add from 192.168.1.0/24 table 1
[root@localhost ~]# ip rule add from 192.168.2.0/24 table 2
[root@localhost ~]#
[root@localhost ~]# ip rule show
0: from all lookup local
32764: from 192.168.2.0/24 lookup 2
32765: from 192.168.1.0/24 lookup 1
32766: from all lookup main
32767: from all lookup default
[root@localhost ~]# ip rule show
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
[root@localhost ~]#
[root@localhost ~]# ip rule add from 192.168.1.0/24 table 1 prio 10
[root@localhost ~]# ip rule add from 192.168.2.0/24 table 2 prio 20
[root@localhost ~]#
[root@localhost ~]# ip rule show
0: from all lookup local
10: from 192.168.1.0/24 lookup 1
20: from 192.168.2.0/24 lookup 2
32766: from all lookup main
32767: from all lookup default
ip rule del prio 10
ip rule del from 192.168.1.0/24
ip rule del table 1
ip rule del from 192.168.1.0/24 table 1 prio 10
[root@localhost ~]# ip rule show
0: from all lookup local
10: from 192.168.1.0/24 lookup 1
20: from 192.168.2.0/24 lookup 2
32766: from all lookup main
32767: from all lookup default
[root@localhost ~]#
在查看路由表之前,首先使用ip rule show命令来查看目前使用了哪些路由表,接着,再使用ip route show [table id | name]命令来查看路由表的内容。例如,可以使用ip route showtable main来查看路由表main的内容,如果省略路由表名称(如ip route show),会默认地查看路由表main的内容。
[root@localhost /]# ip rule show
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
[root@localhost /]#
[root@localhost /]# ip route show table main
10.10.15.0/25 dev eth0 proto kernel scope link src 10.10.15.46
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.10
default via 10.10.15.1 dev eth0
[root@localhost /]#
[root@localhost /]# ip route show table main
10.10.15.0/25 dev eth0 proto kernel scope link src 10.10.15.46
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.10
default via 10.10.15.1 dev eth0
[root@localhost /]#
[root@localhost /]# ip route show table main
10.10.15.0/25 dev eth0 proto kernel scope link src 10.10.15.46
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.10
default via 10.10.15.1 dev eth0
[root@localhost /]#
[root@localhost /]# ip route add 192.168.2.0/24 via 10.10.15.50 table main
[root@localhost /]#
[root@localhost /]# ip route show table main
10.10.15.0/25 dev eth0 proto kernel scope link src 10.10.15.46
192.168.2.0/24 via 10.10.15.50 dev eth0
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.10
default via 10.10.15.1 dev eth0
[root@localhost /]#
[root@localhost ~]# ip rule show
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
[root@localhost ~]#
[root@localhost ~]# ip rule add from 192.168.2.0/24 table 10
[root@localhost ~]#
[root@localhost ~]# ip route show table 10
[root@localhost ~]#
[root@localhost ~]# ip route add 192.168.1.0/24 dev eth1 table 10
[root@localhost ~]# ip route add default via 192.168.1.254 table 10
[root@localhost ~]#
[root@localhost ~]# ip route show table 10
192.168.1.0/24 dev eth1 scope link
default via 192.168.1.254 dev eth1
[root@localhost ~]#
3、删除路由
可以使用ip命令来方便地删除路由,我们使用以下示例来说明如何删除路由。首先将路由表10的内容显示出来.,可以看到路由表10中当前有两条路由,接着使用ip route del命令删除默认路由.,在此别忘了指定我们所要删除的是路由表10,否则默认会删除路由表main的默认路由,接着再使用ip route show 命令查看路由表10.,此时路由表10的默认路由已经不存在了,再次使用ip route del命令删除192.168.122.0/24的路由.,最后可以看到路由表10中已经没有任何路由了。
[root@localhost ~]# ip route show table 10
192.168.1.0/24 dev virbr0 scope link
default via 192.168.1.254 dev eth1
[root@localhost ~]#
[root@localhost ~]# ip route del default table 10
[root@localhost ~]#
[root@localhost ~]# ip route show table 10
192.168.1.0/24 dev virbr0 scope link
[root@localhost ~]#
[root@localhost ~]# ip route del 192.168.1.0/24 table 10
[root@localhost ~]#
[root@localhost ~]# ip route show table 10
[root@localhost ~]#
# service network restart
Shuttingdowninterface eth0: [ OK ]
Shuttingdowninterface eth1: [ OK ]
Shuttingdownloopbackinterface: [ OK ]
Bringinguploopbackinterface: [ OK ]
Bringingupinterface eth0: Determiningif ipaddress 10.3.3.25 is alreadyin use for deviceeth0...
[ OK ]
Bringingupinterface eth1: Determiningif ipaddress 10.2.2.10 is alreadyin use for deviceeth1...
[ OK ]
ip route flush table e0
ip route add default via 10.3.3.1 dev eth0 src 10.3.3.25 table e0
ip route add 127.0.0.0/8 dev lo table e0
ip rule add from 10.3.3.25 table e0
ip route flush table e1
ip route add default via 10.2.2.1 dev eth1 src 10.2.2.10 table e1
ip route add 127.0.0.0/8 dev lo table e1
ip rule add from 10.2.2.10 table e1
ip route flush table e0
ip route add default via 10.3.3.1 dev eth0 src 10.3.3.25 table e0
ip route add 127.0.0.0/8 dev lo table e0
ip rule add from 10.3.3.25 table e0
ip route flush table e1
ip route add default via 10.2.2.1 dev eth1 src 10.2.2.10 table e1
ip route add 127.0.0.0/8 dev lo table e1
ip rule add from 10.2.2.10 table e1
至此,所有配置完成
经测试,这种方式配置的双网卡规则,本机与局域网其他主机,通过内网 ip ping不通,那我们就换种方式配置下,请看下一篇博文