Guide: Forwarding Ports with iptables on Linux

Guide: Forwarding Ports with iptables on Linux

In Linux, iptables is a powerful firewall utility that allows you to manage network traffic by creating rules and policies. One of the common use cases of iptables is port forwarding, which enables you to redirect incoming network traffic from one port to another. This guide will walk you through the process of forwarding ports using iptables on Linux.


Before we begin, make sure you have the following prerequisites:

  • A Linux-based operating system with iptables installed
  • Root or sudo access to the server

Step 1: Check Current iptables Rules

Before configuring port forwarding, it’s a good practice to check the current iptables rules to avoid conflicts or unintended consequences. You can use the following command to display the existing rules:

sudo iptables -L

This will list all the current rules in your iptables configuration.

Step 2: Enable IP Forwarding

By default, IP forwarding is disabled on most Linux distributions. You need to enable it in order to forward ports. To enable IP forwarding, open the /etc/sysctl.conf file using a text editor and uncomment the following line:


Save the file and apply the changes by running the following command:

sudo sysctl -p

Step 3: Create Port Forwarding Rule

To forward a port, you need to create a rule in iptables. The basic syntax for port forwarding is as follows:

sudo iptables -t nat -A PREROUTING -i -p --dport -j DNAT --to-destination :

Let’s break down the command:

  • -t nat: Specifies the NAT table
  • -A PREROUTING: Appends the rule to the PREROUTING chain
  • -i : Specifies the input interface (e.g., eth0)
  • -p : Specifies the protocol (e.g., TCP or UDP)
  • --dport : Specifies the input port number
  • -j DNAT: Jumps to the DNAT target
  • --to-destination :: Specifies the destination IP address and port

For example, to forward incoming TCP traffic from port 80 to port 8080 on the same server, you can use the following command:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination

Step 4: Save iptables Rules

To persist the iptables rules across reboots, you need to save them. The exact command may vary depending on your Linux distribution. Here are a few common commands:

For Ubuntu and Debian-based systems:

sudo iptables-save > /etc/iptables/rules.v4

For CentOS and Red Hat-based systems:

sudo service iptables save

Additional Tips and Considerations

  • If you have multiple network interfaces, make sure to specify the correct input interface in the iptables rule.
  • Ensure that the destination IP address and port in the rule are valid and reachable.
  • If you want to forward traffic to a different server, replace the destination IP address with the target server’s IP.
  • To remove a port forwarding rule, use the iptables -t nat -D PREROUTING command, where is the rule number obtained from iptables -L -t nat --line-numbers.


Port forwarding with iptables is a useful technique for redirecting incoming network traffic to specific ports or servers. By following the steps outlined in this guide, you should now be able to configure port forwarding using iptables on your Linux-based system.

Useful Commands and Examples

Here’s a table summarizing some useful commands and examples related to iptables port forwarding:

Command Description
sudo iptables -L List current iptables rules
sudo sysctl -p Apply changes to sysctl.conf
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination Forward TCP traffic from port 80 to port 8080
sudo iptables-save > /etc/iptables/rules.v4 Save iptables rules (Ubuntu/Debian)
sudo service iptables save Save iptables rules (CentOS/Red Hat)

Similar Commands and Ideas

  • ufw: Uncomplicated Firewall (UFW) is a user-friendly interface to iptables, which simplifies the process of configuring firewall rules, including port forwarding.
  • firewalld: Firewalld is a dynamic firewall management tool that provides a more flexible and scalable alternative to iptables.
  • Consider using SSH tunneling as an alternative to port forwarding for secure remote access to services.

Scripts and Automation

To automate the process of configuring iptables port forwarding, you can create shell scripts or use configuration management tools like Ansible or Puppet. These tools allow you to define the desired state of your firewall rules and apply them consistently across multiple servers.

Here’s an example of a simple shell script that prompts for input and sets up a port forwarding rule:


read -p “Enter input interface: ” input_interface
read -p “Enter protocol (TCP/UDP): ” protocol
read -p “Enter input port: ” input_port
read -p “Enter destination IP: ” destination_ip
read -p “Enter destination port: ” destination_port

iptables -t nat -A PREROUTING -i $input_interface -p $protocol –dport $input_port -j DNAT –to-destination $destination_ip:$destination_port

echo “Port forwarding rule created successfully!”

Save the script to a file, make it executable, and run it as root or with sudo privileges.

Remember to exercise caution when automating firewall configurations and thoroughly test your scripts before deploying them to production environments.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked