Skip to content

Instantly share code, notes, and snippets.

@leotada
Last active May 17, 2021 19:46
Show Gist options
  • Save leotada/44980502b6632ce7196ae28fce61c0f4 to your computer and use it in GitHub Desktop.
Save leotada/44980502b6632ce7196ae28fce61c0f4 to your computer and use it in GitHub Desktop.
IP forwarding and Port redirect/forwarding on Linux
# Redirect Ports using IPTables
sudo iptables -t nat -A PREROUTING -i ens18 -p tcp --dport 80 -j REDIRECT --to-port 8080
# IP forwarding: Receive packets on one PORT and forward to another IP and PORT.
# Can redirect packages through a VPN client connection. Ex: EC2 linux receive packages and route through VPN.
sudo sysctl net.ipv4.ip_forward=1
sudo iptables -A INPUT -p tcp --dport 1444 -j ACCEPT
sudo iptables -t nat -A PREROUTING -p tcp --dport 1444 -j DNAT --to-destination 192.168.8.6:1433
sudo iptables -t nat -A POSTROUTING -p tcp -d 192.168.8.6 --dport 1433 -j MASQUERADE
# How to check port redirection in iptable
sudo iptables -t nat -L -n -v
# How to save IPTables rules and IP Forward
# iptables-save
# How to save IP Forward enabled
edit the file /etc/sysctl.conf and uncomment net.ipv4.ip_forward=1
# sysctl -p
# Fonts:
https://harryvasanth.com/redirect-ports-using-iptables/
https://serverfault.com/questions/749682/ip-forwarding-on-linux-anything-important-to-make-sure-to-do-or-know/750078#750078
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment