1. 程式人生 > >VPN客戶端根據需要選擇網絡卡使用iptables進行nat

VPN客戶端根據需要選擇網絡卡使用iptables進行nat

在一個處於多網段環境的伺服器,我們需要VPN客戶端有選擇的使用不用的介面進行不同需求得通訊。

例如有多個網段 em1 124.17.a.b,em2 10.242.200.c,em2:0 10.242.205.d。VPN 網段為 10.242.1.0/24

vpn撥入後獲取的IP為10.242.1.e,

需要讓vpn 訪問外網iptables 的配置為

/sbin/iptables -t nat -A POSTROUTING -s 10.242.1.0/24 -j SNAT --to-source 124.17.a.b
/sbin/iptables -t nat -A POSTROUTING -s 10.242.1.0/24 -o em1 -j MASQUERADE

需要讓vpn 訪問網段10.242.200.0/24 的配置為

/sbin/iptables -t nat -A POSTROUTING -s 10.242.1.0/24 -j SNAT --to-source 10.242.200.c
/sbin/iptables -t nat -A POSTROUTING -s 10.242.1.0/24 -o em2  -j MASQUERADE

需要讓vpn 訪問網段10.242.205.0/24 的配置為

/sbin/iptables -t nat -A POSTROUTING -s 10.242.1.0/24 -j SNAT --to-source 10.242.205.d
/sbin/iptables -t nat -A POSTROUTING -s 10.242.1.0/24 -o em2:0  -j MASQUERADE