1. MagicESP
  2. Ubuntu Server 22.04
  3. Port Forward Using iptables

Port Forward Using iptables

Synonyms:
  1. iptables

Enable port forward

sudo {{ texteditor.value }} /etc/sysctl.conf
net.ipv4.ip_forward = 1
sudo sysctl -p

Add port forward

sudo iptables -t nat -I PREROUTING -p tcp -i eth0 --dport {{ localport.value }} -j DNAT --to-destination {{ forwardip.value }}:{{ forwardport.value }}
sudo iptables -t nat -I POSTROUTING -p tcp -o eth0 --dport {{ forwardport.value }} -d {{ forwardip.value }} -j SNAT --to-source {{ localip.value }}

Save iptables (interactive)

sudo apt install -y iptables-persistent
sduo dpkg-reconfigure iptables-persistent
cat /etc/iptables/rules.v4
cat /etc/iptables/rules.v6

Show iptables

sudo iptables -t nat -L -v -n --line-numbers

Delete iptables

sudo iptables -t nat -D PREROUTING [N]
sudo iptables -t nat -D POSTROUTING [N]