What if one simple command could guard your entire network like a vigilant security guard?
Why Firewall basics (ufw, iptables) in Linux CLI? - Purpose & Use Cases
Imagine you have a small office network with many devices. You want to keep unwanted visitors out and only allow trusted connections. Without a firewall, you have to check every device manually and hope no one sneaks in.
Manually monitoring network traffic is like watching every door and window all day without any alarm. It's slow, tiring, and easy to miss threats. Mistakes can let hackers in or block important services by accident.
Using firewall tools like ufw or iptables lets you set clear rules once. These tools automatically block or allow traffic based on your rules, working like a smart security guard who never sleeps and never forgets.
Check each connection manually with netstat and guess what to block
sudo ufw allow 22/tcp
sudo ufw enableIt lets you protect your network reliably and easily, so you can focus on your work without worrying about intruders.
A small business uses ufw to only allow web and email traffic, blocking all other ports. This stops hackers from accessing sensitive systems while keeping services running smoothly.
Manual network protection is slow and error-prone.
Firewalls automate traffic control with simple rules.
ufw and iptables help keep networks safe and manageable.
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
