One can use iptables to forward a specific port to another port using NAT PREROUTING chain. This can be used to make a server available on a different port for users.
- Add NAT forwarding using PREROUTING chain
$ sudo iptables -t nat -A PREROUTING -p tcp --dport 81 -j REDIRECT --to-port 80
- Add NAT forwarding using PREROUTING chain on a specific interface
$ sudo iptables -t nat -A PREROUTING -p tcp -d INTERFACE_IP --dport 443 -j REDIRECT --to-port 8443
- List iptable nat PREROUTE rules
## -n (numeric) $ sudo iptables -t nat -L PREROUTING -n --line-numbers Chain PREROUTING (policy ACCEPT) num target prot opt source destination 1 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:81 redir ports 80 2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:82 redir ports 80
- List all rules with line numbers
$ sudo iptables -n -L -v --line-numbers
- Delete a specific rule
$ sudo iptables -t nat -D PREROUTING [NUM]