Firewall basics (ufw, iptables) in Linux CLI - Time & Space Complexity
When using firewall commands like ufw or iptables, it's important to understand how the time to process rules grows as you add more rules.
We want to know how the firewall's work changes when the number of rules increases.
Analyze the time complexity of checking a packet against firewall rules.
sudo iptables -L INPUT -v --line-numbers
sudo ufw status numbered
# Imagine a packet arriving and the firewall checking rules one by one
# until it finds a match or reaches the end.
This shows how firewall rules are listed and how a packet is checked against them in order.
Look at what repeats when a packet is checked.
- Primary operation: Checking each rule one by one in order.
- How many times: Up to the total number of rules in the firewall.
As you add more rules, the firewall may need to check more rules before deciding what to do with a packet.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 rules | Up to 10 checks per packet |
| 100 rules | Up to 100 checks per packet |
| 1000 rules | Up to 1000 checks per packet |
Pattern observation: The number of checks grows directly with the number of rules.
Time Complexity: O(n)
This means the time to check a packet grows linearly with the number of firewall rules.
[X] Wrong: "Adding more rules won't affect how fast the firewall works."
[OK] Correct: Each packet may need to be checked against many rules, so more rules usually mean more work and slower processing.
Understanding how firewall rules affect performance helps you design better systems and troubleshoot network issues confidently.
"What if the firewall used a hash or tree structure to check rules instead of checking them one by one? How would the time complexity change?"
Practice
ufw or iptables on a Linux system?Solution
Step 1: Understand firewall function
A firewall controls network traffic to protect the system from unwanted access.Step 2: Identify the correct purpose
Options B, C, and D describe unrelated system tasks, so only To control network access and protect the system from unauthorized connections fits.Final Answer:
To control network access and protect the system from unauthorized connections -> Option DQuick Check:
Firewall = network protection [OK]
- Confusing firewall with system update tools
- Thinking firewall manages user accounts
- Assuming firewall speeds up internet
ufw firewall?Solution
Step 1: Recall ufw command to enable firewall
The correct command to enable ufw isufw enable.Step 2: Check other options
Commands likeufw start,ufw on, andufw activateare invalid.Final Answer:
ufw enable -> Option AQuick Check:
Enable ufw = ufw enable [OK]
- Using 'ufw start' instead of 'ufw enable'
- Typing 'ufw on' which is not a valid command
- Confusing with systemctl commands
sudo ufw status after running sudo ufw allow 22 and sudo ufw enable on a fresh system?Solution
Step 1: Understand commands run
sudo ufw allow 22opens port 22 (SSH).sudo ufw enableactivates the firewall.Step 2: Predict ufw status output
The status will be active and show port 22 allowed from anywhere.Final Answer:
Status: active\nTo Action From\n-- ------ ----\n22 ALLOW Anywhere -> Option BQuick Check:
Allow 22 + enable = active with port 22 allowed [OK]
- Expecting status inactive after enabling
- Confusing allowed ports (80 vs 22)
- Missing the 'active' status line
sudo ufw allow sshd but it returns an error. What is the likely cause and how to fix it?Solution
Step 1: Identify the error cause
The service name for SSH in ufw is 'ssh', not 'sshd'. Using 'sshd' causes an error.Step 2: Correct the command
Usesudo ufw allow sshto allow SSH traffic properly.Final Answer:
The service name 'sshd' is incorrect; use 'ssh' instead -> Option AQuick Check:
Use correct service names like 'ssh' [OK]
- Trying without sudo and expecting success
- Assuming firewall must be enabled before allowing ports
- Using port numbers when service names are accepted
iptables. Which sequence of commands achieves this correctly?Solution
Step 1: Set default policy to DROP incoming traffic
sudo iptables -P INPUT DROPblocks all incoming by default.Step 2: Allow incoming SSH and HTTP ports
Commands to accept TCP traffic on ports 22 and 80 allow SSH and HTTP.Step 3: Allow established connections
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTlets responses through.Final Answer:
sudo iptables -P INPUT DROP\nsudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT\nsudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT\nsudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -> Option CQuick Check:
Default DROP + allow ports 22,80 + established accept = correct [OK]
- Setting OUTPUT policy instead of INPUT
- Dropping allowed ports instead of accepting
- Forgetting to allow established connections
Ready to prove it?
Pass the test and this pattern moves unknown
