0
0
Linux CLIscripting~30 mins

Firewall basics (ufw, iptables) in Linux CLI - Mini Project: Build & Apply

Choose your learning style9 modes available
Firewall basics (ufw, iptables)
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Call apply_rules with both ufw_rules and iptables_rules to print all commands.