Recover from firewall lockout by resetting iptables rules
If you find yourself unable to reach your VPS, but able to reach other parts of our network, you may have accidentally blocked yourself with iptables firewall rules.
The safest way to clear iptables rules is through VNC console access:
# Flush all rules iptables -F iptables -X iptables -t nat -F iptables -t nat -X iptables -t mangle -F iptables -t mangle -X # Set default policies to ACCEPT iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT
You can create a script to completely reset iptables:
#!/bin/bash # Save as reset-iptables.sh echo "Flushing all iptables rules..." # Flush all rules iptables -F iptables -X iptables -t nat -F iptables -t nat -X iptables -t mangle -F iptables -t mangle -X iptables -t raw -F iptables -t raw -X # Set default policies to ACCEPT iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT echo "iptables rules cleared!" echo "Current rules:" iptables -L -v -n
Make it executable and run it:
chmod +x reset-iptables.sh ./reset-iptables.sh
If your iptables rules aren't saved to persist after reboot, you can simply reboot your VPS from SolusVM. This will clear the rules until you apply them again.
This only works if you haven't made the rules persistent with iptables-save or iptables-persistent package.
After clearing rules, verify they're gone:
# List all rules iptables -L -v -n # Check default policies iptables -L | grep policy
# Allow your IP address before setting DROP policy iptables -A INPUT -s YOUR_IP_ADDRESS -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Apply rules temporarily first and test. If you get locked out, a reboot will restore access (if rules aren't persistent).
Create a script that automatically clears iptables after 5 minutes unless you cancel it:
# Apply your rules ./apply-firewall-rules.sh # Set auto-clear in 5 minutes at now + 5 minutes <<EOF /root/reset-iptables.sh EOF # If everything works, cancel the reset: atrm [job_number]
Always know how to access your VNC console before experimenting with firewall rules.
Consider using these tools instead of raw iptables:
Simple firewall management for Ubuntu/Debian
Dynamic firewall manager for CentOS/RHEL
Feature-rich firewall with web interface
These tools have safety features to prevent lockouts.