TechBuzzPro

How to Configure Firewall on Raspberry Pi

· 5 min read · Security & Privacy

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To configure a firewall on a Raspberry Pi, the simplest and most reliable method is to use UFW (Uncomplicated Firewall), which is included in the default Raspberry Pi OS. UFW gives you a straightforward command-line interface to allow or block traffic, and it’s perfect for beginners. This guide walks you through enabling UFW, setting default policies, allowing essential services like SSH and HTTP, and testing your configuration. You’ll need a Raspberry Pi running Raspberry Pi OS (or any Debian-based Linux), an internet connection, and terminal access (either directly or via SSH).

What You'll Need

  • A Raspberry Pi with Raspberry Pi OS (or any Debian/Ubuntu-based Linux distribution) installed and booted.
  • Access to the terminal (either a monitor and keyboard, or SSH).
  • An internet connection to install packages if needed (though UFW is usually pre-installed).
  • Basic familiarity with the command line.

Step-by-Step

  1. Open a terminal on your Raspberry Pi. If you’re using SSH, connect to your Pi’s IP address. The default username is pi (or ubuntu if you’re using Ubuntu Server).
  2. Check if UFW is installed by running sudo ufw status. If you see Status: inactive, it’s installed. If you get command not found, install it with sudo apt update && sudo apt install ufw -y.
  3. Set default policies to deny incoming traffic and allow outgoing traffic. This is the safest baseline. Run:
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
  4. Allow SSH (if you need remote access). This is critical if you’re connected via SSH—otherwise you’ll lock yourself out. Run:
    sudo ufw allow ssh
    or specify the port: sudo ufw allow 22/tcp.
  5. Allow other essential services based on what you’re using the Pi for. Common examples:
    • Web server (HTTP): sudo ufw allow http or sudo ufw allow 80/tcp
    • HTTPS: sudo ufw allow https or sudo ufw allow 443/tcp
    • VNC (if you use remote desktop): sudo ufw allow vnc (usually port 5900)
    • Custom port (e.g., 8080): sudo ufw allow 8080/tcp
  6. Enable the firewall by running sudo ufw enable. You’ll be prompted to confirm—type y and press Enter. This activates the rules immediately.
  7. Check the status with sudo ufw status verbose. You should see the default policies and your allowed services listed. For example:
    Status: active
    To Action From
    -- ------ ----
    22/tcp ALLOW Anywhere
    80/tcp ALLOW Anywhere
  8. Test your configuration by trying to connect to the Pi from another device. For example, if you allowed SSH, try ssh pi@your-pi-ip from another computer. If you allowed HTTP, open a browser and navigate to your Pi’s IP address.
  9. Make changes if needed. To add a new rule later, use sudo ufw allow [service]. To remove a rule, use sudo ufw delete allow [service] (e.g., sudo ufw delete allow 80/tcp).
  10. Disable the firewall if you ever need to troubleshoot or start over: sudo ufw disable. This will remove all rules and stop the firewall.

Common Mistakes

  • Enabling UFW before allowing SSH—this will lock you out of remote access. Always allow SSH first if you need it.
  • Forgetting to set default policies—without them, UFW may behave unpredictably. Always set defaults first.
  • Using the wrong port numbers—for example, allowing 22/tcp for SSH is correct, but allowing 22/udp is unnecessary and could open a security hole.
  • Not checking the status after enabling—always verify with sudo ufw status to ensure your rules are active.
  • Blocking all outgoing traffic—if you set default deny outgoing, you’ll break DNS, updates, and most internet access. Stick with allow outgoing unless you have a specific reason.

Tips

  • Use service names instead of port numbers where possible (e.g., sudo ufw allow ssh instead of sudo ufw allow 22/tcp). UFW reads from /etc/services and is less error-prone.
  • Be specific with IP addresses if you want to restrict access. For example, to allow SSH only from your home IP: sudo ufw allow from 192.168.1.100 to any port 22.
  • Log firewall activity to help debug: sudo ufw logging on. Logs are written to /var/log/ufw.log.
  • Test from a separate device after enabling—don’t rely solely on the Pi itself to test connectivity.
  • Keep a backup of your rules by copying /etc/ufw/user.rules and /etc/ufw/user6.rules (for IPv6) to a safe location.

Troubleshooting

If something goes wrong after enabling the firewall, here’s what to do:

  • You’ve locked yourself out of SSH: If you enabled UFW before allowing SSH, you can’t connect remotely. You’ll need physical access to the Pi (keyboard and monitor). Log in locally, then run sudo ufw allow ssh and sudo ufw reload.
  • Services stop working: Check the status with sudo ufw status to see if the required port is allowed. If not, add it with sudo ufw allow [port] and reload with sudo ufw reload.
  • UFW won’t enable: Run sudo ufw status to see any error messages. A common issue is a conflicting iptables rule—try sudo ufw reset to clear all rules and start fresh.
  • Can’t reach the internet after enabling: Check if you accidentally set default deny outgoing. If so, reset defaults: sudo ufw default allow outgoing and reload.
  • IPv6 issues: If you’re having trouble with IPv6, ensure it’s enabled in UFW by checking /etc/default/ufw and confirming IPV6=yes.

Frequently Asked Questions

Is UFW the only firewall option for Raspberry Pi?

No. You can also use iptables directly, or front-ends like firewalld. However, UFW is the easiest for most users and is pre-installed on Raspberry Pi OS.

Will enabling UFW slow down my Pi?

No. UFW runs in the kernel and has virtually no performance impact on a Raspberry Pi, even on older models.

How do I allow a specific IP address to access a port?

Use the from syntax: sudo ufw allow from 192.168.1.50 to any port 22. This allows only that IP to connect to SSH.

Can I use UFW to block all incoming traffic except a few ports?

Yes. Set sudo ufw default deny incoming and then allow only the ports you need, like sudo ufw allow 80/tcp.

How do I check if UFW is running after a reboot?

UFW is a system service and will start automatically on boot. To verify, run sudo ufw status—it should say Status: active.

What if I accidentally block all traffic and lose access?

You can reset UFW from a local terminal (keyboard and monitor) by running sudo ufw disable or sudo ufw reset. If you don’t have physical access, you may need to use the recovery console or reinstall the OS.