Hero Image

Configure Firewall on Gentoo Linux

Configure iptables on Gentoo Linux

Gentoo uses iptables/nftables. This guide covers iptables.

Step 1 – Install

emerge --ask net-firewall/iptables

Step 2 – Basic ruleset (/etc/iptables/iptables.rules)

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]

# Allow established and related connections
-A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# Allow loopback
-A INPUT -i lo -j ACCEPT

# Allow SSH
-A INPUT -p tcp --dport 22 -j ACCEPT

# Allow HTTP and HTTPS
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Allow ICMP ping
-A INPUT -p icmp --icmp-type echo-request -j ACCEPT

COMMIT

Step 3 – Apply rules

iptables-restore < /etc/iptables/iptables.rules

Step 4 – Persist across reboots

rc-update add iptables default
rc-service iptables save

Step 5 – View rules

iptables -L -n -v --line-numbers