Suppose I have a network with a server routing all connections from inside the network to the Internet. How can I set up iptables so that instead of routing incoming connections to the Internet, it instead routes them to localhost port 8080. All help is appreciated.

  • The problem is that with simple redirection (destination IP address NAT) you will lose the original destination IP address. Do you want to setup a transparent HTTP proxy or should it process other protocols than HTTP too? – pabouk Oct 18 '13 at 13:14
  • It doesn't matter if its transparent or not – DankMemes Oct 18 '13 at 14:49
  • Redirecting all the traffic to a proxy is a base for transparent proxy :) – pabouk Oct 18 '13 at 15:54
  • Here's another answer that really helped me out: unix.stackexchange.com/questions/111433/… – Wren T. May 16 '14 at 4:38
sysctl net.ipv4.ip_forward=1 
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination
  • 1
    don't use bold .... – user218473 Oct 18 '13 at 13:11
  • Thanks! I haven't tried this yet but I suspect what I've been missing is the ip forwarding command. And next time, please use code blocks, not bold. – DankMemes Oct 18 '13 at 13:16
  • @AlexAntonov please use code blocks. Select your text in edit mode and click the brackets icon. – DankMemes Oct 18 '13 at 13:18
  • @ZoveGames, sorry, fixed it. – Alex Antonov Oct 18 '13 at 13:29
  • 3
    I experimented with this. The ip_forward sysctl is not necessary. However the route_localnet option here is. I see now this is exactly what Juan Cespedes' answer states. – Matt Joiner Feb 8 '15 at 2:23

that can be done with iptables, but only with kernel >= 3.6.

You will have to do:

sysctl -w net.ipv4.conf.all.route_localnet=1
iptables -t nat -I PREROUTING -p tcp --dport 80 -j DNAT --to

ip_forward is not necessary, because the packet is not forwarded, but if you don't include the sysctl for route_localnet (which works only in kernels >= 3.6), the packet will be dropped by the kernel because it considers it a "martian", coming from the outside and having a destination address of

  • Yup, that works – Nick De Greek Feb 15 '16 at 14:09
  • Did not know about route_localnet. So old school and couldn't figure out why ip_forward wasnt working (suspected it wasn't needed but tried anyway). – dmourati Nov 1 '16 at 3:38
  • 1
    Make sure you save net.ipv4.conf.all.route_localnet=1 in /etc/sysctl.conf otherwise it won't be persistent and after reboot the variable will go back to 0, causing the packet dropped. Then it would be very hard to figure why now everything is not working... it happened to me. ;) – viz Mar 12 '18 at 18:23
  • Sorry to necro an old question but is there an ipv6 equivalent of net.ipv4.conf.all.route_localnet for use with ip6tables? – Kebian Jul 10 '19 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.