Shopping Cart
0.00

No products in the cart.

Whether you’re using your Raspberry Pi as a server, media center, or smart home hub, it’s important to protect it from unauthorized access. Setting up a firewall is one of the easiest and most effective ways to secure your Pi.

This guide walks you through setting up a basic firewall on Raspberry Pi using UFW (Uncomplicated Firewall) or iptables.

🛡 Why Use a Firewall on Raspberry Pi?

  • Block unwanted traffic
  • Allow only specific ports
  • Prevent unauthorized access to services
  • Strengthen your Raspberry Pi’s security posture

🧰 What You’ll Need

  • Raspberry Pi running Raspberry Pi OS
  • Terminal or SSH access
  • Basic Linux knowledge

⚙️ Method 1: Using UFW (Uncomplicated Firewall)

Step 1: Install UFW

sudo apt update
sudo apt install ufw

Step 2: Set Default Rules

sudo ufw default deny incoming
sudo ufw default allow outgoing

Step 3: Allow SSH (important!)

sudo ufw allow ssh

Step 4: Enable UFW

sudo ufw enable

Step 5: Check Status

sudo ufw status verbose

⚙️ Method 2: Using iptables

iptables gives you more control but requires deeper networking knowledge. Here’s a basic example:

Step 1: Drop All Incoming by Default

sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

Step 2: Allow Loopback and SSH

sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Step 3: Save the Rules

sudo apt install iptables-persistent
sudo netfilter-persistent save

🔁 How to Reset Firewall Rules

To reset UFW:

sudo ufw reset

To flush iptables:

sudo iptables -F

🧠 Pro Tips

  • Always allow SSH before enabling the firewall, or you may lock yourself out
  • Use sudo ufw allow [port] to open access to specific services (like HTTP or VNC)
  • Run a port scan from another machine to check which ports are open

Leave a Reply


Home Shop Cart Account