Understanding Port Redirection
In many network configurations, servers operate on a private internal network that is not directly accessible from the Internet. In these cases, a public server acts as an intermediary between the Internet and the private network. Connected to both networks, the public server can forward incoming connections to the appropriate private server by using properly defined redirection rules. For example, when a device on the Internet connects to the public server on port 80—commonly used for web traffic—the server must know which internal server should handle the request. Port redirection (or port forwarding) allows you to create rules that, for instance:- Forward incoming connections on port 80 to Server 1.
- Redirect connections on port 993 to Server 2.
- Route connections on port 3306 to Server 3.
Network Address Translation (NAT) Explained
Data transmitted over networks is broken into small packets. Each packet carries header information such as the source and destination IP addresses, which are essential for guiding the packet through different network devices.



Configuring Port Redirection in Linux
Before you can set up port redirection, you must enable IP forwarding on your machine. This allows the system to forward packets between interfaces. By default, IP forwarding is disabled.Enabling IP Forwarding
On Ubuntu, it is recommended to enable IP forwarding in/etc/sysctl.d/99-sysctl.conf rather than in /etc/sysctl.conf because the latter might be overwritten during system updates. Open the file with your preferred editor and uncomment the following lines as needed:
Configuring Port Redirection with iptables
Linux processes network data using the netfilter framework. Although nftables is the modern tool, iptables remains widely used and will convert its rules to nftables rules automatically. Consider the following scenario: Assume the interfaceenp1s0 manages traffic from the internal network range 10.0.0.0/24, and enp6s0 is used for outbound traffic to the Internet. First, configure a rule that forwards incoming TCP connections on port 8080 to an internal address (for example, 192.168.0.5 on port 80):

Remember that while these iptables rules are useful for illustrating concepts, you should restrict the rules by specifying interfaces and source IP ranges in production environments to minimize potential abuse.

A Brief Look at nftables
Although iptables is more familiar to many, nftables provides a modern alternative. Below is an example configuration using nftables that mirrors the iptables example:Maintaining Persistence of the Rules
Keep in mind that iptables rules configured as above are temporary and will be lost after a system reboot. To save these rules permanently on Ubuntu, install the iptables-persistent package:Avoid unrestricted forwarding. Always restrict rules to specific interfaces and IP ranges to prevent unauthorized use of your network setup.
Optional Considerations and Additional Firewall Rules
Using options such as-i, -o, and -s in iptables commands allows you to restrict rules to specific network interfaces or IP ranges—a best practice in production environments. Unrestricted rules could enable malicious actors to misuse your server.
For example, if you are using UFW (Uncomplicated Firewall) on Ubuntu, the default policy is to deny forwarding. To allow traffic, you may need to adjust UFW settings. Here is an example configuration:
Quick Reference Commands
Below is a table summarizing some useful commands for configuring port redirection and NAT on Linux:| Action | Command |
|---|---|
| Enable IP Forwarding | Edit /etc/sysctl.d/99-sysctl.conf and reload with sudo sysctl --system |
| List iptables NAT Rules | sudo iptables -L -t nat |
| Flush iptables NAT Table | sudo iptables --flush --table nat |
| List nftables Rules | sudo nft list ruleset |
| Save iptables/nftables Rules | sudo netfilter-persistent save |
This lesson provided an overview of port redirection and NAT, including both underlying principles and practical configuration using iptables (with a glimpse at nftables). In the next lesson, we will explore advanced networking configurations to further enhance your network’s efficiency and security.