Back to library
Defensive Intermediate Free

Hardened IPTables firewall baseline — default deny with explicit allow rules

Hardened IPTables firewall baseline — default deny with explicit allow rules

IPTables / Linux Hardening

Log in to save

Command

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Syntax

iptables -P <chain> <policy> && iptables -A <chain> <conditions> -j <action>

Use case

Initial hardening of a Linux server to reduce attack surface. Applied during server provisioning or after a security audit identifies overly permissive firewall rules.

Explanation

Implements a deny-all, allow-specific firewall: • -P INPUT DROP → Default deny all inbound • -P FORWARD DROP → Block packet forwarding • -P OUTPUT ACCEPT → Allow outbound (restrict further if needed) • lo (loopback) always allowed for localhost • ESTABLISHED,RELATED allows responses to initiated connections • SSH restricted to internal subnet only • HTTP/HTTPS open to all Save with: iptables-save > /etc/iptables/rules.v4

Example output

Chain INPUT (policy DROP)
target     prot  source               destination
ACCEPT     all   anywhere             anywhere    (lo)
ACCEPT     all   anywhere             anywhere    ctstate RELATED,ESTABLISHED
ACCEPT     tcp   192.168.1.0/24       anywhere    tcp dpt:ssh
ACCEPT     tcp   anywhere             anywhere    tcp dpt:http
ACCEPT     tcp   anywhere             anywhere    tcp dpt:https

More examples

Primary example

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Initial hardening of a Linux server to reduce attack surface. Applied during server provisioning or after a security audit identifies overly permissive firewall rules.

linux iptables firewall hardening network security

Related

Defensive Beginner Free

Audit all listening services and count active established connections

Audit all listening services and count active established connections

ss -tlnp | grep LISTEN && netstat -an | grep ESTABLISHED | wc -l
ss netstat hardening audit network
Linux CLI / Linux Hardening Open