Redirect port 80 to 8080 and make it work on local machine

256,332

Solution 1

You need to use the OUTPUT chain as the packets meant for the loopback interface do not pass via the PREROUTING chain. The following should work; run as root:

iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080

Solution 2

Simple just use iptables allowing both port 80 and 8080 then redirect 80 to 8080 make sure you are assigning to the correct nic.. in example I use eth0

iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

Solution 3

This worked for me.

$ sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

Solution 4

Instead of the iptables, You could try: sudo ssh -gL 80:127.0.0.1:8080 localhost

Share:
256,332

Related videos on Youtube

Max
Author by

Max

Updated on September 18, 2022

Comments

  • Max
    Max over 1 year

    I redirected traffic for port 80 to 8080 on my machine with

    sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-ports 8080
    

    It works fine for all the world except my own machine. I am a developer and I need to redirect port 80 to 8080 for myself.

    My IP is 192.168.0.111

    My web server runs on port 8080

    I wish to open website from http://192.168.0.111/ instead of http://192.168.0.111:8080/ from same machine where server runs.

    • ma11hew28
      ma11hew28 over 9 years
      Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080?
    • Christian
      Christian about 8 years
      @mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user.
    • David Foerster
      David Foerster almost 8 years
      Why don't you just bind the web server to port 80?
    • Pavel K.
      Pavel K. over 7 years
      i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root..
  • Max
    Max about 10 years
    That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING
  • cgseller
    cgseller over 7 years
    Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.
  • alper
    alper over 6 years
    Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl
  • diyoda_
    diyoda_ about 6 years
    This did not work, not sure why this is voted up
  • heemayl
    heemayl about 6 years
    @Diyoda_ Please define did not work.
  • Jerry U
    Jerry U about 6 years
    @Alper I think you didn't read the OPs question.
  • Ahmed Hamdy
    Ahmed Hamdy almost 6 years
    I can confirm that this doesn't work on ubuntu 18.04
  • Christopher Bradshaw
    Christopher Bradshaw over 5 years
    Didn't work for me on Ubuntu 18.10
  • Vladimir Ishenko
    Vladimir Ishenko over 4 years
    Thanks! It's working on Ubuntu 19
  • Vladimir Ishenko
    Vladimir Ishenko over 4 years
    After would be nice to save iptable changes sudo apt-get install iptables-persistent
  • hanshenrik
    hanshenrik over 4 years
    +1 works for me on Ubuntu 18.04 (at least for TCP), did port 7000->7001 and servers listening on port 7001 received incoming connections from port 7000 :)
  • hanshenrik
    hanshenrik over 4 years
    make sure to use the correct network interface tho, it isn't called eth0 on all systems
  • mitchus
    mitchus over 4 years
    I get a "Connection refused" when attempting this solution.
  • M. Volf
    M. Volf about 4 years
    how to stop this forwarding? EDIT - FOUND IT: replace -A with -D in that command
  • user3757405
    user3757405 about 4 years
    @mitchus it sounds like you don't have an SSH service. Try sudo apt install openssh-server.
  • Nisharg Shah
    Nisharg Shah over 3 years
    works perfectly in 20.04
  • Sunding Wei
    Sunding Wei over 3 years
    not working on ubuntu 20.04 LTS
  • Sunding Wei
    Sunding Wei over 3 years
    Works for me on ubuntu 20.04 LTS
  • Dimitar II
    Dimitar II about 2 years
    Probably we should first enable the port forwarding: sysctl net.ipv4.ip_forward=1 (or sysctl net.ipv6.conf.all.forwarding=1). Then make the change persistent, by uncommenting these lines in '/etc/sysctl.conf'.