Interview Prep/Networking/Security: Firewalls, SGs, NACLs & VPN

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 →

  1. Enable forwarding: sysctl -w net.ipv4.ip_forward=1
  2. Add the rule: iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 10.0.1.20:80
  3. Allow the forwarded traffic: iptables -A FORWARD -p tcp -d 10.0.1.20 --dport 80 -j ACCEPT
  4. Add MASQUERADE if the return path would not come back through the gateway
  5. 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.

All Networking questions Roadmap: Networking for DevOps

More on Security: Firewalls, SGs, NACLs & VPN