Using `iptables` for Advanced Firewall Rules
`iptables` is a powerful but complex command-line firewall utility. While UFW is recommended for simplicity, `iptables` offers more granular control.
Example: Basic Ruleset
# Flush all current rules
sudo iptables -F
# Allow all loopback traffic
sudo iptables -A INPUT -i lo -j ACCEPT
# Allow established and related connections
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Allow SSH, HTTP, HTTPS
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Drop all other incoming traffic
sudo iptables -P INPUT DROP
CopyWarning: An incorrect `iptables` rule can lock you out of your server.