How to Forward Ports With Iptables in Linux?


Port forwarding is a technique that allows a network system to access resources from outside the network by forwarding specific ports. It's a process that involves an intermediary device (such as a router) to accept incoming connections on specified ports, then redirects the traffic to an internal device on the network. The end result is allowing remote devices to connect and communicate with services running on devices within the local network.

In practical terms, it means that if you want to run a web server from your home computer, anyone trying to connect from outside your home network would need to know your public IP address and then send requests through port 80 (HTTP) or port 443 (HTTPS). However, without port forwarding configured properly, incoming connection requests wouldn't reach your home computer because they would be blocked by your router's firewall protections.

Brief Overview of Iptables

Iptables is one of several firewall solutions available for Linux systems. It operates at the kernel level and provides robust packet filtering capabilities with advanced features like stateful packet inspection, NAT (network address translation), connection tracking, and more.

Iptables is primarily used for controlling incoming and outgoing network traffic, including blocking or allowing specific protocols, ports, or IP addresses. The iptables tool uses a set of predefined chains to evaluate incoming traffic against a set of rules, with each rule defining specific conditions that must be met to allow or deny the traffic.

The chains are organized in a hierarchical manner with different tables that perform various actions - like filtering packets or modifying them for forwarding - based on the criteria specified in the rules. In the next section, we'll explore how iptables can be used to configure port forwarding on Linux systems.

Understanding Iptables

What is iptables?

Iptables is a powerful firewall utility that comes pre-installed on most Linux distributions. It works by examining packets of data as they pass through a network interface and making decisions based on the rules defined by the user.

These rules are used to decide whether to allow or block traffic, or to forward incoming traffic to a specific port or address. In simple terms, iptables acts as a filter between your computer and the rest of the network.

It can be configured to allow only certain types of traffic into your system, while blocking all other unwanted traffic. This is especially important for servers that need to be publicly accessible but also need to be secured against malicious attacks.

How does it work?

Iptables works by creating several chains of rules that define how incoming and outgoing traffic should be handled. These chains are essentially sets of rules that determine how packets should be processed depending on their attributes such as source IP address, destination IP address, protocol type, and port number.

When an incoming packet arrives at the network interface, iptables checks its attributes against each rule in each chain in sequence until it finds a match. If no match is found, the packet is dropped or rejected depending on how iptables is configured.

Basic commands for iptables

To start using iptables, you need to know some basic commands for configuring its rules −

  • iptables -L − lists all currently defined chains.

  • iptables -F − flushes (deletes) all rules from all chains.

  • iptables -A chain rule − appends (adds) a new rule at the end of an existing chain.

  • iptables -D chain rule − deletes a rule from an existing chain.

  • iptables -P chain target − sets the default policy (accept, drop or reject) for a given chain.

These are just a few of the many commands available in iptables. By learning these basics, you'll be well on your way to mastering this powerful firewall utility.

Port Forwarding with Iptables

Choosing the ports to forward

Before setting up port forwarding in Linux using iptables, it is important to decide which ports you want to forward. Port forwarding allows incoming traffic on a specific port to be redirected to another internal network address.

This is useful when running a server or hosting a service on your local network and you want external users to access it. Commonly forwarded ports include 80 for web traffic, 22 for SSH, and 3389 for Remote Desktop Protocol (RDP).

Setting up the iptables rules for port forwarding

Once you have decided on the ports you want to forward, you can begin setting up iptables rules for port forwarding. In order to do this, we need to create a new chain in the NAT table.

Creating a new chain for port forwarding

To create a new chain for port forwarding, use the following command −

sudo iptables -t nat -N 

This will create a new chain in the NAT table with the specified name.

Configuring the NAT table

After creating a new chain, we need to configure the NAT table by adding our new chain as one of its chains −

sudo iptables -t nat -A PREROUTING -j 

This will add our newly created chain as one of the chains in PREROUTING rule of NAT table.

Adding rules to forward ports

Now that we have set up our new chain and configured NAT table accordingly, we can add rules that will actually do the port-forwarding. There are three ways of doing this −

a) Forwarding single ports:

To forward incoming traffic on one specific port to an internal IP address and port, use the following command −

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

b) Forwarding ranges of ports:

To forward a range of incoming traffic on a specific range of ports to an internal IP address and port, use the following command −

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

c) Forwarding multiple ports at once:

To forward incoming traffic on multiple specific ports to an internal IP address and port, use the following command for each port you want to forward −

sudo iptables -t nat -A -p --dport ,,...-j DNAT --to-destination 

Setting up port forwarding with iptables in Linux is essential when running servers or hosting services on your local network.


In this article, we have covered the basics of port forwarding and how to use iptables in Linux to forward ports. We provided an overview of iptables and its basic commands, followed by a detailed guide on how to set up port forwarding with iptables. We discussed common issues that may arise when setting up port forwarding with iptables and some troubleshooting tips to help you resolve them.

Port forwarding is an essential aspect of networking that enables incoming traffic from the internet to reach a specific device or service on your network. This process is critical for many critical applications such as remote access, gaming, file sharing, and web hosting. Understanding how to forward ports with iptables in Linux is vital if you want to manage your network effectively.

Iptables is a powerful firewall tool that allows you to configure advanced settings for your network security. By understanding how it works and its functionality, you can improve your network's security posture while ensuring that traffic flows smoothly between devices or services within your network.

Additionally, keep in mind that port forwarding configurations must be updated whenever there are changes within your network infrastructure or application requirements. By following the guidelines outlined in this article and putting them into practice gradually over time, users can develop the skills required to configure iptables and make their networks more secure and reliable.

Updated on: 05-Jun-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started