linux - 建立 VPN 连接后 SSH 连接搞砸了

标签 linux ssh openvpn

<分区>

对于我的问题,我搜索了很多但没有找到可行的解决方案,所以我想在这里提出一个问题。

问题:

I have a remote server (lets call it A) and a local computer (lets call it B), both running Ubuntu 14.04. I could establish a reversed SSH tunnel connecting A and B by doing so At server A: ssh -R 2014:localhost:22 userb@B At the local computer B: ssh -p 2014 usera@localhost

where user-a and user-b are two users at A and B, respectively.

Now, I connect A to a VPN. After the VPN connection is successfully established, the currently openning ssh session does not respond anymore. Also, I cannot ssh into A anymore until after I killed the VPN connection.

有没有办法让 SSH 和 VPN 都开心?也许将 SSH session 与 VPN 分开? (我发现了一种叫做 split 隧道但并没有真正理解它的东西)。有人可以启发我吗?

最佳答案

这可能有点晚了,但是......

问题是 OpenVPN 更改了默认网关,这会中断您当前的 SSH 连接,除非您在启动 OpenVPN 之前设置适当的路由。

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

关于linux - 建立 VPN 连接后 SSH 连接搞砸了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24694979/

相关文章:

c++ - 在 Linux 中监视和终止其他程序停滞进程的更好方法?

perl - 如何使用 perl 触发在另一台机器上运行的另一个 perl 脚本?

http - 你能telnet到ssh吗?

ios - 如何在 swift 中以编程方式配置 openVPN?

amazon-ec2 - 预配置的 EC2 VPN AMI

android - 如果没有 Android API,OpenVPN 等如何创建 VPN 连接?

PHP cURL 与 Linux cURL

javascript 设置超时();不适用于 linux (firefox)

c++ - 除了使用 inotify 之外如何递归监视目录

bash脚本在ssh之后执行命令