Iptables – Port forwarding

ip-tablesSimple trick to use iptables for port forwarding. I use Debian as my OS, but should work on most Linux systems with iptables.

Lets assume we have a Minecraft server, and a SSH server behind a gateway on a public IP. The Minecraft and SSH is not accessable directly from the Internet. And here is how we solve that. We assume the Minecraft is running on default port 25565 and the SSH server is running on default port 22. We also assume the gateway have a SSH server on port 22, so we can’t use port 22 on our “Minecraft Server” behind the gateway. Looking at the port list, we find out port 14 is unassigned, and we will use that as SSH port on our Minecraft server.

In our "gateway" we set up the following rules (assuming Minecraft server is at 192.168.1.10):

iptables -t nat -A PREROUTING -p tcp –dport 25565 -j DNAT –to 192.168.1.10:25565
iptables -t nat -A PREROUTING -p tcp –dport 14 -j DNAT –to 192.168.1.10:22
iptables -t nat -A POSTROUTING -d 192.168.1.10 -j MASQUERADE

Now your gateway will forward Minecraft traffic to 192.168.1.10, along with SSH traffic on port 14. If you decide to change the SSH port on the Minecraft server. You will have to change the second rule, and change 22 to 14 at the very end.

If you want to remove the rule that goes to the SSH you just replace the -A with -D :
iptables -t nat -D PREROUTING -p tcp –dport 14 -j DNAT –to 192.168.1.10:22

Then we assume you change the SSH server on your minecraft server to listen to port 14 instead:
iptables -t nat -A PREROUTING -p tcp –dport 14 -j DNAT –to 192.168.1.10:14

To save your Iptables configuration, enter:
apt-get install iptables-persistent
iptables-save > /etc/iptables/rules.v4

Happy forwarding!

About Author

Related Posts

php8 gd

PHP8 gd Activate after installation

PHP8 GD activate after installation. GD doesn’t get activated by default. Not even a reboot after installation will activate it. So how do we do it? In…

Debian 12: linux-image-6.1.0-10amd64

Troubleshooting dependency issues in Debian 12: Resolving linux-image-6.1.0-10amd64 package dependency problems. If you installed the Debian 12 from the live image the issue is the raspi-firmware. Even…

4 Best Free Nas Software That Is Open Source

Free NAS software or operating systems that are free to use and will turn a computer into a NAS more advanced than the dedicated boxes sold. What…

Raspbian default password

Raspbian default password

Looking for the Raspbian default password? It is the most essential username and password that you will need for your raspberry. At least if you are running…

OpenMediaVault default password

OpenMediaVault default password

OpenMediaVault default password is printed in the documentation. I did not see it the first time I installed it either. So I had to do some detective…

Debian change dns

Debian change DNS settings to a new DNS

Debian change DNS settings for speed improvement or privacy. It is really easy to do. So let us see how it’s done and get to it. The…

Leave a Reply