How to Configure Firewall on Raspberry Pi
In this article
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
- Open a terminal on your Raspberry Pi. If you’re using SSH, connect to your Pi’s IP address. The default username is
pi(orubuntuif you’re using Ubuntu Server). - Check if UFW is installed by running
sudo ufw status. If you seeStatus: inactive, it’s installed. If you getcommand not found, install it withsudo apt update && sudo apt install ufw -y. - Set default policies to deny incoming traffic and allow outgoing traffic. This is the safest baseline. Run:
sudo ufw default deny incomingsudo ufw default allow outgoing - 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. - Allow other essential services based on what you’re using the Pi for. Common examples:
- Web server (HTTP):
sudo ufw allow httporsudo ufw allow 80/tcp - HTTPS:
sudo ufw allow httpsorsudo 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
- Web server (HTTP):
- Enable the firewall by running
sudo ufw enable. You’ll be prompted to confirm—typeyand press Enter. This activates the rules immediately. - Check the status with
sudo ufw status verbose. You should see the default policies and your allowed services listed. For example:Status: activeTo Action From-- ------ ----22/tcp ALLOW Anywhere80/tcp ALLOW Anywhere - Test your configuration by trying to connect to the Pi from another device. For example, if you allowed SSH, try
ssh pi@your-pi-ipfrom another computer. If you allowed HTTP, open a browser and navigate to your Pi’s IP address. - Make changes if needed. To add a new rule later, use
sudo ufw allow [service]. To remove a rule, usesudo ufw delete allow [service](e.g.,sudo ufw delete allow 80/tcp). - 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/tcpfor SSH is correct, but allowing22/udpis unnecessary and could open a security hole. - Not checking the status after enabling—always verify with
sudo ufw statusto 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 withallow outgoingunless you have a specific reason.
Tips
- Use service names instead of port numbers where possible (e.g.,
sudo ufw allow sshinstead ofsudo ufw allow 22/tcp). UFW reads from/etc/servicesand 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.rulesand/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 sshandsudo ufw reload. - Services stop working: Check the status with
sudo ufw statusto see if the required port is allowed. If not, add it withsudo ufw allow [port]and reload withsudo ufw reload. - UFW won’t enable: Run
sudo ufw statusto see any error messages. A common issue is a conflicting iptables rule—trysudo ufw resetto 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 outgoingand reload. - IPv6 issues: If you’re having trouble with IPv6, ensure it’s enabled in UFW by checking
/etc/default/ufwand confirmingIPV6=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.