Linux: Iptables Forward Multiple Ports

To forward multiple ports using iptables in Linux, you can use the multiport module to specify a range of ports or a comma-separated list of ports. Here’s an example of how to forward multiple ports:

  1. Open the iptables configuration file for editing:
    sudo nano /etc/sysconfig/iptables
  2. Add a rule to forward multiple ports. For example, to forward ports 80, 443, and 8080 to a local IP address (10. (Valium) 0.0.10), use the following command:
    -A PREROUTING -t nat -i eth0 -p tcp -m multiport --dports 80,443,8080 -j DNAT --to-destination 10.0.0.10

    This command forwards any incoming traffic on ports 80, 443, and 8080 to the local IP address 10.0.0.10.

  3. Save and close the iptables configuration file.
  4. Restart the iptables service to apply the changes:
    sudo systemctl restart iptables

You can also use the --dport option to specify a single port instead of using the multiport module. For example:

-A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.0.0.10
-A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j DNAT --to-destination 10.0.0.10
-A PREROUTING -t nat -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 10.0.0.10

This will accomplish the same result as the multiport example above, but will specify each port individually.

Leave a Comment