Course contents · Lesson 13 of 22

Firewalls and Security Groups Explained Simply

Networks for Engineers · Lesson 13 of 22 · 10 min Engineer

You start a new cloud server, run curl against it from your laptop, and the command hangs before giving up, although the app is running. Most often, a firewall is quietly throwing your packets away. This lesson explains firewalls and security groups: what a rule says, why replies get back in, how cloud security groups differ from network ACLs, and what an error tells you.

What a firewall rule says

In lesson 2, a firewall was a filter that blocks unwanted traffic. More exactly, it is software that checks each packet against a list of rules and lets it through or discards it. It runs on laptops, home routers, company networks and in the cloud.

Each rule names:

  • Action: allow or deny.
  • Direction: inbound (arriving) or outbound (leaving).
  • Protocol: TCP or UDP (lesson 5), or ICMP (lesson 12), which has no ports.
  • Port: one, such as 443, or a range.
  • Address range in CIDR notation (lesson 11): the source for inbound rules, the destination for outbound ones.

Traffic that no rule matches gets the firewall's default. A safe firewall denies inbound traffic by default, as the guard turns away anyone unlisted, and often allows all outbound. These are an illustrative web server's inbound rules; 0.0.0.0/0, the default route's range in lesson 12, means any IPv4 address.

ActionProtocolPortSourceWhy
AllowTCP4430.0.0.0/0HTTPS from anyone
AllowTCP22203.0.113.0/24SSH from the office
DenyAnyAnyAnything elseThe default

When rules overlap, many firewalls check them in order and stop at the first match, so order matters. Cloud security groups hold only allow rules and let a packet in if any rule allows it.

Stateful firewalls let replies back in

When your laptop fetches a page, the request leaves from a temporary port, such as 50000, and the reply returns from port 443 to port 50000. If the laptop's firewall denies all inbound traffic, how does the reply get in?

A stateful firewall remembers connections. When an allowed packet starts one, the firewall records the protocol, both addresses and both ports in a connection table, much like lesson 12's NAT table. Later packets matching an entry, in either direction, pass without a rule check until the connection closes or goes idle: the guard waving a resident back in.

A stateless firewall remembers nothing and checks every packet against its rules, so letting replies in means allowing every temporary port they might return to, which opens far more than the replies need.

In the flowchart, notice that the rule list is consulted only for packets that start something new.

Flowchart. A packet arrives: TCP to port 443 from 198.51.100.23. First question: is it part of a known connection, in the connection table? Yes: let it straight in, because replies skip the rule list. No: does it match an allow rule by protocol, port and source? Yes: let it in and add the connection to the table. Both yes paths reach the instance, the program listening on port 443. No: drop it silently, with no reply, so the sender times out. A note says the default is deny unless listed
Replies skip the rule list; anything new must match an allow rule or it is dropped.

Security groups and network ACLs

In the cloud, your servers live in a VPC (virtual private cloud), a private network divided into subnets, as What Is a VPC? in Cloud Fundamentals explains. Each server is an instance, a virtual machine. AWS gives you two firewalls; the names and details here are AWS's.

A security group is a stateful firewall attached to an instance, strictly to its network interface, its virtual network card. It has allow rules only, and an instance's groups add up. A new group allows no inbound and all outbound traffic. A rule's source can be an address range or another security group, meaning any instance in that group.

A network ACL (access control list) is a stateless firewall on a subnet, checking every packet that enters or leaves. Its numbered rules can allow or deny and are checked from the lowest number up; the first match decides. The default ACL allows everything.

Being stateless, an ACL needs rules for replies: a web server's subnet must allow outbound traffic to visitors' temporary ports, a wide range such as 1024–65535, because operating systems pick them from different ranges.

In the analogy, a security group is a guard at each flat's door who remembers who went out; a network ACL is a checkpoint at the block gate with a printed list and no memory.

Comparison table of AWS's two cloud firewalls. Security group: attached to an instance, its network interface; remembers connections, stateful; replies allowed automatically; allow rules only; all rules checked together; sources can be address ranges or other groups; a new group has no inbound rules and allows all outbound. Network ACL: attached to a subnet; stateless; replies need rules of their own; allow and deny rules; numbered, first match wins; address ranges only; the default ACL allows everything. In the analogy, a guard at each flat's door versus a checkpoint at the block gate
Security groups remember connections and only allow; network ACLs remember nothing and can also deny.

Other clouds differ: Azure's network security groups and Google Cloud's VPC firewall rules are stateful but also accept deny rules, ordered by priority.

Least privilege: open only what you need

Lesson 9's least privilege, giving each user only the access their job needs, applies to ports too: open only the ports a service uses, to only the sources that need them.

So narrow the source: allow SSH on port 22 from your office range, such as 203.0.113.0/24, or one address as a /32, never from 0.0.0.0/0. And keep internal ports internal: a database port such as 5432 never faces the internet. On AWS, 0.0.0.0/0 covers IPv4 only; IPv6's "anywhere", ::/0, needs its own rule.

The diagram chains three tiers, with illustrative group names. Only the load balancer, a server that spreads requests across the app servers (lesson 14), faces the internet; each later tier accepts traffic only from the group in front of it.

Illustrative security group chain. The internet, where 0.0.0.0/0 means anyone, sends port 443 to a load balancer in a public subnet whose group lb-sg allows TCP 443 from 0.0.0.0/0. The load balancer sends port 8080 to app servers in a private subnet whose group app-sg allows TCP 8080 from lb-sg only. The app servers send port 5432 to a database in a private subnet whose group db-sg allows TCP 5432 from app-sg only. A dashed line from the internet straight to the database's port 5432 is crossed out: not listed, so dropped. A note says each tier admits only the group in front of it, and replies flow back automatically because security groups are stateful
Each tier admits only the group in front of it, so nothing on the internet reaches the database port.

The database rule names app-sg, not the app subnet's range: a range would also admit anything placed in that subnet later, while the group admits only the app servers, however many there are.

Timeout or refused: what the error tells you

Lesson 2 showed that "connection refused" means the machine answered but nothing listens on that port, while a timeout means nothing answered. Firewalls cause most of those silences.

A firewall can drop a packet, discarding it silently, or reject it, sending back a refusal. Security groups and network ACLs drop, so a blocked connection looks like a timeout: your side resends its opening SYN packet from lesson 5 a few times, then gives up.

So a timeout points at the path: the security group's rule for that port and your address, the ACL in both directions, the route. A refusal proves your packet reached the machine: check that the program runs on that port and listens on an address other than 127.0.0.1.

It is a rule of thumb, because some firewalls reject: then a blocked port may look refused or report "No route to host". If a program clearly listens and you are still refused, check the machine's own firewall.

Check yourself

A security group allows SSH only from the office range 203.0.113.0/24. From home, SSH hangs, then times out. Is the server down?

Probably not. Your home address is outside that range, so the security group drops your packets silently, which shows up as a timeout. Connect from the office or add a /32 rule for your home address.

A web server's security group allows inbound TCP 443 from anywhere, and someone deletes all its outbound rules. Can visitors still load pages?

Yes. Security groups are stateful, so replies to an allowed inbound connection go out automatically. Only connections the server starts itself, such as downloading updates, break.

A subnet's network ACL allows only TCP 443, inbound and outbound. The security group is correct, yet pages never load. Why?

The ACL is stateless, so it checks the server's replies too. They go to each visitor's temporary port, not 443, and no outbound rule allows that; allowing outbound 1024–65535 fixes it.