OpenVPN和路由和IPtables

目标:访问内部networking设备并通过隧道浏览网页。

192.168.2.x = internal network 192.168.3.x = openvpn server 192.168.2.111 = openvpn server on internal network [root@openvpn ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.3.2 * 255.255.255.255 UH 0 0 0 tun0 192.168.3.0 192.168.3.2 255.255.255.0 UG 0 0 0 tun0 192.168.2.0 * 255.255.255.0 U 0 0 0 eth0 link-local * 255.255.0.0 U 1002 0 0 eth0 default 192.168.2.254 0.0.0.0 UG 0 0 0 eth0 

 [root@openvpn ~]# cat /etc/openvpn/server.conf port 1194 #- port proto udp #- protocol dev tun tun-mtu 1500 tun-mtu-extra 32 mssfix 1450 reneg-sec 0 ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt cert /etc/openvpn/easy-rsa/2.0/keys/server.crt key /etc/openvpn/easy-rsa/2.0/keys/server.key dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem plugin /usr/share/openvpn/plugin/lib/openvpn-auth-pam.so /etc/pam.d/login #- Comment this line if you are using FreeRADIUS #plugin /etc/openvpn/radiusplugin.so /etc/openvpn/radiusplugin.cnf #- Uncomment this line if you are using FreeRADIUS client-cert-not-required username-as-common-name server 192.168.3.0 255.255.255.0 push "redirect-gateway def1" push "dhcp-option DNS 8.8.8.8" push "dhcp-option DNS 8.8.4.4" push "route 192.168.2.0 255.255.255.0" keepalive 5 30 comp-lzo persist-key persist-tun status 1194.log verb 3 

 client dev tun proto udp remote 18.4.79.28 1194 resolv-retry infinite nobind tun-mtu 1500 tun-mtu-extra 32 mssfix 1450 persist-key persist-tun ca ca.crt auth-user-pass comp-lzo reneg-sec 0 verb 3 

我可以连接就好,访问VPN盒。 但是,我无法浏览网页或访问其他本地networking设备。

我有IPTablesclosures。 我需要IP表规则还是我的路由closures?

我需要192.168.3.0访问192.168.2.0 🙂

编辑:忘了提及,我有这个集合;

 net.ipv4.conf.default.forwarding=1 

编辑:

我用这个:

 [root@openvpn ~]# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source <SERVERIP> [root@openvpn ~]# iptables -I INPUT 1 -p udp --dport 1194 -j ACCEPT 

我得到:

 pinging 192.168.2.5 reply from 192.168.3.1 destination host unreachable 

有两件事你需要检查和可能修复。

首先,您需要确保在内核中打开IP转发。 IP转发允许内核将数据包从一个接口传递到另一个接口。 你可以用下面的方法来检查

 $ sysctl net.ipv4.ip_forward net.ipv4.ip_forward = 0 

如果您看到0而不是1 ,则需要启用IP转发。 最简单和最可靠的方法是/etc/sysctl.conf下行添加到/etc/sysctl.conf (或者如果已经有net.ipv4.ip_forward条目,则修改它):

 # Controls IP packet forwarding net.ipv4.ip_forward = 1 

然后运行sysctl -p从该文件重新加载configuration。

接下来,您将需要configurationIPtables对来自VPN的数据包执行networking地址转换(NAT)。 否则,当这些数据包发送出eth0 ,任何接收数据包的设备将不知道如何回话(它们没有通过VPN服务器回到192.168.3.0/24的路由)。 有两种方法可以设置NAT:静态NAT(SNAT)和伪装。 build议在出站接口上的IP地址(您的情况下为eth0 )不要更改时使用SNAT。 伪装模式专为dynamicIP情况而devise,如拨号或其他dynamic分配的地址configuration(电缆调制解调器,DSL等)。 虽然两者configuration相似。

对于SNAT,你可以添加一条IPtables规则(注意,我使用的是192.168.2.13因为我不知道你已经分配给eth0的IP地址;你可以根据需要改变它):

 iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 192.168.2.13 

如果eth0上的IP地址不是静态可靠的,你可以使用Masquerade,它看起来像:

 iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 

你需要一个iptables规则来让VPN客户端访问networking。

首先确保你的系统允许NAT:

 # Setup sysctl to enable NAT. echo "# Allowing nat translation for VPN clients. net.ipv4.conf.default.forwarding=1 net.ipv4.ip_forward=1" > "/etc/sysctl.d/openvpn.conf" # load new sysctl config. command sysctl -p "/etc/sysctl.d/openvpn.conf" > '/dev/null' 

然后为VPNnetworking安装NAT iptables规则:

 CURRENT_IP_RANGE="192.168.2" command iptables -t nat -C POSTROUTING -s "${CURRENT_IP_RANGE}.0/24" \ -o 'eth0' -j MASQUERADE 2>'/dev/null' \ || command iptables -t nat -A POSTROUTING -s "${CURRENT_IP_RANGE}.0/24" \ -o 'eth0' -j MASQUERADE 

这些规则是openvpn-tools的一个提取, 在Debian上安装和设置OpenVPN ,OpenVPNpipe理脚本和我写的。

确保也有从您的VPN客户端访问的DNS服务器。 一个简单的答案是OpenDNS(8.8.8.8)。 更复杂但可能更好的解决scheme是在服务器上安装Bind(这是openvpn-tools使用的解决scheme)。

openvpn-tools可能是你感兴趣的,因为它为各种系统提供客户端configuration输出,并自动化新VPNnetworking的设置。

编辑为Openvpn 2.x服务器和1.5客户端

请参阅: OpenVPN发布说明 。

为了让OpenVPN 2.0与1.5 / 1.6版本交谈,把它放在1.xconfiguration文件中:

 tun-mtu 1500 tun-mtu-extra 32 mssfix 1450 key-method 2 

对于TLS的使用,key-method 2现在是默认值。

我不确定您是否正在使用AWS,但请确保您禁用可能用于运行此操作的任何AWS实例的目标/源检查

右键点击实例,将鼠标移到networking上,那里应该有一个选项。

如果这不能帮助你,希望它可以帮助别人。

在你的情况下,你应该尝试使用标准路由而不是使用NAT(在内部networking和openvpnnetworking之间)。 NAT应该是你的最后select。

“边缘路由器”(可能是您的CPE)(在内部networking和互联网的其余部分)需要将VPN节点的数据包(在192.168.3.0/24中)发送到Open服务器。 您必须在边缘路由器/ CPE中添加这样的路由:

 # This is the Linux command, find the equivalent for your router: ip route add 192.168.3.0/24 via 192.168.2.111 

现在这个路由器可能是一个组合FAI提供的xDSL模型+路由器,所以你可能无法做到这一点。

如果你不能这样做(你不能在路由器上添加路由),如果你的边缘路由器认为你的内部networking是192.168.0.0/16(或者至less包含192.168.3.0/34),你可以在eth0中设置一个ARP代理主networking中的VPN节点:

 echo 1 > /proc/sys/net/ipv4/conf/$xx/proxy_arp 

用x = eth0,x = tun0,x = all(我不知道你应该在哪个界面设置这个选项)。

这个想法是你的OpenVPN服务器将会在eth0 netrwork上为VPN中的节点回答ARP请求。 它只会工作是你的路由器认为192.168.3.0/24是在内部networking:否则,它不会发送内部networking上的ARP请求。