DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

NetworkingLesson 8 of 12
Your progress · 0%
On this page
  1. Stateful and stateless
  2. iptables and nftables
  3. Security groups and NACLs
  4. Default deny
↖ Course roadmap
Lesson 8 · Firewalls

Firewalls: allow only the traffic you need

Understand stateful and stateless filtering, inspect nftables, and separate host firewalls from cloud security groups and network ACLs.

23 min read · beginner · hands-on

By the end of this lesson you'll be able to

0 of 3 complete
Lab status

Verified lab: an inet nftables table with a drop policy and loopback allow rule ran inside an Alpine container. The listed rules are real. No host firewall was changed.
Cloud lab: none. Security groups and network ACLs are explained; no AWS resources were created.

Before host firewalls, a server often relied on each application to reject unwanted traffic. One missed setting could expose a service. A firewall makes the network policy explicit and checks packets before they reach a process. It can remember connections or treat each packet independently. In this lesson, you will read the policy and locate which firewall layer made a decision.

Stateful and stateless

A stateful firewall tracks a connection. After an allowed TCP request, it recognizes packets belonging to that flow and permits the reply. A stateless filter evaluates each packet using rules, without remembering the earlier request. It needs rules for both directions, including return ports when relevant.

This difference matters when debugging. If a stateful rule allows outbound HTTPS but the response never arrives, check the route, the server, and other stateless filters. Do not assume every firewall on the path shares the same connection state.

iptables and nftables

Linux firewall rules are evaluated at kernel networking hooks. nftables is the current framework; iptables commands may use a compatibility layer. A chain attaches rules to a point such as incoming local traffic (input) or forwarded traffic (forward). A base chain also has a policy for packets that reach its end.

The isolated lab created an input chain with a drop policy and allowed loopback:

$ nft list chain inet lesson input
table inet lesson {
    chain input {
        type filter hook input priority filter; policy drop;
        iif "lo" accept
    }
}

The same ruleset reports separate default-accept policies in the iptables compatibility view. The lesson uses only the nftables chain it created. Do not copy a policy change to a remote host without an approved recovery path.

Security groups and NACLs

In AWS, a security group attaches to network interfaces and is stateful. A network ACL applies at the subnet boundary, is stateless, and checks numbered rules in order. A packet can pass one control and fail the other. The instance's host firewall and listening process still matter too.

Use security groups for the normal workload allow-list. Use a network ACL when you need a subnet-wide rule or an explicit deny. A common mistake is allowing inbound traffic in a NACL but forgetting the outbound reply rule. For console examples, see the AWS VPC lesson.

Default deny

With default deny, the firewall rejects traffic unless a rule allows it. Start by listing existing rules and finding the access path you use to manage the host. Then add the smallest required allow rule, test it, and only then tighten the default policy. This order prevents locking yourself out.

nft list ruleset

Prefer explicit service ports and trusted source ranges over broad rules. A firewall is one layer, not identity management: a permitted connection still needs application authentication and authorization.

Keep firewall experiments isolated

The mini mission changes a policy only inside a temporary container. Do not set a default-drop policy on a remote host until you have tested the rules and have console or out-of-band recovery.

⌘
Mini mission

Inspect a default-deny chain

Create a tiny nftables input chain in an Alpine container, allow loopback, inspect the resulting policy, and let the container remove it.

Input: Docker with NET_ADMIN capability availableOutput: An nftables chain with a drop policy and a loopback rule
Reveal one safe solution
docker run --rm --cap-add NET_ADMIN alpine:3.22 sh -ec '
  apk add --no-cache nftables iptables
  nft add table inet lesson
  nft add chain inet lesson input "{ type filter hook input priority 0; policy drop; }"
  nft add rule inet lesson input iif lo accept
  nft list chain inet lesson input
  iptables -S
'

The container exits after the inspection, so this policy cannot lock you out of the host.

Try it yourself

0 of 4 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 3

A stateful firewall allows an outbound TCP connection. What usually happens to matching reply packets?

Go deeper

Official reading for this lesson

  • Docsnftables manualNetfilter ↗
  • DocsControl traffic using security groupsAWS Docs ↗
  • DocsControl subnet traffic with network ACLsAWS Docs ↗
  • GuideAWS VPC lessonDevOps Academy ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousRouting and NAT: finding the next hopNext lesson →Proxies and load balancing: one front door