A default-deny firewall with iptables
A host firewall decides which of your services the internet can reach. Twenty lines of rules are enough for most servers, provided you apply them without locking yourself out.
Why run a firewall on the server itself?
Because it is the only filter that knows which services you meant to expose. Our network filters attack traffic before it reaches your port, but it cannot tell that your database listens on 5432 by accident. A host firewall that drops everything not explicitly allowed turns a forgotten service into a non-event. On a dedicated server there is no shared hypervisor rule set either: the machine's own rules are the policy.
iptables or nftables?
Both work; nftables is the modern framework, and on current distributions the iptables command is itself a front end that writes nftables rules. Use whichever syntax your team already knows. Avoid mixing them on one machine, and avoid running two managers at once, for example ufw and firewalld together, because each one overwrites the other's work.
| System | Default tool | Saved rules live in |
|---|---|---|
| Debian 12 and 13 | nftables (service off by default) | /etc/nftables.conf |
| Ubuntu 22.04 and 24.04 | ufw front end, iptables-nft backend | /etc/ufw/ or /etc/iptables/rules.v4 |
| AlmaLinux 9 and 10, Rocky Linux 9 and 10 | firewalld on nftables | /etc/firewalld/ |
| Fedora 43 | firewalld on nftables | /etc/firewalld/ |
How do I avoid locking myself out?
Allow your SSH session before you set any drop policy, and arm an automatic undo. Schedule a rule flush a few minutes ahead, apply the new rules, open a second SSH session to prove you can still log in, then cancel the undo. If you do get locked out, rescue mode lets you edit the saved rules from outside the system.
echo "iptables -P INPUT ACCEPT; iptables -F" | at now + 5 minutes # apply rules, test a new login, then: atq && atrm 1
Which rules belong in a minimal policy?
Accept loopback and established traffic, accept the few ports you serve, accept useful ICMP, and drop the rest. This example serves SSH and a website:
iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -m conntrack --ctstate INVALID -j DROP iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 5/s -j ACCEPT iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set --name sshlim iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 60 --hitcount 6 --name sshlim -j DROP iptables -A INPUT -p tcp -m multiport --dports 22,80,443 -j ACCEPT iptables -P INPUT DROP iptables -P FORWARD DROP
The two recent rules allow five new SSH connections per minute from one address and drop the sixth, which slows password guessing to a crawl without bothering a human. With keys-only login this is a noise filter rather than a necessity.
How do I cover IPv6?
Mirror the rules with ip6tables, and allow ICMPv6, which IPv6 needs to work at all. Neighbour discovery and packet-too-big messages travel over ICMPv6; blocking them breaks connectivity in ways that look like random timeouts. Every server here has an IPv6 block routed to it, so an IPv4-only firewall leaves the second door open.
ip6tables -A INPUT -i lo -j ACCEPT ip6tables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT ip6tables -A INPUT -p ipv6-icmp -j ACCEPT ip6tables -A INPUT -p tcp -m multiport --dports 22,80,443 -j ACCEPT ip6tables -P INPUT DROP
The same policy in nftables
One table of type inet handles both protocols, which halves the work. Save it as /etc/nftables.conf and enable the service.
table inet hostfw {
chain input {
type filter hook input priority filter
policy drop
iif "lo" accept
ct state related,established counter accept
ct state invalid drop
meta l4proto { ipv6-icmp, icmp } accept
tcp dport { ssh, http, https } accept
}
}
nft -c -f /etc/nftables.conf && systemctl enable --now nftables
How do I keep iptables rules after a reboot?
Save them to a file that a service loads at boot. On Debian and Ubuntu, the iptables-persistent package does this; run netfilter-persistent save after each change. On Enterprise Linux, either use firewalld natively or disable it and use the iptables-services package with service iptables save. Reboot once from the console after setting up persistence and confirm that iptables -S shows your rules.
What about outgoing traffic?
Most servers leave output open, which is reasonable. Filtering egress helps when you want to stop a compromised application from calling home: allow DNS, NTP, package mirrors and the specific services your application talks to, then drop the rest. For game hosting, where the open ports are many and traffic is UDP, see Firewall for game servers; for a broader look at attack filtering in front of your port, read DDoS protection on bare metal.
Related pages
Firewall for game servers
Open exactly the UDP and TCP ports a game needs, protect query ports from reflection abuse and keep lag low under load.
Read moreDDoS protection on bare metal
How network edge filtering stops volumetric attacks before they reach your port, and what you still handle yourself.
Read moreHardening a new dedicated server
The first hour on a fresh machine: SSH keys, a firewall, automatic updates, brute force blocking and a habit of checking what listens.
Read moreRespond to a compromised server
What to do in the first hour after you suspect an intruder on a rented machine: contain, preserve evidence, rebuild and close the hole.
Read moreQuestions and answers
Is a host firewall needed if the network filters DDoS traffic?
Yes. The network filter removes attack floods, while the host firewall decides which of your services are reachable at all. They solve different problems.
Should I use iptables, nftables, ufw or firewalld?
Any of them, but only one per machine. nftables is the modern base; ufw and firewalld are friendlier front ends that write to it.
What if I lock myself out with a bad rule?
Boot rescue mode from the console, mount the system and edit the saved rules file, then boot from disk again.
Does IPv6 traffic need its own firewall rules?
With iptables yes, through ip6tables. An nftables table of type inet covers IPv4 and IPv6 at once. Always allow ICMPv6.
How do I make iptables rules survive a reboot?
Install iptables-persistent on Debian or Ubuntu and run netfilter-persistent save, or use the iptables-services package on Enterprise Linux.