This guide explains how to implement a secure default-deny firewall strategy using iptables on your Linux server — all traffic is blocked unless explicitly allowed.
Order is critical! Applying the final DROP policy before allowing SSH access will immediately lock you out of your server. Always test rules carefully and keep a console connection available via the PowerPanel as a fallback.
Configure all necessary access rules before applying the global block policy.
Start with a clean slate by removing all existing iptables rules:
Allow all internal traffic on the loopback interface — essential for local services:
Allow packets belonging to already-established connections — required so responses to outgoing requests (e.g. DNS, updates) can reach the server:
Allow SSH before setting the DROP policy — otherwise you will lock yourself out immediately:
Only now — after all allow rules are in place — set the global block policy. Any packet not matched by a previous rule will be dropped:
Add rules for all services that need to be reachable from outside. Using a custom chain keeps the INPUT chain clean and maintainable:
Create a dedicated chain for open ports and jump to it from INPUT:
Critical ports like database ports should only be opened for known, trusted IP addresses:
Optional — allows inbound and outbound ping requests for reachability checks:
Add basic flood protection and logging for dropped packets:
Limits the rate of new connections per source IP to mitigate simple flood attacks:
Blocks packets typically used for port scans and specific attack vectors:
Insert these rules directly before the final DROP policy — with a rate limit to prevent log flooding:
iptables rules are temporary and will be lost after a reboot. They must be saved persistently.
Install iptables-persistent — it automatically loads rules on every reboot:
Alternative — nftables: On modern distributions (Debian 10+, Ubuntu 20.04+, RHEL 8+), iptables is being replaced by nftables. For new server deployments, we recommend nftables — it is faster, more readable, and actively maintained. View the nftables guide →
For a complete reference of all iptables options, refer to the official documentation.