iptables forward external ip and port to internal ip and port

7,991

You could try to read this tutorial. It describes iptables well. For your question, you could use PREROUTING rules in nat table. It will looks like:

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 5050 -j DNAT --to-destination 192.168.199.105:5050
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 

You should change eth0 to your interface name.

Share:
7,991
dark_avenger
Author by

dark_avenger

Updated on September 18, 2022

Comments

  • dark_avenger
    dark_avenger over 1 year

    I need help urgent with direct examples, not with words. As of being working with FreeBSD since version 4.2 I'm ok with pf(packet filter), but now I got a project to make a module for web panel for OpenVZ in Linux.

    The admin part is 95% ready but I got stucked. I realized that every VM have to be forwarded to a different port. And the thing is that it have to be forwarded to a local IP. They have only 1 NIC and only 1 public IP, so they will use for every VM a local IP.

    Let's say:

    We create VM 101 with local IP 192.168.199.105 to listen for SSH on port 5050, and we have public IP 88.252.14.32. How to set IPTABLES to forward ssh requests from 88.252.14.32:5050 to 192.168.199.105:5050?

    The server is NATed and for Apache is easy with vhost, but for SSH - no idea how. I beg you for help with direct examples, not with words. Thank you.

    • Tero Kilkanen
      Tero Kilkanen almost 7 years
      To me it seems that you haven't even tried anything yourself. ServerFault is for people who have tried solutions, but haven't been able to find a working solution.
    • Jenny D
      Jenny D almost 7 years
      Welcome to Server Fault! Please read How do I ask a good question and try to rewrite your question so that it matches the site quidelines. Include the following: What have you tried? What did you expect to happen? What happened instead? What does your config look like? Do you have any log entries from the times it didn't work as expected?