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

Port unreachable when wireguard is connected

· 52CE 我爱测

I have a server that routes all of its traffic through wireguard. When I start the wireguard, all ports listening on this server's public IP are unavailable - even ssh does not work. What I want is to allow incoming traffic on a …

问题原文

原帖链接 · Iuri G. · CC BY-SA 4.0

I have a server that routes all of its traffic through wireguard. When I start the wireguard, all ports listening on this server's public IP are unavailable - even ssh does not work.

What I want is to allow incoming traffic on a port (ssh for example) from any IP, but use wireguard for any outgoing connection.

Example:

MY_SERVER to any public ip -> use wireguard peer for routing (other servers should see wireguard peer's IP as source). This is working by having AllowedIPs = 0.0.0.0/0 in wireguard conf.

Any public IP to MY_SERVER -> this is not working when wireguard is enabled. If I stop wireguard it works.

Any ideas how can I allow incoming connections on my public IP and route outgoing connections through wireguard?

Update:

The main issue is that I am getting asymmetric routing due to wireguard routing all packets through it. So when incoming connection on public IP for given port comes in, it's reply is routed over wg0 instead of public interface and never makes it back to client.

I was able to somewhat fix it by adding ip rule add sport PORT table main but not sure if there is a better fix so I don't have to 1 by 1 add all ports - I want any port I start listening on to be routed correctly. Also, this fix works for TCP but not for UDP. Would be great if I can have it working for both protocols.

已采纳解答

原帖链接 · setenforce 1 · CC BY-SA 4.0

Your routing tables are stateless, they don't have any information about a packet being an answer or your port being bound. So you can use the connection table to add a mark when a connection is made to the server interface (not the wireguard one), and then restoring this mark for every packet of this connection.

You can then use this mark in a routing rule:

iptables -t mangle -A PREROUTING -d <server_address> -m conntrack --ctstate NEW -j CONNMARK --set-mark 51820
iptables -t mangle -A OUTPUT -j CONNMARK --restore-mark
ip rule add fwmark 51820 table main

Or if you use nftables:

nft add rule ip mangle prerouting ip daddr <server_address> meta mark set 51820
nft add rule ip mangle prerouting meta mark 51820 ct mark set mark
nft add rule filter output meta mark set ct mark
ip rule add fwmark 51820 table main

Or you can do it with listing ports in rules, it might be the cleaner. For UDP, you want services to listen on a single IP address (not 0.0.0.0), and then the ip rule should work.

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

资料来源与适用范围

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

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

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