Score:0

iptables port forwarding is not working

lv flag

I have a domestic server, and there are two public IPs on this server: One is domestic IP: 1.1.1.1 One is the IP of Hong Kong: 2.2.2.2

The default route of the server is to go out from the Hong Kong side of 2.2.2.2. I now have a server 3.3.3.3 in the United States as a scientific server. The port is 2080.

I want to access port 1080 of 1.1.1.1 and redirect the traffic to 3.3.3.3:2080 in the United States through DNAT.

How should it be written?

iptables -t nat -A PREROUTING -p tcp -d 1.1.1.1 --dport 1080 -j DNAT --to-destination 3.3.3.3:2080

iptables -t nat -A POSTROUTING -j MASQUERADE

This statement has no effect. Server forwarding is enabled.

Nikita Kipriyanov avatar
za flag
Please [append](https://serverfault.com/posts/1127086/edit) a *complete ruleset* as output with, for example, `iptables-save`. For masking public IP addresses you should not invent addresses you don't own like 1.1.1.1. Select specially dedicated addresses from blocks 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24 as descirbed in the [RFC 5737](https://www.rfc-editor.org/rfc/rfc5737.html) instead.
Score:0
kz flag
  1. Verify that the iptables rule is actually being applied by running the following command:
iptables -t nat -L -n
  1. Verify that the default route of the server is correct and that the traffic is going out from the Hong Kong side of 2.2.2.2:
ip route
  1. Verify that server 3.3.3.3 in the United States is able to receive traffic on port 2080:
telnet 3.3.3.3 2080

If the connection is successful, the server can receive traffic on port 2080.

  1. Verify that any firewalls on server 3.3.3.3 in the United States are not blocking port 2080 traffic.

If those steps didng't resolve the issue, you have to add some logging to your iptables rule to see if the traffic is being matched and forwarded as expected:

-j LOG --log-prefix "Port Forwarding: "

This will add a log message to the kernel log whenever the rule is matched:

dmesg
I sit in a Tesla and translated this thread with Ai:

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.