Skip to content

Instantly share code, notes, and snippets.

@todgru
Created January 6, 2019 21:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todgru/abdd4ed44bf42a970eca0200419bd60b to your computer and use it in GitHub Desktop.
Save todgru/abdd4ed44bf42a970eca0200419bd60b to your computer and use it in GitHub Desktop.
set up ufw to forward requests to port 2222 of server to same server, port 22

Foward requests to one port to another on the same server

I wanted to forward requests comming to a server on port 2222 to the same server port 22.

I'm using ufw for the firewall on the server.

  1. allow port 22 and 2222 on the server.
$ sudo ufw allow 22
$ sudo ufw allow 2222
  1. Setup port forwarding requests by editing /etc/ufw/before.rules. Add the following to the top, just before *filter section:
*nat
:PREROUTING ACCEPT [0:0]
-A PREROUTING -p tcp --dport 2222 -j REDIRECT --to-port 22
COMMIT
  1. Finally, enable the ufw rules and restart ufw service:
$ sudo ufw enable
$ sudo service ufw restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment