Insights
Raspberry Pi firewall: Set up UFW without losing SSH access
Use UFW on Raspberry Pi OS to deny incoming connections by default, keep private SSH access, and test from approved and excluded clients.

On this page
Start with Uncomplicated Firewall (UFW)Opens in a new tab to protect services on your Raspberry Pi. On Raspberry Pi OS, set UFW to deny incoming connections by default, then allow only the services and clients you need.
In short:
- Use a host firewall. UFW protects services running on the Pi.
- Allow private SSH first. Add a Secure Shell (SSH) rule for your admin device before enabling UFW.
- Test both pathsOpens in a new tab. Try fresh connections from an approved client and an excluded client.
Decide what the Pi needs to protect
Use UFW when the connection ends at the Pi, such as an SSH session you use to manage it. UFW wraps iptablesOpens in a new tab, so you can set firewall policy without writing lower-level packet rules yourself.
Use a gateway firewall when you need to filter traffic between other devices and the internet. The Pi can apply that policy only to traffic routed through it. A host firewall on the Pi won't filter traffic from devices that take another route.
Set up UFW without losing access
This Raspberry Pi OS example assumes your Pi is at 192.168.1.10, your admin device has the stable private address 192.168.1.20, and a second client at 192.168.1.30 should be denied. SSH is the only inbound service you need, and it listens on Transmission Control Protocol (TCP) port 22. Replace the addresses and port with the ones your devices actually use.
1. Install UFW if it isn't already installed.
sudo apt-get update
sudo apt install ufw
2. Check UFW's status before changing the policy.
sudo ufw status
Continue only for a fresh UFW setup. If UFW is already active, review the existing policy and any broader rules that overlap your intended exception instead of following these steps. An inactive UFW status doesn't tell you whether other filtering is in place.
3. Set the default policy to deny incoming connections.
sudo ufw default deny incoming
4. Allow SSH from your admin device's private address.
sudo ufw allow proto tcp from 192.168.1.20 to any port 22
This source-specific ruleOpens in a new tab allows the admin device to reach the SSH port. A port-only allowance doesn't select an approved client, so don't leave a broader SSH allowance in place if you want this restriction to work. Use your actual private subnet, meaning a range of addresses, only if every client in that range should be able to reach SSH. If you need another inbound service, give it its own rule for the required clients, protocol and port.
Before activation, make sure you're authorized to change the Pi's firewall and can sign in at its local console. Keep your current SSH session open, but don't count on it surviving activation. If activation locks you out of this fresh setup, use this fallback at the Pi's local console. It turns off all of UFW, not just the new rule.
sudo ufw disable
5. Enable UFW after the admin rule is in place.
sudo ufw enable
This activates UFW and sets it to start at boot.
Test what gets through
1. Check the active policy on the Pi.
sudo ufw status verbose
Look for an active firewall, a default deny policy for incoming traffic and an SSH allowance limited to your intended source. Check whether logging is active. If you need to turn it on for the blocked-path test, use the low logging levelOpens in a new tab.
2. Open a fresh SSH connection from the approved client.
In your SSH client on 192.168.1.20, connect to 192.168.1.10 on TCP port 22. A successful sign-in shows the admin path works after activation. If it fails, check that SSH is listening on the expected address and port and that the client can route to the Pi before changing firewall rules.
3. Try the same SSH connection from the excluded client.
From 192.168.1.30, use the same destination address, port and protocol. Test with SSH, not ping. If SSH connects, look for a broader allowance or check whether the connection reached the service through a different interface or address family.
A failed SSH attempt alone doesn't show that UFW blocked it. Look in the Pi's kernel logs for a UFW block entry that matches the test time, source address, destination address, protocol and destination port. A match corroborates the block. Without one, the result is inconclusive because log delivery varies by system and low-level logging limits how often it records blocks.
Where doxx.net fits. doxx.net runs a private network on its own infrastructure where people and agents can share private channels. If you'd like to connect a supported device or agent privately, get doxx.netOpens in a new tab and keep UFW in charge of the Pi's local firewall rules.