Was this helpful? Support me via buymeacoffee.com and help me create lots more great content!

Using IPTables to Forward Ports

In this blog article, I will walk you through the process of using IPTables to forward ports on an Ubuntu 22.04 instance. Port forwarding is a common network configuration task, often used to redirect incoming traffic from one port to another, or to another machine. I will provide a step-by-step guide to set up IPTables for port forwarding and explain the purpose of each command used.

Assumptions

  • You are running Ubuntu 22.04 instance.
  • You have administrative privileges to execute commands.

Step 1: Enable IP Forwarding

IP forwarding must be enabled to allow traffic to flow between network interfaces. You can do this by editing the /etc/sysctl.conf file:

sudo nano /etc/sysctl.conf

Add the following line to the file or uncomment it if it already exists:

net.ipv4.ip_forward = 1

Save the file, and then apply the changes using:

sudo sysctl -p

This command will reload the sysctl settings, and IP forwarding will be enabled.

Step 2: Install iptables-persistent

To make your IPTables rules persist across reboots, it's recommended to install the iptables-persistent package. You can install it using:

sudo apt update
sudo apt-get install iptables-persistent

During the installation, you will be prompted to save your current IPTables rules. Choose 'Yes' to save them.

Step 3: Set Up Port Forwarding Rules

Now, let's configure IPTables to forward incoming traffic on ports 80 and 443 to another machine (in this example, 187.205.169.158:80 and 187.205.169.158:443). We'll use the NAT (Network Address Translation) table to achieve this. Run the following commands:

sudo iptables -t nat -A PREROUTING -i ens5 -p tcp --dport 80 -j DNAT --to-destination 187.205.169.158:80
sudo iptables -t nat -A PREROUTING -i ens5 -p tcp --dport 443 -j DNAT --to-destination 187.205.169.158:443
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

The first two lines redirect incoming TCP traffic on ports 80 and 443 via the ens5 interface to the specified destination IP and ports.

The third line ensures that the source IP addresses are masqueraded to match the outgoing traffic.

Step 4: Save IPTables Rules

To save your IPTables rules, use the following command:

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

This command saves the current IPTables rules to a file that will be automatically loaded at boot.

Step 5: Verify Your Configuration

To check if your IPTables rules are set up correctly, run the following command:

sudo iptables -t nat -nvL

This command displays the NAT table's rules, allowing you to confirm that your port forwarding rules are in place and working as expected.

Conclusion In this article, we've covered the setup and configuration of IPTables for port forwarding on an Ubuntu 22.04 instance. By following these steps, you can efficiently redirect incoming traffic to another machine or specific ports, enhancing your network's functionality and security.

Originally published at https://chrisshennan.com/blog/using-iptables-to-forward-ports