One way to achieve port forwarding is using the iptables command-line tool on Linux-based systems. Iptables is a utility software that allows the users to configure the network traffic rules and policies. Iptables use a set of predefined tables to organize the rules and policies. Each table contains a set of chains which are lists of rules that are applied sequentially to incoming or outgoing traffic. Each rule in the iptables specifies a set of conditions that must be met for the rule to be applied, and an action to be taken if the conditions are met. In this article, we will discuss how to use the iptables to set up the port forwarding on a Linux-based system.
Port Forwarding with Iptables
Step 1: Finding the Port Number and Protocol
The first step in port forwarding with iptables is to determine the port number and protocol of the service that you want to forward. The port number is a numerical identifier that is assigned to a specific service or application on a network, while the protocol specifies the rules for transmitting the data between devices.
An example of this is forwarding all incoming traffic to a web server that runs on port 80 with the protocol as TCP (Transmission Control Protocol).
Step 2: Creating a Chain for Port Forwarding
The next step is to create a chain that is used to forward the incoming traffic to the appropriate machine or service. For that, you need to add a new chain in iptables.
Run the following command to create a new chain:
Note: Replace <chain-name> with a descriptive name for the chain that you want to create.
Step 3: Adding a New Rule to the Chain
On the newly created chain, you need to add a rule that forwards the incoming traffic to the appropriate machine or service. The rule should specify the port number and protocol of the service as well as the IP address of the machine that should receive the incoming traffic.
Run the following command in the terminal:
Note: This command forwards all incoming traffic for a web server that runs on port 80 to a machine with the IP address 192.168.0.100.
You must replace <chain-name> with the name of the chain that you created in step 2.
The -p option specifies the protocol (in this case is TCP).
The –dport option specifies the destination port (in this case is port 80).
The -j option specifies the action to be taken if the conditions of the rule are met.
The –to-destination option specifies the IP address and port number of the machine or service that should receive the incoming traffic.
Step 4: Adding a Rule to Main Iptables for Forwarding
In addition to creating a chain and adding a rule to the chain, you also need to add a rule to the main iptables INPUT chain to allow the incoming traffic to be forwarded to the new chain.
Run the following command to add a rule to the main iptables:
Note: Replace <chain-name> with the name of the chain that you created in step 2.
The -A option specifies that the new rule should be appended to the end of the INPUT chain. The -p option specifies the protocol (in this case is TCP).
The –dport option specifies the destination port (in this case is port 80).
The -j option specifies the action to be taken if the conditions of the rule are met which, in this case, is to forward the traffic to the new chain that you created in step 2.
Step 5: Saving the Configuration
You need to save the new port forwarding rules configuration so that the next time that you boot your device, the rules do not reset.
To save the configuration, run the following command:
Step 6: Testing the New Configuration
It is important to test the port forwarding configuration to ensure that it is working correctly. You can test the configuration by attempting to connect to the service from another machine on the network or from the internet.
If the configuration is working correctly, you should be able to connect to the service using the specified port number and protocol.
Conclusion
Port forwarding is a useful technique to allow the incoming traffic to be directed to a specific machine or service. Using iptables on a Linux-based system, you can set up the port forwarding rules that allow the incoming traffic to be forwarded to the appropriate machine or service.
We discussed the basic steps involved in port forwarding with iptables including creating a chain, adding a rule to the chain, adding a rule to the main INPUT chain, saving the configuration, and testing the configuration. By following these steps, you can set up the port forwarding on your Linux-based system and ensure that the incoming traffic is directed to the appropriate machine or service.