📖 Scenario: You are managing a small Linux server that needs basic firewall protection. You want to allow only certain types of network traffic and block everything else to keep your server safe.
🎯 Goal: Learn how to set up simple firewall rules using ufw and iptables commands to allow SSH and HTTP traffic and block other connections.
📋 What You'll Learn
Create a basic firewall rule set using ufw
Create a basic firewall rule set using iptables
Allow SSH (port 22) and HTTP (port 80) traffic
Block all other incoming traffic
Display the active firewall rules
💡 Why This Matters
🌍 Real World
Setting up firewall rules is essential for protecting servers from unauthorized access and attacks. This project shows how to configure basic firewall rules using common Linux tools.
💼 Career
System administrators and DevOps engineers regularly configure firewalls to secure servers and networks. Knowing ufw and iptables basics is a key skill for these roles.
Progress0 / 4 steps
1
Set up initial ufw firewall rules
Create a variable called ufw_rules that contains the commands to allow SSH on port 22 and HTTP on port 80 using ufw. Also include the command to enable ufw with default deny incoming policy.
Linux CLI
Hint
Use a list to store the exact ufw commands as strings.
2
Set up initial iptables firewall rules
Create a variable called iptables_rules that contains the commands to set default policy to DROP for INPUT chain, allow incoming SSH on port 22, allow incoming HTTP on port 80, and allow established connections using iptables commands.
Linux CLI
Hint
Use a list to store the exact iptables commands as strings.
3
Write a function to apply firewall rules
Write a function called apply_rules that takes a list of commands as input and prints each command prefixed with Executing:. Use a for loop with variable cmd to iterate over the list commands.
Linux CLI
Hint
Use a for loop to print each command with the prefix Executing:.
4
Display the firewall rules being applied
Call the function apply_rules twice: first with the variable ufw_rules, then with the variable iptables_rules. This will print all the commands to be executed.
Linux CLI
Hint
Call apply_rules with both ufw_rules and iptables_rules to print all commands.
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
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 D
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
Step 1: Recall ufw command to enable firewall
The correct command to enable ufw is ufw enable.
Step 2: Check other options
Commands like ufw start, ufw on, and ufw activate are invalid.
Final Answer:
ufw enable -> Option A
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
Step 1: Understand commands run
sudo ufw allow 22 opens port 22 (SSH). sudo ufw enable activates the firewall.
Step 2: Predict ufw status output
The status will be active and show port 22 allowed from anywhere.