dual wan 設定問題 - Linux

Table of Contents

各位先進好~~

小的目前家裡有兩條對外網路,想設定為某一段走wan2,其他則走wan1

以下為小弟的環境

主機, Ubuntu 14.04 LTS X64

三張網路卡分別為:

wan1 1.2.3.4 >> ppp0 (pppoe 固定IP)

wan2 4.5.6.7 >> eth2 (DHCP 浮動IP)

lan 192.168.0.0/16 >> eth0

以下為設定ip route 的 script:

iptables -t nat -A POSTROUTING -o eth2 -j SNAT --to-source 4.5.6.7
iptables -t nat -A POSTROUTING -o ppp0 -j SANT --to-source 1.2.3.4
iptables -t mangle -A PREROUTING -s 192.168.2.0/24 -j MARK --set-mark 1

ip rule add fwmark 1 table 1

ip route add table 1 4.5.6.0/24 dev eth2 proto kernel scope link src 4.5.6.7

ip route add default dev eth2 scope link table 1

ip route flush cache

以上跑起來都沒問題~~但192.168.2.0/24這一段的還是走wan1出去~~

不曉得是哪個環節沒做到, ip_forward 也已經設定為 1

google找到的資料也大多是講到這邊就表示可以順利執行了

本來想改為load balance, 但因為小弟有在server跑apache跟mail

改成load balance好像就不會通了..

希望各位大大能協助解惑, 搞了周末兩天一點頭緒都沒有= =

--------2015/05/26更新-----------

最後挖出一本N年前買的Linux iptables的書, 看了一下裡面章節解說.

發現在設定預設路由時不可使用 ip route add dev eth2 ...

必須使用 ip route add via eth2 ...

並加上ip rule add from 192.168.2.0/24 table 1

所以最後修改script如下:

iptables -t nat -A POSTROUTING -o eth2 -j SNAT --to-source 4.5.6.7
iptables -t nat -A POSTROUTING -o ppp0 -j SANT --to-source 1.2.3.4
iptables -t mangle -A PREROUTING -s 192.168.2.0/24 -j MARK --set-mark 1

ip rule add fwmark 1 table 1

ip route add table 1 4.5.6.0/24 dev eth2 proto kernel scope link src 4.5.6.7

ip route add default via eth2 table 1

ip rule add from 192.168.2.0/24 table 1

ip route flush cache

設定完後再使用reaceroute -n 168.95.1.1 測得兩個網段出去的路徑會按照設定

192.168.2.0/24 走eth2出去, 其他則走ppp0出去~~

感謝tkcn大百忙之中抽空指導!!也謝謝Linux版

--
GordonWei http://www.ngu.tw/

--

All Comments

Oscar avatarOscar2015-05-29
少了 ip rule 那行?
Carolina Franco avatarCarolina Franco2015-05-31
阿阿..script本身有打~~立刻補上,感謝!!
Annie avatarAnnie2015-06-03
你是希望 192.168.2.0/24 全部走 wan2(eth2)?
Erin avatarErin2015-06-04
是的!!
Harry avatarHarry2015-06-07
那 ip route add default 那行怎麼沒指定 table?
Tom avatarTom2015-06-10
感謝tkcn大大指正,將ip rule加上後還是沒辦法