Using the UFW Firewall on the Raspberry Pi?

The Raspberry Pi is a tiny, affordable computer that provides endless possibilities for DIY computing projects. However, like any computer connected to the internet, it’s important to configure the Raspberry Pi to be secure. One of the best ways to secure a Raspberry Pi is to setup a firewall.

Using the UFW Firewall on the Raspberry Pi?

The Uncomplicated Firewall (UFW) is an easy to use firewall that comes pre-installed on Raspbian, the official operating system for Raspberry Pi. UFW provides a simple interface for managing a firewall on Raspberry Pi to control incoming and outgoing network connections.

A firewall acts as a protective barrier between your Raspberry Pi and the internet. It controls what traffic is allowed in and out of the device.

Firewalls work by setting rules that define what types of network connections should be allowed or blocked. For example, you can configure UFW to:

  • Allow SSH connections so you can remotely access your Pi
  • Block incoming HTTP requests to prevent web attacks
  • Allow ping requests for troubleshooting connectivity issues

Using a firewall helps protect your Raspberry Pi against:

  • Incoming attacks: A firewall blocks malicious traffic and abuse like DDoS attacks.
  • Unauthorized access: A firewall only allows connections to services you explicitly allow, preventing unwanted access.
  • Data exposure: A firewall prevents confidential data from leaving your Pi through an unsecured app or port.

In short, a properly configured firewall enhances the security and control of your Raspberry Pi.

Installing and Configuring UFW

UFW comes preinstalled on the Raspbian operating system. To get started, first check if UFW is installed:

sudo ufw status

If UFW is not already installed, you can install it with:

sudo apt install ufw

Once installed, basic UFW configuration only takes a few commands.

First, enable UFW:

sudo ufw enable

This turns on the firewall. At this point, all incoming and outgoing connections are blocked by default.

Next, allow SSH so you can connect remotely:

sudo ufw allow ssh

You can also allow other common services like HTTP web traffic:

sudo ufw allow http

sudo ufw allow https

Finally, check the status to see configured rules:

sudo ufw status

This displays firewall rules like:

Status: active

To                         Action      From

—                         ——      —-

22/tcp                     ALLOW       Anywhere                  

80/tcp                     ALLOW       Anywhere                  

443/tcp                    ALLOW       Anywhere                  

22/tcp (v6)                ALLOW       Anywhere (v6)             

80/tcp (v6)                ALLOW       Anywhere (v6)

The firewall is now setup to allow remote SSH access and local HTTP/HTTPS web services while blocking everything else.

Key Advantages of Using UFW

UFW simplifies Linux firewall management with these main features:

Simple Command Syntax

UFW makes it easy to manage firewall rules using simple commands like:

ufw allow port

ufw deny ip

ufw delete rule

You don’t have to memorize complex iptables syntax. UFW handles interacting with iptables behind the scenes.

Preconfigured Profiles

UFW comes with profiles that automatically set common rules for services like SSH or HTTP servers. This simplifies opening up specific services.

For example, ufw allow ssh automatically opens port 22 with optimized firewall rules for SSH traffic.

Easy Rule Management

UFW stores rules in a format that lets you easily review, update, and delete rules. You can see all active rules with ufw status.

This makes it simple to audit your firewall and adjust rules if needed without disrupting existing connections.

IPv6 Support

UFW has full support for IPv6 networks. You can create firewall rules optimized for IPv6 traffic alongside IPv4 rules.

Built-in Logging

UFW can log activity to syslog or a file. This allows you to monitor all firewall traffic for any suspicious activity or attempted attacks.

Common UFW Firewall Rules

Here are some common examples of configuring UFW firewall rules:

Allow SSH

To allow incoming SSH connections:

sudo ufw allow ssh

This opens TCP port 22. You can also allow SSH on a non-standard high port:

sudo ufw allow 6000/tcp

Open a Port

To allow traffic on a specific TCP or UDP port:

sudo ufw allow 8080/tcp

sudo ufw allow 53/udp

Allow a Service

To allow a preconfigured service like HTTP:

sudo ufw allow http

sudo ufw allow https

Allow From an IP or Subnet

To expose a service only to a specific IP address or subnet:

sudo ufw allow from to any port 22

sudo ufw allow from to any port 80

Deny Access

To explicitly block traffic:

sudo ufw deny smtp

sudo ufw deny from

Delete a Rule

To remove an existing allow or deny rule:

sudo ufw delete allow 80/tcp

sudo ufw delete deny smtp

Securing Public Raspberry Pi Servers

For Raspberry Pis that are publicly accessible on the internet, extra security steps should be taken:

  • Change default passwords: Set strong, unique passwords for user accounts.
  • Enable SSH key login: Require SSH public key authentication instead of password login.
  • Disable remote root login: Prevent direct root logins via SSH for accountability.
  • Remove unnecessary services: Delete unused open ports and apps to reduce attack surface.
  • Setup fail2ban: Install fail2ban to ban IPs that attempt too many failed login attempts.
  • Monitor logs: Check UFW and system logs regularly for signs of attacks.
  • Update regularly: Apply security patches by updating the system frequently.
  • Limit exposed services: Only open the minimum ports and services required. Allow access from trusted IPs only if possible.

Properly hardening and configuring the firewall is a key part of securing any internet-connected Raspberry Pi.

Troubleshooting UFW Issues

Here are some common troubleshooting steps if you run into problems with UFW:

  • Check status: Run sudo ufw status to verify configured rules.
  • Check other firewalls: Tools like iptables could conflict with UFW, disable them.
  • Allow ping: Temporarily allow ICMP ping with ufw allow ping for connectivity tests.
  • Check routes: Ensure your Raspberry Pi has correct routes and connectivity to allowed IPs.
  • Review logs: Check UFW logs in /var/log/ufw.log for specific denied traffic.
  • Disable and reenable: Stop UFW with ufw disable, fix issues, then reenable UFW.
  • Reset all rules: Run ufw reset to delete all rules and start over from scratch.
  • Consult docs: The man pages (man ufw) have extensive documentation if issues persist.


UFW provides an uncomplicated way to securely lock down your Raspberry Pi. With minimal commands, you can setup a basic firewall that blocks unsafe access while allowing needed services like SSH.

Configuring the firewall is an essential first step for any Raspberry Pi deployment. UFW gives you simple yet powerful options to control network access without complex iptables commands.

As you continue to expand your Raspberry Pi projects, follow security best practices and consult UFW’s documentation to tailor firewall rules that meet your specific needs. With UFW, you can easily control access and harden your Raspberry Pi systems against attacks.

Key Takeaways:

  • UFW provides simple interface for managing Linux firewall on Raspberry Pi
  • UFW makes it easy to allow or deny access to services and ports
  • Preconfigured profiles simplify enabling common services like SSH
  • UFW stores rules in a readable format for easy management
  • Use UFW to lock down Raspberry Pis accessible on the public internet
  • Troubleshoot issues by checking status, logs, and reviewing documentation

Frequently Asked Questions:

Q: What is the default policy of UFW?
A: The default policy is to deny all incoming connections and allow all outgoing connections.

Q: Does UFW work with Raspbian Lite?
A: Yes, UFW can be used on any Raspbian installation, including headless Raspbian Lite.

Q: How do I allow ping requests through UFW?
A: Use the command sudo ufw allow ping to allow ICMP ping requests.

Q: Can I use UFW on other Linux distributions?
A: UFW is available in Ubuntu, Debian, Linux Mint, and other Debian-based distros. It may act differently or have limited functionality compared to Raspbian.

Q: Should I use UFW or iptables directly
A: UFW is generally simpler for basic firewall needs. For complex rules, you can directly use iptables commands which UFW is based on.

Q: Does UFW monitor traffic for threats?
A: No, UFW is just a firewall to block/allow connections. You need additional software like Snort or Suricata to analyze traffic for intrusions.

Q: Why is a service not available after allowing it in UFW?
A: Make sure the service is actually installed and running. UFW just exposes allowed ports, it doesn’t start services.

Q: How do I open a range of ports in UFW?
A: Use ufw allow 6000:60010/tcp to allow a range of ports from 6000 to 60010.

Q: Can I set different rules for IPv4 and IPv6?
A: Yes, UFW fully supports creating IPv6 specific rules like ufw allow 80/tcp ipv6.

Q: Does UFW work with VPNs like OpenVPN?
A: Yes, you can configure UFW rules to allow VPN traffic on the relevant ports and protocols.

Q: Where are UFW logs located?
A: UFW stores logs in /var/log/ufw.log. You can view them with sudo cat /var/log/ufw.log.

Q: What is the difference between ufw allow and ufw allow 22/tcp
A: ufw allow 22/tcp allows port 22 with tcp protocol. ufw allow 22 allows all protocols on port 22 including tcp/udp.

Q: How do I disable the firewall completely?
A: Run sudo ufw disable to disable the firewall. To reenable it use sudo ufw enable.

Q: Can I create a UFW rule to redirect a port?
A: No, UFW does not support port redirection. You would need to use iptables for that level of control.

Q: Does UFW work on Windows?
A: No, UFW is designed for Linux. Windows has its own firewall software like Windows Defender Firewall.

Q: What is the command to allow all ports?
A: sudo ufw default allow will allow all incoming ports. This is very insecure and should be avoided.

Q: Can I manage UFW remotely via SSH?
A: Yes, UFW can be entirely managed over SSH if you allow SSH connections in the firewall.

Q: What is the best way to open just a single port in UFW?
A: Use ufw allow [port]/[protocol] like ufw allow 3306/tcp to securely expose only a single port.

Q: How do I allow access to a subnet range?
A: Use ufw allow from to allow a subnet in CIDR notation. Replace with your desired subnet.

Q: What is the difference between ufw reset and ufw reload?
A: ufw reset clears all existing rules. ufw reload reloads rules from your UFW config files.

Leave a Comment