How does DNAT or port forwarding work on a Linux gateway?
BeginnerScenarioSecurity: Firewalls, SGs, NACLs & VPN
Answer
Situation read → Rewrites the destination address of incoming packets.
What I'd do →
- Enable forwarding:
sysctl -w net.ipv4.ip_forward=1 - Add the rule:
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 10.0.1.20:80 - Allow the forwarded traffic:
iptables -A FORWARD -p tcp -d 10.0.1.20 --dport 80 -j ACCEPT - Add MASQUERADE if the return path would not come back through the gateway
- List with
iptables -t nat -L -n -v
Why → PREROUTING runs before the routing decision, so rewriting there sends the packet to the new host.
Interview tip → Say nftables is the modern replacement for iptables.
More on Security: Firewalls, SGs, NACLs & VPN
Security group versus NACL: explain the differences.BeginnerHow do you let only your office IP SSH into a server?BeginnerWhat does it mean that a security group can reference another security group?BeginnerStateful versus stateless firewalls: what is the practical difference?BeginnerWhat is a WAF and where do you place it?BeginnerWhat is the difference between a site-to-site VPN and a client VPN?Beginner