Raspberry Pi Firewall Setup Guide
In this article
Setting up a home firewall on a Raspberry Pi turns a small, low-power computer into a dedicated network security appliance. It sits between your modem and your router (or acts as the router itself) and filters incoming and outgoing traffic based on rules you define. This guide walks you through installing and configuring a firewall using UFW (Uncomplicated Firewall) on Raspberry Pi OS, which is the easiest and most reliable method. You will need a Raspberry Pi (any model from Pi 2 onward works), a microSD card with Raspberry Pi OS installed, and a way to connect to it (keyboard and monitor, or SSH).
What You'll Need
- A Raspberry Pi (Pi 2, 3, 4, or 5) with Raspberry Pi OS (32-bit or 64-bit) installed and updated.
- Power supply and microSD card.
- Network cable (Ethernet) to connect the Pi to your modem or router.
- Optional: USB Wi-Fi adapter if you want the Pi to act as a wireless access point (not covered here).
- Access to the terminal (either directly or via SSH).
Step-by-Step
- Update your system. Open a terminal and run:
sudo apt update && sudo apt upgrade -y
This ensures you have the latest security patches and software versions. - Install UFW. UFW is included by default on Raspberry Pi OS, but if it's not present, install it with:
sudo apt install ufw -y
- Check your current network configuration. Run
ip addrto see your network interfaces. Note the name of your main interface (usuallyeth0for Ethernet orwlan0for Wi-Fi). This is the interface you'll apply rules to. - Set default policies. By default, UFW denies all incoming traffic and allows all outgoing. To set this explicitly:
sudo ufw default deny incoming
sudo ufw default allow outgoing
This is a secure baseline—only traffic you explicitly allow will get in. - Allow SSH (if you need remote access). If you're managing the Pi remotely, allow SSH before enabling the firewall to avoid locking yourself out:
sudo ufw allow ssh
This opens port 22. If you use a different SSH port, replacesshwith that port number (e.g.,sudo ufw allow 2222/tcp). - Allow other essential services. If you want the Pi to act as a router or firewall for your home network, you'll need to allow forwarding. Edit the UFW configuration file:
sudo nano /etc/default/ufw
Find the lineDEFAULT_FORWARD_POLICY="DROP"and change it toDEFAULT_FORWARD_POLICY="ACCEPT". Save and exit (Ctrl+X, then Y, then Enter). This allows the Pi to forward traffic between your LAN and the internet. - Enable IP forwarding (if using as a router). Edit the sysctl file:
sudo nano /etc/sysctl.conf
Uncomment the linenet.ipv4.ip_forward=1(remove the #). Save and exit. Then apply it with:sudo sysctl -p
- Enable UFW. Now enable the firewall:
sudo ufw enable
You'll be prompted to confirm—typeyand press Enter. UFW will start and apply your rules. - Check the status. Verify that UFW is active and see your rules:
sudo ufw status verbose
You should seeStatus: activeand a list of allowed services. - Test your configuration. From another device on your network, try to access the Pi (e.g., SSH if you allowed it). Also try accessing the internet from a device behind the Pi if you set it up as a router. If everything works, your firewall is live.
- Make UFW start on boot. UFW is a systemd service and should start automatically. Check with:
sudo systemctl enable ufw
(It's usually already enabled, but running this ensures it.)
Common Mistakes
- Enabling UFW before allowing SSH – This locks you out of remote access. Always allow SSH first.
- Forgetting to change the default forward policy – If you're using the Pi as a router, leaving the forward policy as DROP will block all traffic between your LAN and the internet.
- Not enabling IP forwarding – Without
net.ipv4.ip_forward=1, the Pi won't route packets between interfaces. - Using the wrong interface name – Applying rules to
eth0when your Pi useswlan0(or vice versa) will cause rules to not apply as expected. Always check withip addr. - Blocking all outgoing traffic – If you set the outgoing policy to deny, you'll break DNS, DHCP, and updates. Stick with allowing outgoing.
Tips
- Use
sudo ufw allow from 192.168.1.0/24to allow traffic from a specific local subnet (adjust the IP range to match your network). - For a home setup, you usually only need to allow SSH (port 22) and perhaps HTTP/HTTPS (ports 80 and 443) if you're hosting a web server.
- Check your router's DHCP settings to assign a static IP to your Raspberry Pi so its address never changes.
- Keep your Pi updated regularly with
sudo apt update && sudo apt upgradeto patch security vulnerabilities. - If you want to monitor blocked traffic, use
sudo ufw logging onand check/var/log/ufw.log.
Troubleshooting
If you get locked out of SSH: Connect a monitor and keyboard to the Pi, log in locally, and run sudo ufw allow ssh then sudo ufw reload.
If the Pi doesn't forward traffic: Double-check that net.ipv4.ip_forward is set to 1 (sysctl net.ipv4.ip_forward) and that the forward policy in /etc/default/ufw is ACCEPT. Also ensure your router's DHCP is handing out the Pi's IP as the gateway (or manually set clients to use it).
If UFW won't enable: Run sudo ufw status to see any error messages. Often it's a syntax error in a rule. Review your rules with sudo ufw show added and remove any that look wrong.
If you can't reach the internet after enabling: Make sure you didn't accidentally set the outgoing policy to deny. Also check that DNS (port 53) is allowed—UFW's default allow outgoing covers it, but if you changed that, add sudo ufw allow out 53.
If the Pi's firewall works but your other devices can't connect: Ensure the Pi is actually the gateway for those devices. On the client device, check its default gateway—it should point to the Pi's IP address.
Frequently Asked Questions
Can I use the Raspberry Pi as a full router with DHCP?
Yes, you can install and configure dnsmasq or similar to provide DHCP and DNS services, but that's beyond the scope of this firewall guide. UFW only handles packet filtering.
Does UFW protect against all cyber threats?
No. UFW filters network traffic based on rules, but it doesn't detect malware or block phishing. It's a first layer of defense, not a complete security solution.
Can I run a firewall on a Raspberry Pi Zero?
Technically yes, but the Pi Zero's single-core CPU and limited RAM may bottleneck network throughput. For gigabit internet, use a Pi 4 or 5.
How do I add a rule to allow a specific port?
Use sudo ufw allow 8080/tcp to allow TCP traffic on port 8080. Replace tcp with udp if needed.
How do I remove a rule?
Use sudo ufw delete allow 8080/tcp or find the rule number with sudo ufw status numbered and then sudo ufw delete NUMBER.
Will this firewall slow down my internet?
On a Pi 4 or 5, UFW adds negligible latency for typical home use (under 100 Mbps). On older models, you might see a slight reduction on high-bandwidth connections.