I want connections coming in on ppp0 on port 8001 to be routed to 192.168.1.200 on eth0 on port 8080.
I've got these two rules
-A PREROUTING -p tcp -m tcp --dport 8001 -j DNAT --to-destination 192.168.1.200:8080
-A FORWARD -m state -p tcp -d 192.168.1.200 --dport 8080 --state NEW,ESTABLISHED,RELATED -j ACCEPT
and it doesn't work. What am I missing?
First of all - you should check if forwarding is allowed at all:
If both returns
1
it's ok. If not do the following:Second thing -
DNAT
could be applied onnat
table only. So, your rule should be extended by adding table specification as well (-t nat
):Both rules are applied only to TCP traffic (if you want to alter UDP as well, you need to provide similar rules but with
-p udp
option set).Last, but not least is routing configuration. Type:
and check if
192.168.1.0/24
is among returned routing entries.You forget postrouting source address SNAT 'ing:
And don't forget to set your linux firewall as default gateway on computer with 192.168.1.200 address.
I think what you want is:
I have created the following bash script for doing this on my linux router. It automatically infers the WAN IP and confirms your selections before proceeding.
The use of the script is simple just copy and paste it to a file and then.
To remove the same rule
I thought this might save someone time on their respective router.
The accepted solution works when the destination host and the gateway are on the same subnet (like is in your case, both are on
eth0
192.168.1.0/24).Below is a generic solution for when the gateway, source and destination are all on different subnets.
1) Enable IP forwarding:
2) Add 2 iptables rules to forward a specific TCP port:
To rewrite the destination IP of the packet (and back in the reply packet):
To rewrite the source IP of the packet to the IP of the gateway (and back in the reply packet):
3) If you don't have a default
ACCEPT
firewall rule, allow traffic to the destination:4) Test the new setup. If it works, make sure the changes persist across reboots:
I had the task to make MACHINE_A into thinking that the service is running physically on MACHINE_B, but transparently re-route all requests to MACHINE_C.
The trick was to use MASQUERADE.
Please note that you might want to tweak the commands:
To allow packet forwardning on a specific interface only. For example:
To allow not only MACHINE_A, but also all others to use port forwarding, remove:
Try
These files tell the kernel it's allowed to forward packets between the interfaces.
This command doesn't work for me:
I have 2 LAN interfaces and FORWARD work when I'll written:
PREROUTING and FORWARD are necessary too, of course :)