How to Configure the Firewall in Raspberry Pi

How to Configure the Firewall in Raspberry Pi

March 15, 2024

DALL·E 2024-03-15 17.16.50 - Create a captivating cover image for an article titled 'How to Configure the Firewall in Raspberry Pi'. The cover should visually represent the concep.png__PID:09340b50-f721-4bdf-b766-a9c44c07fe45

Introduction

A firewall is an essential tool in any network, including that of a Raspberry Pi. Essentially, a firewall acts as a security barrier between an internal network (such as that of your Raspberry Pi) and other external networks, such as the Internet. Its main purpose is to control and filter network traffic, allowing or blocking certain connections based on predefined rules.

1.png__PID:7e801ccd-68ce-4609-b40b-50f7212bdf37

Network traffic types

Incoming, outgoing, and forwarded traffic refer to different types of network traffic that can be controlled and managed by the firewall rules.

Incoming Traffic

Incoming traffic refers to data packets that are destined for the Raspberry Pi from external sources, such as requests to access services running on the Raspberry Pi from devices on the internet or other devices on the local network. Examples include requests to access a web server, SSH connections, or any other service running on the Raspberry Pi.

When configuring the firewall, you can define rules to allow or deny incoming traffic based on various criteria, such as the source IP address, destination port, protocol, etc. This helps in protecting the Raspberry Pi from unauthorized access or potential security threats.

Outgoing Traffic

Outgoing traffic refers to data packets that originate from the Raspberry Pi and are destined for external destinations, such as requests made by services running on the Raspberry Pi to access resources on the internet or other devices on the local network. Examples include web requests initiated by a web server running on the Raspberry Pi or software updates fetching data from the internet.

Controlling outgoing traffic is also important for security and privacy reasons. Firewall rules can be configured to allow or deny outgoing traffic based on specific criteria, such as destination IP address, destination port, protocol, etc. This can help in preventing unauthorized communication from the Raspberry Pi to external destinations or controlling access to specific resources.

Forwarded Traffic

Forwarded traffic refers to data packets that are passing through the Raspberry Pi from one network interface to another. This typically occurs when the Raspberry Pi is acting as a router or gateway between different networks, such as between a local network and the internet.

Firewall rules can also be applied to control forwarded traffic, allowing you to define rules to allow or deny the forwarding of packets based on specific criteria, such as source and destination IP addresses, ports, protocols, etc. This helps in controlling the flow of traffic between different network segments and enforcing security policies.

In the context of Raspberry Pi, a firewall can play several important roles:

Home network protection:If you use your Raspberry Pi as a server or device connected to your home network, a firewall can protect devices and data on your network by controlling what traffic can enter or leave it.

Server security: If your Raspberry Pi acts as a web server, a firewall can help protect it against unauthorized access attempts by filtering and blocking unwanted or malicious requests.

Remote access control: If you access your Raspberry Pi remotely via SSH or other services, a firewall can limit access only to certain IP addresses or IP address ranges, thereby increasing security.

Blocking unwanted traffic: A firewall can block certain types of traffic, such as spam traffic, known botnet traffic, or any other unwanted traffic that may pose a threat to the security of your Raspberry Pi or your network.

Firewall iptables

2.png__PID:801ccd68-ce46-4934-8b50-f7212bdf3766

Configuring the firewall on a Raspberry Pi typically involves using a tool called iptables, which is a user-space utility program that allows a system administrator to configure the IP packet filter rules of the Linux kernel firewall, implemented as different Netfilter modules.

Configuration the firewall (iptables) on a Raspberry Pi:

1.Install iptables (if not already installed):

sudo apt-get update
sudo apt install iptables


2.Define your firewall rules:

Decide what traffic you want to allow or deny. For example, if you want to SSH (port 22) and HTTP (port 80) traffic, but deny all other incoming traffic, you can define the rules accordingly.

Here's an example of how to allow SSH and HTTP traffic and deny all other incoming traffic: 
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT # Allow HTTP
sudo iptables -A INPUT -j DROP    # Drop all other incoming traffic


3.Save your rules:

Once you've defined your rules and confirmed they work as expected, you'll want to save them so they persist across reboots. You can do this by using the `iptables-save` command:
sudo iptables-save > /etc/iptables/rules.v4

4.Make sure iptables rules are restored at boot:
sudo nano /etc/rc.local

Edit the `/etc/rc.local` file to load the saved rules at boot time. Open the file for editing: Add the following line before the `exit 0` line in the file:
/sbin/iptables-restore < /etc/iptables/rules.v4 Save the file and exit.


5.Restart your Raspberry Pi:

6.Check iptables rules:
sudo iptables -L

After rebooting, you can check if the iptables rules are applied correctly by running: This command will display the currently active iptables rules.

UFW (Uncomplicated Firewall)

3.png__PID:1ccd68ce-4609-440b-90f7-212bdf3766a9

Is a user-friendly front-end for managing iptables firewall rules. It simplifies the process of configuring the firewall on a Raspberry Pi.

Configuration UFW on a Raspberry Pi:

1.Install ufw (if not already installed):

sudo apt update
sudo apt install ufw
sudo ufw enable


2.Enable ufw:

This command will start the firewall and enable it to start automatically on boot.

3.Set default policies:

You can set the default policies for incoming, outgoing, and forwarded traffic. For example, to allow all outgoing traffic, deny all incoming traffic, and deny all forwarded traffic, you can use the following commands:

sudo ufw default allow outgoing
sudo ufw default deny incoming
sudo ufw default deny forwarded


4.Allow specific services or ports:

You can allow specific services or ports by specifying them with ufw. For example, to

sudo ufw allow 22/tcp
sudo ufw allow 80/tcp


allow SSH (port 22) and HTTP (port 80) traffic, you can use the following commands:

sudo ufw allow ssh
sudo ufw allow http

You can also specify the service name instead of the port number, for example:

5.Deny specific services or ports (optional):

If you want to explicitly deny certain services or ports, you can use the `deny`

sudo ufw deny 21/tcp

command. For example, to deny FTP (port 21) traffic, you can use:

6.Reload ufw:

After making changes to the firewall rules, you need to reload ufw for the changes to

sudo ufw reload

take effect:

7.Check ufw status:

sudo ufw status verbose

You can check the status of ufw and the firewall rules with the following command:

This command will display the current status of ufw and list all the configured rules.

That's it! You've now configured the firewall using ufw on your Raspberry Pi. ufw provides a simpler interface compared to directly manipulating iptables rules, making it easier to manage the firewall configuration.

Gufw

Gufw is a graphical interface for managing the Uncomplicated Firewall (ufw) on Ubuntu and other Debian-based Linux distributions. However, it's not typically installed by default on Raspberry Pi OS (formerly known as Raspbian), the official operating system for Raspberry Pi.

4.png__PID:cd68ce46-0934-4b50-b721-2bdf3766a9c4

Configuration GUFW on a Raspberry Pi

1.Install gufw:

sudo apt update
sudo apt install gufw


2.Launch gufw:

After installation, you can launch gufw by searching for it in the applications menu, or

sudo gufw

you can start it from the command line:

5.png__PID:68ce4609-340b-40f7-a12b-df3766a9c44c

3.Configure Firewall Rules:

Once gufw is launched, you'll have a graphical interface where you can configure your firewall rules. You can enable or disable the firewall, allow or deny specific ports or applications, and set default policies for incoming, outgoing, and forwarded traffic.

6.png__PID:ce460934-0b50-4721-abdf-3766a9c44c07

4.Apply Changes:

After configuring your firewall rules in gufw, make sure to click the "Apply" button to apply the changes.

7.png__PID:4609340b-50f7-412b-9f37-66a9c44c07fe

Keep in mind that gufw is just a graphical interface for ufw, so all the configuration changes you make through gufw are essentially modifying ufw rules in the background.

Using gufw can be especially helpful if you prefer a graphical interface for managing your firewall rules or if you're less comfortable working with the command line.

Complete Guide to Setting Up a Raspberry Pi as a Firewall: How to Configure IPFire on Your LAN Back to News The Ultimate Guide to Transforming Your Raspberry Pi into a Powerful Router