Bird
Raised Fist0
Linux CLIscripting~20 mins

Firewall basics (ufw, iptables) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firewall Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this UFW status command?
You run the command sudo ufw status verbose on a Linux server with these rules enabled:

- Allow SSH (port 22)
- Deny all incoming by default

What will the output show?
Linux CLI
sudo ufw status verbose
AStatus: inactive
B
Status: active
Logging: on (low)
Default: allow (incoming), allow (outgoing), disabled (routed)

To                         Action      From
--                         ------      ----
22                         DENY IN     Anywhere
C
Status: active
Logging: off
Default: allow (incoming), deny (outgoing), disabled (routed)

To                         Action      From
--                         ------      ----
22                         ALLOW IN    Anywhere
D
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22                         ALLOW IN    Anywhere
Attempts:
2 left
💡 Hint
Think about the default policies and what rules you added.
💻 Command Output
intermediate
1:30remaining
What does this iptables command do?
You run this command:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

What is the effect on the firewall?
Linux CLI
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
AIt blocks all incoming TCP traffic on port 80.
BIt allows incoming TCP traffic on port 80 (HTTP).
CIt allows outgoing TCP traffic on port 80.
DIt deletes existing rules for port 80.
Attempts:
2 left
💡 Hint
Look at the chain, protocol, port, and action.
🔧 Debug
advanced
2:00remaining
Why does this iptables rule cause an error?
You try to run:

sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT

But it returns an error. Why?
Linux CLI
sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
AThe chain 'INPUT' does not exist.
BThe protocol 'tcp' is invalid for port 'ssh'.
CThe port name 'ssh' is not recognized; port numbers must be used.
DThe jump target 'ACCEPT' is misspelled.
Attempts:
2 left
💡 Hint
Check if iptables accepts service names or only numbers for ports.
🚀 Application
advanced
2:30remaining
Create a UFW rule to allow HTTPS only from a specific IP
You want to allow incoming HTTPS (port 443) traffic only from IP 192.168.1.100 using UFW.

Which command achieves this?
Asudo ufw allow from 192.168.1.100 to any port 443 proto tcp
Bsudo ufw allow 443 from 192.168.1.100
Csudo ufw allow to 192.168.1.100 port 443
Dsudo ufw deny from 192.168.1.100 to any port 443
Attempts:
2 left
💡 Hint
Think about specifying source IP, destination port, and protocol.
🧠 Conceptual
expert
3:00remaining
What is the effect of this iptables command sequence?
Consider these commands run in order:

1. sudo iptables -P INPUT DROP
2. sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
3. sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

What is the overall effect on incoming traffic?
AAll incoming traffic is blocked except established connections and new SSH connections on port 22.
BAll incoming traffic is allowed including SSH and established connections.
CAll incoming traffic is blocked including SSH and established connections.
DOnly SSH traffic is allowed; all other traffic is blocked.
Attempts:
2 left
💡 Hint
Look at the default policy and the rules allowing specific traffic.

Practice

(1/5)
1. What is the primary purpose of a firewall like ufw or iptables on a Linux system?
easy
A. To update the Linux kernel automatically
B. To speed up the internet connection
C. To manage user accounts and passwords
D. To control network access and protect the system from unauthorized connections

Solution

  1. Step 1: Understand firewall function

    A firewall controls network traffic to protect the system from unwanted access.
  2. 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.
  3. Final Answer:

    To control network access and protect the system from unauthorized connections -> Option D
  4. Quick Check:

    Firewall = network protection [OK]
Hint: Firewalls block or allow network traffic to protect your system [OK]
Common Mistakes:
  • Confusing firewall with system update tools
  • Thinking firewall manages user accounts
  • Assuming firewall speeds up internet
2. Which of the following is the correct command to enable the ufw firewall?
easy
A. ufw enable
B. ufw start
C. ufw on
D. ufw activate

Solution

  1. Step 1: Recall ufw command to enable firewall

    The correct command to enable ufw is ufw enable.
  2. Step 2: Check other options

    Commands like ufw start, ufw on, and ufw activate are invalid.
  3. Final Answer:

    ufw enable -> Option A
  4. Quick Check:

    Enable ufw = ufw enable [OK]
Hint: Remember: 'enable' starts ufw, not 'start' or 'on' [OK]
Common Mistakes:
  • Using 'ufw start' instead of 'ufw enable'
  • Typing 'ufw on' which is not a valid command
  • Confusing with systemctl commands
3. What will be the output of the command sudo ufw status after running sudo ufw allow 22 and sudo ufw enable on a fresh system?
medium
A. Status: active\nTo Action From\n-- ------ ----\n80 ALLOW Anywhere
B. Status: active\nTo Action From\n-- ------ ----\n22 ALLOW Anywhere
C. Status: active\nNo rules configured
D. Status: inactive\nNo rules configured

Solution

  1. Step 1: Understand commands run

    sudo ufw allow 22 opens port 22 (SSH). sudo ufw enable activates the firewall.
  2. Step 2: Predict ufw status output

    The status will be active and show port 22 allowed from anywhere.
  3. Final Answer:

    Status: active\nTo Action From\n-- ------ ----\n22 ALLOW Anywhere -> Option B
  4. Quick Check:

    Allow 22 + enable = active with port 22 allowed [OK]
Hint: Allow port then enable ufw to see active status with rules [OK]
Common Mistakes:
  • Expecting status inactive after enabling
  • Confusing allowed ports (80 vs 22)
  • Missing the 'active' status line
4. You wrote the command sudo ufw allow sshd but it returns an error. What is the likely cause and how to fix it?
medium
A. The service name 'sshd' is incorrect; use 'ssh' instead
B. You need to run the command without sudo
C. The firewall is disabled; enable it first
D. The port number must be specified instead of service name

Solution

  1. Step 1: Identify the error cause

    The service name for SSH in ufw is 'ssh', not 'sshd'. Using 'sshd' causes an error.
  2. Step 2: Correct the command

    Use sudo ufw allow ssh to allow SSH traffic properly.
  3. Final Answer:

    The service name 'sshd' is incorrect; use 'ssh' instead -> Option A
  4. Quick Check:

    Use correct service names like 'ssh' [OK]
Hint: Use 'ssh' not 'sshd' as ufw service name [OK]
Common Mistakes:
  • Trying without sudo and expecting success
  • Assuming firewall must be enabled before allowing ports
  • Using port numbers when service names are accepted
5. You want to block all incoming traffic except SSH (port 22) and HTTP (port 80) using iptables. Which sequence of commands achieves this correctly?
hard
A. sudo iptables -P OUTPUT DROP\nsudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT\nsudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
B. sudo iptables -P INPUT ACCEPT\nsudo iptables -A INPUT -p tcp --dport 22 -j DROP\nsudo iptables -A INPUT -p tcp --dport 80 -j DROP
C. 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
D. sudo iptables -P INPUT DROP\nsudo iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT\nsudo iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT

Solution

  1. Step 1: Set default policy to DROP incoming traffic

    sudo iptables -P INPUT DROP blocks all incoming by default.
  2. Step 2: Allow incoming SSH and HTTP ports

    Commands to accept TCP traffic on ports 22 and 80 allow SSH and HTTP.
  3. Step 3: Allow established connections

    sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT lets responses through.
  4. 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 C
  5. Quick Check:

    Default DROP + allow ports 22,80 + established accept = correct [OK]
Hint: Set default DROP, then allow needed ports and established states [OK]
Common Mistakes:
  • Setting OUTPUT policy instead of INPUT
  • Dropping allowed ports instead of accepting
  • Forgetting to allow established connections
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard