Skip to main content

Forwarding Port From Public to 127.0.0.1

Enable eth0 route localnet

replace eth0 with your own public interface

sudo sed -i '/net.ipv4.conf.eth0.route_localnet/d' /etc/sysctl.conf
sudo sed -i -e '$anet.ipv4.conf.eth0.route_localnet=1' /etc/sysctl.conf
sudo sysctl -p

Create Firewall Script

sudo tee /root/firewall.sh > /dev/null <<EOF
iptables -t nat -A PREROUTING -p tcp -j DNAT --to-destination 127.0.0.1
EOF
sudo chmod +x /root/firewall.sh
sudo /root/firewall.sh

Create Crontab

sudo crontab -l > crontemp
sudo echo "@reboot . /root/firewall.sh" >> crontemp
sudo crontab crontemp
sudo rm crontemp

Delete Rules

# check your rules
sudo iptables -t nat -v -L PREROUTING -n --line-number

image.png

# Replace Number 2 with list number rules
sudo iptables -t nat -D PREROUTING 2