Linux iptables: Port Redirection Example

ht‮/:spt‬/www.lautturi.com
Linux iptables: Port Redirection Example

The iptables utility is a firewall program that can be used to control network traffic on a Linux system. One of the things that you can do with iptables is to redirect traffic from one port to another.

For example, suppose you want to redirect incoming traffic on port 80 to port 8080. You can use the following iptables commands to accomplish this:

$ iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
$ iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

The first command uses the -A option to append a rule to the PREROUTING chain of the nat table. The -t nat option specifies that the rule should be applied to the nat table, and the -i eth0 option specifies that the rule should apply to traffic received on the eth0 interface. The -p tcp option specifies that the rule should apply to TCP traffic, and the --dport 80 option specifies that the rule should apply to traffic destined for port 80. The -j REDIRECT option specifies that the traffic should be redirected to a different port, and the --to-port 8080 option specifies the port to which the traffic should be redirected.

The second command uses the -A option to append a rule to the INPUT chain. The -p tcp option specifies that the rule should apply to TCP traffic, and the --dport 8080 option specifies that the rule should apply to traffic destined for port 8080. The -j ACCEPT option specifies that the traffic should be accepted by the firewall.

These rules will redirect incoming traffic on port 80 to port 8080, and allow traffic on port 8080 to pass through the firewall. Note that these rules will only apply until the next time the iptables rules are flushed or changed. If you want the rules to persist across reboots, you will need to save the rules to a file and restore the rules from the file after each reboot.

This is just one example of how iptables can be used to redirect traffic. You can use similar techniques to redirect traffic from other ports or to perform other types of traffic manipulation. To learn more about iptables and its capabilities, you can consult the iptables

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