OpenVPN 和路由和 IPtables

4 networking vpn openvpn

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

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
Run Code Online (Sandbox Code Playgroud)

[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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我可以正常连接并访问 VPN 盒。但是,我无法浏览网页或访问其他本地网络设备。

我关闭了 IPTables。我需要 IP 表规则还是我的路由关闭?

我需要 192.168.3.0 才能访问 192.168.2.0 :)

编辑:忘了说,我有这一套;

net.ipv4.conf.default.forwarding=1
Run Code Online (Sandbox Code Playgroud)

编辑:

我用过这个:

[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
Run Code Online (Sandbox Code Playgroud)

我得到:

pinging 192.168.2.5
reply from 192.168.3.1 destination host unreachable
Run Code Online (Sandbox Code Playgroud)

Chr*_*ell 5

您需要检查并可能修复两件事。

首先,您需要确保在内核中开启了 IP 转发。IP 转发允许内核将数据包从一个接口传递到另一个接口。您可以通过以下方式进行检查:

$ sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0
Run Code Online (Sandbox Code Playgroud)

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

# Controls IP packet forwarding
net.ipv4.ip_forward = 1
Run Code Online (Sandbox Code Playgroud)

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

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

对于 SNAT,您将按照以下内容添加 IPtables 规则(注意,我192.168.2.13之所以使用,是因为我不知道您分配给 eth0 的 IP;您希望根据需要进行更改):

iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 192.168.2.13
Run Code Online (Sandbox Code Playgroud)

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

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)