大网数据广告活动
服务器运维问答

How to bypass/exclude a port on OpenVPN client WITHOUT bypassing all incoming traffic?

· 52CE 我爱测

I read this answer beforehand, it says: ip rule add from x.x.x.x table 100 ip route add table 100 to y.y.y.y/y dev ethX ip route add table 100 default via z.z.z.z The problem is, this will apply to all ports , and then I could us…

问题原文

原帖链接 · Austin Huang · CC BY-SA 4.0

I read this answer beforehand, it says:

ip rule add from x.x.x.x table 100
ip route add table 100 to y.y.y.y/y dev ethX
ip route add table 100 default via z.z.z.z

The problem is, this will apply to all ports, and then I could use iptables to DROP certain connections, but that's not what I want.

I want the OpenVPN to...

  • Exclude some ports, like SSH, TeamViewer, etc.
  • Tunnel all other ports, not DROP them.

Client is a Mac Mini. I can change server config if needed.

已采纳解答

原帖链接 · Lenniey · CC BY-SA 4.0

This can be achieved using iptables mangle. Your routing tables are fine, you just have to add some rules to ex/include the specific traffic.

In your case it would be something like this for SSH:

ip rule add fwmark 2 table 100
ip route flush cache
iptables -t mangle -A OUTPUT -p tcp --dport 22 -j MARK --set-mark 2
iptables -t nat -A POSTROUTING -o ethX -j SNAT --to-source z.z.z.z
                                  ^                        ^
                               your public interface      your public IP

This will:

  1. set fwmark 2 to your routing table 100
  2. flush the routing cache to avoid interference
  3. set mark 2 for all packets to destination port 22 (SSH)
  4. SNAT all packets going out of ethX to your public IP

I don't know if you need all of this or have to turn it around, depends on which way you want to look at it (all traffic via OpenVPN except xxx, or no traffic via OpenVPN except xxx).

E.g. you don't even have to use routing tables, but it certainly is much cleaner this way, or you don't need the SNAT / masquerade etc.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-01-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。