How do you make iptables rules survive a reboot and avoid locking yourself out?
IntermediateScenarioSecurity: Firewalls, SGs, NACLs & VPN
Answer
Situation read → Rules in memory vanish and mistakes on remote hosts cut your session.
What I'd do →
- Apply rules while keeping an escape:
iptables -A INPUT -p tcp --dport 22 -j ACCEPTbefore setting-P INPUT DROP - Use a timed rollback:
(sleep 120; iptables -F; iptables -P INPUT ACCEPT) &then cancel it once verified - Save with
iptables-save > /etc/iptables/rules.v4andnetfilter-persistent - Prefer nftables or config management
- Keep console access
Why → Rule order and default policy decide whether you can still log in.
Interview tip → Say always allow ESTABLISHED,RELATED and SSH first.
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