Score:0

port forwarding from wireguard interface to another host with iptables

ru flag

I have two servers that I can access one of them via wireguard. I have a service that listen on 4559 port on the second server. how can I port forward to second server via iptables.

[laptop]                         [server1]                 [server2]
10.10.0.1 (wireguard) -------> 10.10.0.2:4559 --------> 192.168.1.20:4559
Michael Hampton avatar
cz flag
Why do you want to NAT? Just route normally.
heydar dasoomi avatar
ru flag
how shuold id do it?
Score:0
cn flag

To forward TCP port 4559 from your WireGuard interface on server1 to server2, add this to the [Interface] section of server1's WireGuard config:

PreUp = sysctl -w net.ipv4.ip_forward=1
PreUp = iptables -t nat -A PREROUTING -i %i -p tcp --dport 4559 -j DNAT --to-destination 192.168.1.20
PreUp = iptables -t nat -A POSTROUTING ! -o %i -j MASQUERADE
PostDown = iptables -t nat -D PREROUTING -i %i -p tcp --dport 4559 -j DNAT --to-destination 192.168.1.20
PostDown = iptables -t nat -D POSTROUTING ! -o %i -j MASQUERADE

Replace -p tcp with -p udp if it's UDP port 4559 you're trying to forward.

That will work just fine to access server2 from the laptop as 10.10.0.2:4559; but your particular scenario sounds a lot like the common "point-to-site" access pattern -- with the laptop being the remote "point", and server1 providing access to a larger "site". With that pattern, you normally would either configure the router at the site to route your WireGuard subnet (like 10.10.0.0/24) directly via server1 (no NAT), or you would set up SNAT on server1 to masquerade packets from WireGuard to the site.

With either of those point-to-site approaches, you would access port 4559 on server2 from the laptop as 192.168.1.20:4559 (just like if the laptop was physically on the site's LAN). Here's a good overview of your WireGuard point-to-site routing options, with links to step-by-step guides if you want to further explore those options.

heydar dasoomi avatar
ru flag
I still can't access service with telnet.
cn flag
Ah, you're probably also going to need to masquerade the packets forwarded from server1 to server2 -- I edited my answer to include an additional `MASQUERADE` iptables rule.
heydar dasoomi avatar
ru flag
still don't work
cn flag
1) run `sudo iptables -S` on server1 to list your active iptables rules to see if you have other rules that are blocking access; 2) run `sudo nft list ruleset` on server1 to see if you have nftables rules that are blocking access; 3) run `ping 10.10.0.2` on the laptop to verify that you can connect to server1 through WireGuard; 4) run `telnet 192.168.1.20 4559` on server1 to verify that you can connect to server2
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.