通过公共接口路由OpenVPN隧道,并通过NAT接口(到互联网)

我希望有人能帮帮忙…

我想configurationOpenVPN-AS(即OpenVPN接入服务器,而不是OpenVPN)在我的VPS上工作。 VPS是运行Ubunto 10.04 LTS的KVM,具有非常的configuration。 OpenVPN-AS同样以最小的“风味”安装。

服务器有两个接口(两个都是DHCP,eth1没有configuration默认网关,但有一个可用): – eth0(在美国不是地理定位的公共IP地址), – eth1(私有IP地址NAT通过在美国地理定位的路由器)

大多数stream量,包括OpenVPN隧道(UDP / 1194)通过eth0进入,但隧道客户端应该通过eth1走出去,以获得美国IP地址的好处。 我认为有两个独立的问题:1)configurationIP,所以有一个隧道客户端通过NAT路由器离开2)configurationOpenVPN AS,使客户端使用该网关的互联网接入

文件/ etc / network / interfaces是:

# The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet dhcp # The internal (private) network interface auto eth1 iface eth1 inet dhcp up ip route add default via 172.16.0.254 dev eth1 table 100 down ip route del default via 172.16.0.254 dev eth1 table 100 up ip rule add from 172.16.0.0/16 iif eth1 lookup 100 down ip rule del from 172.16.0.0/16 iif eth1 lookup 100 up iptables -t nat -A POSTROUTING -s 5.5.0.0/20 -j SNAT --to-source 172.16.191.125 down iptables -t nat -D POSTROUTING -s 5.5.0.0/20 -j SNAT --to-source 172.16.191.125 

networking如下:

 root@us-tunnel:~# ifconfig | grep -A 1 encap as0t0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 inet addr:5.5.0.1 PtP:5.5.0.1 Mask:255.255.248.0 -- as0t1 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 inet addr:5.5.8.1 PtP:5.5.8.1 Mask:255.255.248.0 -- eth0 Link encap:Ethernet HWaddr 00:16:3c:34:01:20 inet addr:209.141.60.114 Bcast:209.141.60.255 Mask:255.255.255.0 -- eth1 Link encap:Ethernet HWaddr 00:16:3c:55:84:81 inet addr:172.16.191.125 Bcast:172.16.255.255 Mask:255.255.0.0 -- lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 

路由表如下:

 root@us-tunnel:~# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 209.141.60.0 * 255.255.255.0 U 0 0 0 eth0 5.5.0.0 * 255.255.248.0 U 0 0 0 as0t0 5.5.8.0 * 255.255.248.0 U 0 0 0 as0t1 172.16.0.0 * 255.255.0.0 U 0 0 0 eth1 default 209.141.60.1 0.0.0.0 UG 100 0 0 eth0 

你需要执行几个步骤来完成这个工作。

首先,您必须在您的configuration文件中设置路由,以通过客户端适配器引导客户端stream量。

您可以通过将“路由”行添加到客户机configuration文件或通过添加来完成此操作

 "--pull" 

到客户端configuration,然后将您的路由添加到服务器configuration。

 "push route 0.0.0.0 5.5.0.1" "push route 0.0.0.0 5.5.8.1" 

其次,你需要configuration你的iptables来允许来自vpnnetworking的传入数据包,并在服务器端启用masqurade和nat转发。

要启用数据包转发和Nat

  1. 在内核中启用数据包转发

     echo 1 > /proc/sys/net/ipv4/ip_forward 
  2. 在iptables中启用NAT

     sudo iptables --table nat --append POSTROUTING --out-interface eth1 -j MASQUERADE 
  3. 为vpn接口启用转发

     sudo iptables --append FORWARD --in-interface as0t0 -j ACCEPT sudo iptables --append FORWARD --in-interface as0t1 -j ACCEPT 

这是路由端的基本configuration,如果您需要更详细的帮助,请随时发表评论。