Lesson 5 of 14 · 11 min · Ports, firewalls and remote access

Firewalls, security groups and opening ports safely

Covers: Ubuntu 24.04 LTS, iptables 1.8 (nf_tables backend), nftables 1.0. Updated 13 Nov 2026. Part of the DevOps Engineer roadmap.

What you will learn

  • What a firewall decides and on what information
  • Stateful filtering, default policies, and the order rules are evaluated
  • DROP versus REJECT, with real timings
  • Writing rules with iptables, nftables and ufw
  • How cloud security groups and network ACLs differ
  • Principles for exposing as little as possible

In simple words

A firewall is a security guard at the gate with a list of rules: who may come in, through which door, and from where. Everything not on the list is turned away. This lesson shows how the guard reads that list and how to open only the doors you really need.

Words to know

  • Firewall: software that allows or blocks network traffic according to rules.
  • Rule: one line of the list, such as "allow port 22 from the office address".
  • Stateful: the firewall remembers connections it allowed, so replies get back in automatically.
  • Security group: a cloud firewall attached to a virtual machine.

What a firewall does

A firewall decides, for each packet or connection, whether to allow or block it, based on rules over: source and destination IP, protocol, port, direction, and (for stateful firewalls) whether the packet belongs to an existing connection. Layer 7 firewalls such as a WAF also look at the content (URL, headers).

Firewalls exist at several levels, and a connection must pass all of them:

  1. The host firewall inside the machine (iptables/nftables, ufw, firewalld).
  2. The cloud's virtual firewalls: security groups and network ACLs.
  3. Network firewalls and gateways in the path (corporate, ISP).

"The port is open on the machine but I cannot connect" almost always means a different layer is blocking it.

Stateful filtering

A stateless filter judges every packet alone. A stateful firewall tracks connections (Linux does this with conntrack). The consequence is convenient: you allow a connection inbound and the replies are allowed automatically; you do not write a rule for the return traffic. The typical host policy is therefore:

  1. Allow packets that belong to established or related connections.
  2. Allow inbound new connections only to specific ports.
  3. Deny everything else (default deny).

DROP versus REJECT

When a rule blocks a packet it can do two things, and the client sees a different failure. Tested for real against a local web server on port 8080:

iptables -A INPUT -p tcp --dport 8080 -j REJECT --reject-with tcp-reset
curl -m 5 http://IP:8080/        # fails at once: exit code 7, "connection refused", 0.0001 s
iptables -F INPUT
iptables -A INPUT -p tcp --dport 8080 -j DROP
curl -m 3 http://IP:8080/        # fails after the full timeout: exit code 28, 3.0 s
  • REJECT sends back an error (a TCP reset or ICMP unreachable). The client knows immediately. Friendlier for internal systems and easier to debug.
  • DROP silently discards. The client waits until a timeout. It reveals less to a scanner but makes legitimate failures slow and confusing.

Cloud security groups behave like DROP. So timed out usually means a firewall; refused usually means no listener.

iptables and nftables

Linux packet filtering lives in the kernel (netfilter). Two front ends: the older iptables and the newer nftables (nft). On current distributions iptables is often a compatibility layer over nftables. Rules are organised in chains (INPUT for traffic to this host, OUTPUT from it, FORWARD through it) and evaluated top to bottom; the first match decides, otherwise the chain's default policy applies.

sudo iptables -L INPUT -n -v --line-numbers    # list rules with counters
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT          # append a rule
sudo iptables -I INPUT 1 -s 203.0.113.5 -j DROP             # insert at position 1
sudo iptables -D INPUT 3                                    # delete rule number 3
sudo iptables-save                                          # print rules in saveable form

A sensible minimal server policy (shown to learn from; do not paste it over SSH without a way back in, see the warning below):

iptables -P INPUT DROP                                          # default: drop inbound
iptables -A INPUT -i lo -j ACCEPT                               # loopback
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT   # replies
iptables -A INPUT -p tcp --dport 22 -j ACCEPT                   # SSH
iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT # web

Allowing a port only from one source (verified: another address got a timeout, the allowed one got 200):

iptables -A INPUT -p tcp --dport 8080 -s 10.0.0.5 -j ACCEPT
iptables -A INPUT -p tcp --dport 8080 -j DROP

The same idea in nftables:

nft add table inet demo
nft add chain inet demo input '{ type filter hook input priority 0; policy accept; }'
nft add rule inet demo input tcp dport 8080 drop
nft list table inet demo
table inet demo {
	chain input {
		type filter hook input priority filter; policy accept;
		tcp dport 8080 drop
	}
}

Rules added on the command line are lost on reboot unless saved (netfilter-persistent save on Debian/Ubuntu, /etc/nftables.conf with the nftables service). Forgetting this is a classic outage: the server reboots and its ports are closed (or, the reverse, open).

Warning: setting a default DROP policy over an SSH session without first allowing SSH locks you out. On a remote machine, add the allow rule first, test from a second session, and keep console access.

ufw and firewalld

Friendlier front ends exist:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow from 10.0.0.0/24 to any port 5432 proto tcp
sudo ufw enable
sudo ufw status numbered

ufw is the Ubuntu choice; firewalld (firewall-cmd --add-service=https --permanent) is the RHEL family's. Use whichever your platform uses, and only one at a time; do not mix raw iptables and these tools carelessly.

Docker also adds iptables rules for published ports (-p 8080:80), and those can bypass ufw: a published container port may be open to the world even when ufw says otherwise. Bind to 127.0.0.1 (-p 127.0.0.1:8080:80) when it should be local.

Cloud security groups and network ACLs

Security group Network ACL (AWS)
Applies to an instance or network interface a whole subnet
State stateful stateless (return traffic needs its own rule)
Rules allow only; everything else denied allow and deny, evaluated in order
Typical use the main control a coarse extra layer

Security groups can reference other security groups as the source: "allow port 5432 from the app servers' group". That is better than listing IP addresses, because instances come and go. Default behaviour: all inbound denied, all outbound allowed.

A reasonable three-tier setup: the load balancer's group allows 80/443 from anywhere; the app group allows 8080 only from the load balancer's group; the database group allows 5432 only from the app group.

Principles for exposing ports

  1. Default deny. Open only what a service needs.
  2. Narrow the source: specific IP ranges or security groups, not 0.0.0.0/0, unless the service is truly public (web on 80/443).
  3. Never expose databases and admin interfaces (Redis, MongoDB, Elasticsearch, Docker's API on 2375, Kubernetes dashboards) to the internet. Many real breaches start with an open Redis or Elasticsearch port.
  4. SSH: keys only, restricted source, or no public SSH at all (use a bastion, VPN or SSM Session Manager).
  5. Record and review rules. Manage them as code (Terraform, covered later) so changes are reviewed.
  6. Scan yourself: nmap (against your own hosts only) or ss -tlnp shows what is actually listening.

Common mistakes

  • Opening 0.0.0.0/0 on port 22 or a database port "temporarily".
  • Forgetting UDP (DNS, WireGuard, QUIC).
  • Not saving rules, or locking yourself out of a remote machine.
  • Checking only one layer: host rules look fine but the security group blocks.
  • Assuming ufw protects published Docker ports.
  • Rule order mistakes: a broad DROP above a specific ACCEPT.

Try it yourself

  1. In a throwaway VM or privileged container, reproduce DROP and REJECT against a local http.server and compare the curl timings and exit codes.
  2. Write a rule that allows port 8080 only from your own IP and blocks everyone else, and test from a second address if you can.
  3. Draw the security groups for a web app with a load balancer, two app servers and a database, naming the source of every rule.
  4. Run ss -tlnp on your machine and decide which listeners should not be reachable from outside.

Check yourself

What is the difference between DROP and REJECT?

DROP discards the packet silently, so the client times out. REJECT answers with an error (TCP reset or ICMP), so the client fails immediately with "connection refused".

Why do you not need a rule for return traffic in a stateful firewall?

It tracks connections, so packets that belong to an established connection are allowed automatically. Only the initial packet of a new connection is checked against the rules.

What is the difference between a security group and a network ACL?

A security group is stateful, attached to instances, and only has allow rules. A network ACL is stateless, attached to subnets, and has ordered allow and deny rules, so return traffic must be allowed explicitly.

You can reach a service from the server itself but not from outside. What do you check?

The bind address (ss -tlnp: 127.0.0.1 vs 0.0.0.0), the host firewall, the cloud security group and network ACL, and the route/NAT or load balancer in front.

Interview questions

How would you secure a new internet-facing web server?

Default-deny inbound; allow 80/443 (and SSH only from a trusted range or via a bastion/VPN); keys-only SSH; keep the OS patched; run services as non-root users; no databases exposed; logging and monitoring; rules managed as code.

A connection to a port hangs, then times out. What does that suggest?

Packets are being dropped, typically by a firewall or security group, or there is a routing problem. A closed port with no firewall would be refused immediately.

Why can Docker published ports bypass ufw?

Docker inserts its own iptables rules that take effect before ufw's, so published ports are reachable regardless of ufw. Bind published ports to localhost or use Docker's own filtering options.

Summary

  • Firewalls decide per packet/connection; stateful ones allow replies automatically; default deny is the right base.
  • DROP gives timeouts, REJECT gives immediate refusals; rules are evaluated top to bottom, and saved rules survive reboot only if persisted.
  • Host firewalls (iptables/nftables/ufw/firewalld) and cloud security groups are separate layers; reference groups, not IPs.
  • Expose the minimum, never databases or admin ports, and manage rules as code.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on Networking for DevOps for DevOps Engineer and scores every answer, with what to fix.