What is a firewall, and how does it protect a server?

A firewall decides which network traffic may reach a machine and which may leave it. On a dedicated server with a public address, it is the first piece of configuration worth getting right.

What does a firewall actually do?

It compares every packet against an ordered list of rules and accepts, drops or rejects it. Rules match on things such as the source and destination address, the protocol, the port and the state of the connection the packet belongs to. A web server, for example, might accept new connections on ports 80 and 443 from anyone, SSH on port 22 only from an office address, and nothing else. Everything a firewall decides happens before the packet reaches the application, so a service that is blocked never even sees the attempt.

What does stateful mean?

A stateful firewall remembers connections. Once it has allowed an outgoing request or an incoming connection, it automatically accepts the reply packets that belong to it. That makes rules short and safe: you describe which new connections are welcome, and one rule lets established traffic flow. Without state you would need matching rules for both directions of every conversation, which is easy to get wrong. Linux's connection tracking provides this for nftables and iptables.

What is a default-deny policy?

A rule set whose final verdict is to drop anything not explicitly allowed. It is the safest starting point, because a service you install later stays unreachable until you decide to open it. The opposite, allowing everything and blocking known bad traffic, leaves every forgotten service exposed. Default deny for incoming traffic and default allow for outgoing traffic is the usual balance on a server.

table inet edge_guard {
  chain from_outside {
    type filter hook input priority filter - 5; policy drop; # unmatched packets end here
    ct state vmap { established : accept, related : accept, invalid : drop }
    iifname "lo" accept
    ip protocol icmp accept
    meta l4proto ipv6-icmp accept
    tcp dport { http, https } accept
    ip saddr 198.51.100.24 tcp dport ssh accept
  }
}

Note the two ICMP lines: IPv6 cannot work without neighbour discovery and path MTU messages, and blocking all ICMP also breaks monitoring probes.

Host firewall or network firewall?

A host firewall runs on the server itself; a network firewall sits in front of one or many machines. Network firewalls are convenient for whole offices, but on a rented server the host firewall is what you control directly, and it travels with the machine when you rebuild it from a script. Every server we rent also sits behind DDoS filtering in the network, which removes floods; it is not a replacement for your own rule set, which decides which services are reachable at all.

LayerWho manages itWhat it stops
Edge DDoS filteringThe network, always onVolumetric and protocol floods
Host firewall (nftables, firewalld, ufw)You, with full rootAccess to ports and services you have not opened
Application firewall (rate limits, request rules)You, in the web server or a moduleAbusive requests to open services

Which tool should I use on Linux?

nftables is the modern kernel framework on every distribution we install. firewalld, the default front end on AlmaLinux, Rocky Linux and Fedora, writes nftables rules from zones and services. ufw on Ubuntu offers short commands for common cases. iptables commands still work through a compatibility layer, so older scripts keep running. Pick one front end per machine; mixing them leads to confusing rule orders. Our Build an iptables firewall guide covers the classic syntax step by step.

firewall-cmd --permanent --zone=public --add-port=443/tcp --add-port=443/udp
firewall-cmd --permanent --zone=public --add-rich-rule='rule family=ipv4 source address=198.51.100.24 service name=ssh accept'
firewall-cmd --reload

How do I avoid locking myself out?

Test rules with a safety net. Before loading a new rule set remotely, schedule a job that restores the old one in five minutes, then cancel it once you have confirmed you can still connect. If you do lock yourself out, our console can boot the machine into rescue mode, where you mount the disk and fix the rule file; there is no IPMI or KVM console on our servers, so the rescue boot is the way back in.

nft list ruleset > /root/fw-known-good.nft
echo "nft -f /root/fw-known-good.nft" | at now + 5 minutes
nft -c -f /etc/nftables.d/edge_guard.nft && nft -f /etc/nftables.d/edge_guard.nft

What about outgoing traffic?

Restricting it is worthwhile on servers that hold sensitive data. If a database server only ever needs to reach package mirrors and a backup host, allowing just those destinations means stolen data has nowhere to go, and malware cannot easily call home. Start with logging outgoing connections for a week to learn what is normal. For a wider security baseline, read Hardening a new dedicated server.

Related pages

Build an iptables firewall

A default-deny host firewall with iptables or nftables: allow SSH and web, rate-limit new connections, cover IPv6 and keep the rules across reboots.

Read more

Hardening 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 more

DDoS protection on bare metal

How network edge filtering stops volumetric attacks before they reach your port, and what you still handle yourself.

Read more

Rescue mode walkthrough

A full rescue session from start to finish: boot the network system, find and mount your drives, repair, chroot in and return to disk boot.

Read more

Questions and answers

What is the difference between a stateful and a stateless firewall?

A stateful firewall tracks connections and accepts replies automatically; a stateless one judges each packet alone and needs rules for both directions.

Should a server firewall block all ICMP?

No. IPv6 needs ICMPv6 to work, path MTU discovery relies on ICMP, and monitoring uses ping. Allow ICMP and filter only what you must.

Does DDoS protection replace a firewall?

No. Edge filtering removes floods, while your host firewall decides which services may be reached at all. You need both.

Which Linux firewall tool should I use?

nftables directly, or a front end such as firewalld or ufw. Use one per machine so rule order stays predictable.

What if my firewall locks me out of the server?

Boot into rescue mode from our console, mount the system disk, correct the rule file and reboot to the disk.