Table of Contents
Port redirection allows traffic coming to one port to be forwarded to another. This is commonly used for running services on non-standard ports, Docker containers, Node.js apps, or game servers on a VPS.
Step 1: Check the Current Listening Port
Before redirecting, confirm which port your service is listening on.
ss -tulnp
Example: your app is listening on port 3000 and you want users to access it via port 80.
Step 2: Enable IP Forwarding (Required)
Port redirection requires IP forwarding to be enabled.
sudo sysctl -w net.ipv4.ip_forward=1
To make it permanent:
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
Step 3: Redirect Port Using iptables
Redirect incoming traffic from port 80 to port 3000.
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000
This works instantly without restarting any service.
Step 4: Allow Traffic Through the Firewall
If UFW is enabled, allow both ports.
sudo ufw allow 80/tcp
sudo ufw allow 3000/tcp
Reload firewall rules if needed:
sudo ufw reload
Step 5: Verify Port Redirection
Check active NAT rules:
sudo iptables -t nat -L -n -v
Test from the server:
curl http://localhost
If configured correctly, traffic to port 80 will be served by port 3000.
Step 6: Make iptables Rules Persistent
iptables rules reset after reboot unless saved.
sudo apt install iptables-persistent -y
sudo netfilter-persistent save
This ensures port redirection survives server restarts.
Optional Step: Redirect a Different Port
Example: redirect port 443 to 8443.
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443
Useful for SSL apps, control panels, or Docker-based services.
