1、首先先定義自定義的路由表,不然寫路由條目的時候會寫到主路由表,那就會導致選路錯亂了。
vi /etc/iproute2/rt_tables
100 tab1
2、添加靜態默認路由。
ip route add default via 172.16.60.1 dev ppp0 table tab1
這里是在tab1這張路由表里面添加一個默認路由,默認網關出口是ppp0的接口。
查看tab1的路由表命令
ip route list table tab1
3、給用戶接入進來的地址打上標記,然后強制打上標記的數據使用tab1這張路由表。
數字3是打標記的名稱。
iptables -A PREROUTING -t mangle -s 172.16.131.0/24 -j MARK --set-mark 3
查看上述命令的生效:
iptables -t mangle -L
ip rule add fwmark 3 table tab1
4、使用ip rule來根據源地址來使用路由表。
ip rule add from 172.16.131.0/24 table tab1
查看ip rule,可以查看上面兩條命令是否生效
ip rule
0: from all lookup local
32764: from 172.16.131.0/24 lookup tab1
32765: from all fwmark 0x3 lookup tab1
32766: from all lookup main
32767: from all lookup default
5、最后一步,設置iptables轉發。
iptables -A POSTROUTING -s 172.16.131.0/24 -o ppp0 -j MASQUERADE
查看上述命令生效
cat /etc/sysconfig/iptables
service iptables restart
6、設置開機啟動
chmod +x /etc/rc.d/rc.local
vi /etc/rc.d/rc.local
pon pptpvpn
sleep 10
ip route add default via 172.16.60.1 dev ppp0 table tab1
iptables -A PREROUTING -t mangle -s 172.16.131.0/24 -j MARK --set-mark 3
ip rule add fwmark 3 table tab1
ip rule add from 172.16.131.0/24 table tab1
route add -net 172.16.60.0 netmask 255.255.255.0 gw 172.16.60.1