远程连接 OpenVPN

远程连接 OpenVPN

我家里有一台 Ubuntu 服务器,它连接到 OpenVPN 服务器。OpenVPN 远程地址为176.126.237.214,端口为25000。此隧道连接将我的公共 IP 更改为我的 OpenVPN 服务器提供的 IP。目前我无法远程连接到我的个人 Ubuntu 服务器。我已经尝试打开我的服务器使用的端口 ,192.168.1.11端口为8882。如何通过远程 SSH 连接到我的个人服务器?

答案1

所以您想使用服务器的原始(非 VPN)IP 地址进行连接?

问题是默认网关被 OpenVPN 更改,这会中断任何来自非 VPN 接口的连接。Linux 会将对来自真实接口的数据包的响应发送到 VPN 接口之外!您需要在启动 OpenVPN 之前设置适当的路由。

以下内容对我有用。它使用 iptables 和 ip(iproute2)。下面假设 OpenVPN 启动前的默认网关接口是“eth0”。这个想法是为了确保当连接到 eth0 时,即使 eth0 不再是默认网关接口,连接的响应数据包也会再次返回到 eth0。

连接标记、防火墙标记和路由表可以使用相同的数字。我使用不同的数字来使它们之间的区别更加明显。

# set "connection" mark of connection from eth0 when first packet of connection arrives
sudo iptables -t mangle -A PREROUTING -i eth0 -m conntrack --ctstate NEW -j CONNMARK --set-mark 1234

# set "firewall" mark for response packets in connection with our connection mark
sudo iptables -t mangle -A OUTPUT -m connmark --mark 1234 -j MARK --set-mark 4321

# our routing table with eth0 as gateway interface
sudo ip route add default dev eth0 table 3412

# route packets with our firewall mark using our routing table
sudo ip rule add fwmark 4321 table 3412

===

更新:

上面的方法在 Debian Jessie 上对我来说很好用。但在较旧的 Wheezy 系统上,我发现我需要在路由表条目中添加“via”:

# our routing table with eth0 as gateway interface
sudo ip route add default dev eth0 via 12.345.67.89 table 3412

这里的“12.345.67.89”肯定是原来的非VPN网关。

相关内容