I used to run a service on port 25570 I'm now changing it to run on port 25565 but I still want people who try to access it on the old port to have access to the service. So I'd like to forward port 25570 to 25565 on the same machine.

I know there's a lot of questions that talk about forwarding to a different machine but I was wondering if the method is different when using the same machine?

2 Answers 2

iptables -t nat -A PREROUTING -p tcp --dport 25570 -j REDIRECT --to-port 25565

This assumes you're not routing traffic for an entire network through this box and that if you were there's no expectation that traffic destined for other hosts will be on that port


It is worth noting the the accepted answer only applies for other network hosts connecting to the machine running iptables. It does not redirect the port for clients running on the iptables machine trying to connect to port 25570 (for example).

These entries will forward the port for connections coming from the network or from the local host running the services.

sudo iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
sudo iptables -t nat -I OUTPUT -p tcp -o lo --dport 443 -j REDIRECT --to-ports 8443
  • 2
    Thanks for this! Without this addition I wouldn't have been able to get my Vagrant box to request the site it was serving using the same port internally (to that of the host machine) i.e. HOST = localhost:9000 and now GUEST = localhost:9000 (which routes to :80)
    – Pebbl
    Sep 4, 2015 at 10:29

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.