Skip to content
$ lynuxpro
← Linux

LESSON 17 OF 26 · INTERMEDIATE · 18 MIN

Firewalls: ufw, firewalld and iptables

In one minute

  • A firewall decides which network traffic may come in to, or go out of, a machine.
  • Firewalls keep attackers out. They are also a top cause of "it times out" problems. You need to find the one rule that is blocking traffic.
  • A firewall is a list of rules, such as "allow port 22" or "block everything else". Each packet is checked against the rules from top to bottom, and the first rule that matches wins. If no rule matches, a default rule decides. On Linux, every firewall tool (ufw, firewalld, iptables) programs the same engine inside the kernel, called netfilter.
  • 👉 Opening a port for a new app, locking down a new server, and fixing timeouts.

Think of it like: a security guard at a gate with a list. The guard reads the list from the top and follows the first line that fits you. If no line fits, they follow the sign at the bottom: "Everyone else: no entry".

Layers of firewalls

A packet travelling from the internet to your app may pass several firewalls:

Internet → AWS NACL (subnet) → Security group (instance) → Host firewall (ufw / firewalld) → App
OUTPUTterminalutf-8 · 1L
LayerStateful?Where it is set up
Network ACLNo: you must allow reply traffic yourselfAWS VPC subnet
Security groupYes: replies are allowed automaticallyAWS instance or network card
Host firewallYesOn the Linux server
Kubernetes NetworkPolicyYesInside the cluster

Stateful means the firewall remembers connections. If a request was allowed out, its reply is allowed back in automatically.

Why it matters: When you debug, check every layer. Any one of them can be the block.

Think of it like: a building with a gate (NACL), a front door (security group) and an office door (host firewall). You need all three open to reach the desk (app).

ufw on Ubuntu

ufw ("uncomplicated firewall") is Ubuntu's easy firewall tool.

👉 Any Ubuntu server.

sudo ufw statussudo ufw allow 22/tcpsudo ufw allow 80/tcpsudo ufw enable
SHELLbashutf-8 · 4L
CommandWhat it does
ufw status verboseShow the rules and default settings
ufw default deny incomingBlock everything coming in, unless allowed
ufw default allow outgoingAllow everything going out
ufw allow 443/tcpOpen a port
ufw allow from 10.0.0.0/16 to any port 5432Allow only one network to one port
ufw deny 23Block a port
ufw status numbered then ufw delete 3Remove rule number 3
ufw limit sshSlow down repeated SSH attempts (stops password guessing)

Allow SSH before you enable ufw on a remote server. Otherwise you cut off your own connection and cannot get back in.

firewalld on RHEL, Rocky and Amazon Linux

firewalld is the firewall tool on the Red Hat family. It groups rules into zones (public, internal, trusted). Each network card belongs to one zone.

👉 Any RHEL-family server.

sudo firewall-cmd --statesudo firewall-cmd --get-active-zonessudo firewall-cmd --zone=public --list-allsudo firewall-cmd --zone=public --add-service=http --permanentsudo firewall-cmd --zone=public --add-port=8080/tcp --permanentsudo firewall-cmd --reload
OUTPUTterminalutf-8 · 6L
  • Without --permanent: the change works now, but is lost on reload or reboot.
  • With --permanent: the change is saved, but only takes effect after --reload.
  • A common habit: test without --permanent, then repeat with it once it works.

Remember it: --permanent = saved for later. --reload = use it now. You usually need both.

iptables: what is underneath

ufw and older firewalld write iptables rules for you. Seeing iptables directly helps you understand any Linux firewall.

Rules are grouped into chains:

ChainTraffic
INPUTPackets coming to this machine
OUTPUTPackets leaving from this machine
FORWARDPackets passing through (routers, Docker, Kubernetes)

Rules are checked top to bottom. The first match wins. If nothing matches, the chain's policy (ACCEPT or DROP) is used.

sudo iptables -L -n -v
SHELLbashutf-8 · 1L
Chain INPUT (policy DROP) pkts bytes target  prot opt in  out  source        destination 9123  812K ACCEPT  all  --  lo  *    0.0.0.0/0     0.0.0.0/0 51K   9M   ACCEPT  all  --  *   *    0.0.0.0/0     0.0.0.0/0    ctstate RELATED,ESTABLISHED 210   12K  ACCEPT  tcp  --  *   *    0.0.0.0/0     0.0.0.0/0    tcp dpt:22 88    5K   ACCEPT  tcp  --  *   *    0.0.0.0/0     0.0.0.0/0    tcp dpt:80
OUTPUTterminalutf-8 · 6L

How to read it, line by line:

  1. Traffic from the machine to itself (lo) is allowed.
  2. Replies to connections that already exist are allowed.
  3. New connections on port 22 (SSH) are allowed.
  4. New connections on port 80 (HTTP) are allowed.
  5. Everything else is dropped, because the policy is DROP.

The pkts column counts how many packets matched each rule. It shows which rules are really being used, which is very useful when debugging.

Writing rules by hand:

sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT        # appendsudo iptables -I INPUT 1 -s 203.0.113.9 -j DROP            # insert at the topsudo iptables -D INPUT -p tcp --dport 443 -j ACCEPT        # deletesudo iptables-save > /etc/iptables/rules.v4                # persist
OUTPUTterminalutf-8 · 4L
ActionWhat happensWhat the client sees
ACCEPTLet it throughA normal connection
DROPThrow it away silentlyA timeout
REJECTThrow it away and say "no""Connection refused" straight away

Remember it: DROP = ignored = timeout. REJECT = told no = refused.

nftables

Newer distributions use nftables (nft) underneath. The iptables command often just translates into it. The ideas are the same: tables, chains, rules, first match wins. sudo nft list ruleset shows everything.

Docker and Kubernetes write rules too

  • Docker adds its own iptables rules for published ports. A port published with docker run -p 8080:80 can skip ufw entirely, because Docker's rules are checked first. If it should not be public, publish it as 127.0.0.1:8080:80, or use the DOCKER-USER chain.
  • Kubernetes (kube-proxy) writes thousands of iptables rules (or IPVS) for its Services. Never wipe iptables on a Kubernetes node.

Real-world scenario: port 8080 times out from other servers

ss -tulpnsudo ufw statussudo iptables -L -n -v
SHELLbashutf-8 · 3L
  1. ss shows the app listening on 0.0.0.0:8080. Good.
  2. curl localhost:8080 works on the server itself. The app is fine.
  3. From another server, it times out. Something is dropping packets.
  4. ufw status does not list 8080, and the default is "deny incoming". Found it.
  5. sudo ufw allow from 10.0.0.0/16 to any port 8080 proto tcp fixes it. It only opens the port to the VPC, not the whole internet.
  6. If it still times out, check the AWS security group and NACL next.

Common mistakes

  • Turning on a firewall over SSH without allowing port 22 first.
  • Forgetting --permanent with firewalld, so the fix disappears after a reboot.
  • Opening ports to 0.0.0.0/0 (everyone) when only the VPC or the load balancer needs access.
  • Running iptables -F (flush all rules) on a Docker or Kubernetes host. This breaks all container networking.
  • Checking only the server's firewall when the security group is the real block.

Practice

  1. Set ufw to deny incoming and allow outgoing, allow SSH and HTTP, then enable it.
  2. Allow PostgreSQL (5432) only from 10.0.0.0/16.
  3. Read the iptables output above and say what happens to a new connection on port 3306.
  4. Explain why a DROP rule causes a timeout, but a REJECT rule causes "connection refused".

Cheat sheet

Taskufwfirewalld
Statusufw status verbosefirewall-cmd --list-all
Open a portufw allow 8080/tcpfirewall-cmd --add-port=8080/tcp --permanent
Allow a networkufw allow from 10.0.0.0/16firewall-cmd --add-source=10.0.0.0/16 --permanent
ApplyAutomaticfirewall-cmd --reload
Low leveliptables -L -n -vnft list ruleset

Key takeaways

  • A firewall is a list of allow and block rules for network traffic.
  • Rules are read top to bottom, and the first match wins.
  • Traffic can pass several firewalls: NACL, security group, server firewall. Check them all.
  • Ubuntu uses ufw. Red Hat uses firewalld. Both sit on top of iptables or nftables.
  • DROP causes a timeout. REJECT causes "refused". Always allow SSH before turning a firewall on.