Port forwarding routes external traffic through your firewall to internal services running on specific IP addresses and ports. This allows you to expose web servers, SSH connections or other services to the internet while keeping them behind a NAT or firewall.
You will configure iptables to forward incoming traffic to internal network resources. Once complete, external requests reach your services securely through defined firewall rules that persist across system reboots.
The iptables-persistent package saves your firewall rules automatically so they survive system reboots. Without it, you would need to reapply rules manually after each restart.
sudo apt update && sudo apt upgrade && sudo apt install iptables iptables-persistentYour system now has iptables installed with automatic rule persistence. Next, you will enable IP forwarding to allow traffic routing between network interfaces.
IP forwarding allows your system to route packets between different network interfaces. This is required for port forwarding to work because traffic must pass from your external interface to internal network destinations.
sudo nano /etc/sysctl.confnet.ipv4.ip_forward=1 and remove the # symbol if it appears at the start. If the line does not exist, add it to the end of the file.Ctrl+X, then Y to confirm, then Enter to save.sudo sysctl -pYour system can now route traffic between network interfaces. Before adding new rules, you will check existing iptables configuration to avoid conflicts.
Reviewing current firewall rules prevents conflicts with your new port forwarding configuration. This shows you what rules already exist and whether they might interfere with the traffic you want to forward.
sudo iptables -L -nYou now understand your current firewall configuration. Next, you will add the port forwarding rule that routes external traffic to your internal service.
The PREROUTING chain in iptables processes incoming packets before routing decisions. Adding a DNAT rule here redirects traffic from an external port to an internal IP address and port combination.
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.1.1:9086 This example forwards external traffic arriving on port 80 to 192.168.1.1 on port 9086.sudo netfilter-persistent saveYour port forwarding rule is now active and will survive system restarts. You will verify the configuration by checking the NAT table.
Checking the NAT table confirms your forwarding rule appears correctly in the PREROUTING chain. This shows the exact destination IP and port that incoming traffic will be redirected to.
sudo iptables -t nat -L -n -vThe NAT table shows your forwarding rule is in place. Finally, you will test external access to confirm traffic reaches your internal service.
Testing from outside your network confirms that port forwarding works correctly. A successful response proves that external traffic reaches your internal service through the firewall rule.
curl http://your-external-ip:80 Replace your-external-ip with your actual public IP address.Your iptables port forwarding configuration now routes external traffic to internal services securely. You enabled IP forwarding, added persistent NAT rules and verified external access. Traffic arriving on your specified port reaches the correct internal destination.
Monitor your forwarded services over the next few days. Check that connections remain stable and review your firewall logs for any unexpected traffic patterns. Our managed VPS hosting includes firewall configuration support and security monitoring.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting