This guide explains how to implement a secure "Default-Deny" firewall strategy using iptables on your Linux server. This method blocks all network traffic unless explicitly permitted.
Warning: The order of these commands is absolutely critical. Executing the final DROP policy before allowing SSH access will instantly lock you out of your server. Always test rules carefully.
We must configure the necessary access rules first, before applying the global blocking policy.
Start with a clean configuration by deleting all existing iptables rules.
Add rules for all services that need to be externally accessible. For better maintenance, using user-defined chains is recommended.
To keep the INPUT chain clean, create a custom chain for open ports and redirect traffic there.
Integrate basic protection against floods and ensure proper logging of rejected packets.
Limits the rate of new connections per source IP to mitigate simple floods on a specific port.
iptables rules are temporary after entry and will be lost upon reboot. They must be permanently saved.
Before saving, verify that all rules have been correctly inserted into the chain.
Alternative nftables: On modern distributions, iptables is being succeeded by nftables. If you require a very complex rule set, switching to nftables is recommended for future-proofing your setup.