OpenVPN 服务器子网地址冲突

OpenVPN 服务器子网地址冲突

我工作的地方和我家里有相同的子网池 (192.168.1.x)。因此,当连接到 OpenVPN 服务器时,我无法访问家里的子网地址。

解决此冲突的最佳/最简单的方法是什么?我想避免更改家庭路由器 DHCP 池地址。

答案1

如果有人在 Linux 计算机上遇到同样的问题:您可以仅对使用网络命名空间的选定程序使用 openvpn 连接!

这个小脚本连接到 openvpn 并打开使用 VPN 的 bash。计算机上的其他所有内容都看不到 VPN(因此它可以连接到您的本地网络)。在此 shell 中启动的每个程序都将使用 VPN 连接(并且无法访问本地网络)。

我假设openvpn的启动命令在文件./start_openvpn中,停止命令在./stop_openvpn中:

#!/bin/bash

IP=`hostname -I | cut -d' ' -f1`

ip netns add myvpn
ip link add veth0 type veth peer name veth1
ip link set veth1 netns myvpn
ip netns exec myvpn ifconfig veth1 192.168.99.1/24 up
ifconfig veth0 192.168.99.2/24 up
ip netns exec myvpn route add default gw 192.168.99.2
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.99.0/24 -j SNAT --to-source $IP
ip netns exec myvpn ./start_openvpn &
ip netns exec myvpn sudo -u myunixuser /bin/bash

./stop_openvpn
iptables -t nat -D POSTROUTING -o eth0 -s 192.168.99.0/24 -j SNAT --to-source $IP
ifconfig veth0 down
ip netns exec myvpn ifconfig veth1 down
ip link delete veth0
ip netns delete myvpn 

我的unix用户是您通常使用的本地 Linux 用户。必须以 root 用户身份调用此脚本。

工作完成后,关闭外壳出口

如果你需要第二个 VPN-shell,只需调用

ip netns exec myvpen /bin/bash

您已退出第一个 shell。

答案2

是的,这里最好的教训是永远不要在公司环境中使用 192.168.0.0/24、192.168.0.1/24,就像永远不要使用 802.1q VLAN ID 1 一样。

但是,有些情况下你什么也做不了。对于这些情况,OpenVPN 从 2.4 开始支持内部无状态地址转换 (NAT),又称为网络映射。来自man openvpn

--client-nat snat|dnat network netmask alias
        This pushable client option sets up a stateless one-to-one NAT rule on
        packet addresses (not ports), and is useful in cases where routes or
        ifconfig settings pushed to the client would create an IP numbering
        conflict.

        network/netmask (for example 192.168.0.0/255.255.0.0) defines the
        local view of a resource from the client perspective, while
        alias/netmask (for example 10.64.0.0/255.255.0.0) defines the remote
        view from the server perspective.

        Use snat (source NAT) for resources owned by the client and dnat
        (destination NAT) for remote resources.

        Set --verb 6 for debugging info showing the transformation of
        src/dest addresses in packets.

因此,您需要在客户端 .conf 文件中添加一行:

client-nat dnat 192.168.2.0 255.255.255.0 192.168.1.0
route 192.168.2.0 255.255.255.0

然后,如果您的服务器端有 192.168.1.5 系统,客户端将能够以 192.168.2.5 访问它,从而解决寻址冲突。 依此类推。

答案3

在我看来,这对于 openvpn 子网来说是一个糟糕的选择,因为它被很多家用路由器使用。除非您可以更改 openvpn 配置,否则您将不得不更改您的家庭网络。由于它是 DHCP,它不会造成太多麻烦——只需将其更改为 192.168.42.0/24 之类的地址,然后让您的设备重新连接即可。

编辑:

“push”指令会将 OpenVPN 服务器(您的工作网络)后面的网络添加到客户端(您的家用计算机)上的路由表。push 指令必须与工作子网匹配,并且应该不是与家庭子网相同。

相关内容