Linux: Iptables Forward Multiple Ports

Linux: Iptables Forward Multiple Ports

To forward multiple ports using the iptables firewall in Linux, you can use the "--dport" option to specify the destination port or range of ports. Here's an example of how to forward multiple ports using the iptables firewall:

  1. Allow incoming traffic on port 80 (HTTP) by running the following command:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
‮cruoS‬e:www.lautturi.com
  1. Allow incoming traffic on port 443 (HTTPS) by running the following command:
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
  1. Forward incoming traffic on port 80 to port 8000 by running the following command:
iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-port 8000
  1. Forward incoming traffic on port 443 to port 8443 by running the following command:
iptables -A PREROUTING -t nat -p tcp --dport 443 -j REDIRECT --to-port 8443

This will allow incoming traffic on ports 80 and 443 and forward it to ports 8000 and 8443, respectively.

Note: The above commands assume that you have the necessary permissions to modify the iptables configuration. Make sure to test the changes before using them in a production environment.

Additionally, make sure that you have a service or application listening on the destination ports (8000 and 8443 in this example). If you don't have a service or application listening on the destination ports, the traffic will be dropped by the firewall.

To save the iptables configuration, run the following command:

service iptables save

This will persist the changes across system reboots.

Created Time:2017-10-30 10:17:46  Author:lautturi