martes, 29 de abril de 2025

Port forwarding on linux

To forward traffic received on port 27017 from server to 10.10.10.10, you'll need to set up the following iptables rules on the server:
# Enable IP forwarding in the kernel
sudo sysctl -w net.ipv4.ip_forward=1

# Make IP forwarding persistent across reboots
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf

# Add the PREROUTING rule to redirect incoming traffic
sudo iptables -t nat -A PREROUTING -p tcp --dport 27017 -j DNAT --to-destination 10.10.10.10:27017

# Add the POSTROUTING rule for masquerading (if the destination is on a different network)
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

# Allow forwarded traffic in the FORWARD chain
sudo iptables -A FORWARD -p tcp -d 10.10.10.10 --dport 27017 -j ACCEPT 
To make these rules persistent across reboots, save them with:
# For Debian/Ubuntu systems
sudo apt-get install iptables-persistent
sudo netfilter-persistent save

# OR for other distributions
sudo service iptables save

 

 

No hay comentarios:

Publicar un comentario