Raspberry pi check firewall status: A Step-by-Step Setup

Raspberry pi check firewall status: A Step-by-Step Setup

IEM Robotics

Table of Content 

Raspberry Pi is a compact but versatile computer that many use for different functions. Whether you're automating your home or using it as a web server, it has you covered. All of this is possible due to IoT, or the Internet of Things. While connectivity to the Internet opens up many doors and opportunities, it is also possible to use raspberry pi check firewall status since it is at risk of a ton of security vulnerabilities.

The most elementary way of keeping your Pi safe is by having and monitoring a firewall. Consider it a watchman for your devices. It decides what goes in and out during network traffic based on predetermined rules. It allows any legitimate information to pass over while blocking anything that is unauthorised or a threat.

This guide will tell you the basics of everything you should know about the safety of your Pi and firewall status. There are several tools that you can use. This guide will tell you how to configure and look after your firewall. Ensure that your device is secure and safe.

What is a firewall, and why do you need it

To secure your network, it is first and foremost important to decipher what it does. You can have two kinds of firewalls: hardware and software-based. The raspberry pi firewall essentially deals with the software-based one. It filters out whatever it considers a threat between your system and the network. Some of its functions include:

Unauthorized access getting blocked: Not everything from outside can be accessed, only the specified services.

Filtering the traffic: The firewall lets information pass through based on several factors, such as the IP address, protocols, and ports.

Protection from malicious traffic: It saves you from getting scammed

Segmenting your network: If you are using several applications on your Raspberry Pi, the firewall helps isolate the services.

Monitoring raspberry pi check firewall status is thus essential to have a smooth and safe experience.

The basics: raspberry pi network firewall

The Raspberry Pi OS is Linux-based and does not have a pre-installed firewall. That means you will have to configure a firewall on your Pi manually. All your ports will be open for you, which is risky as it threatens your system. While there are firewalls for you to choose from, the most common one used by beginners and advanced users is UFW (Uncomplicated Firewall). Just follow these 10 steps to install, configure, and monitor your firewall.

1. Check to see if you have a firewall installed:

● Open the terminal on your Pi and run this code: ‘which ufw’

● If it is installed, then you will get back a code which says ‘/usr/sbin/ufw’

● If nothing appears on the screen, the firewall is not yet installed.

2. Install the firewall

● Use the terminal to install the firewall in just a few simple steps.

● Ensure that you have updated your system

● Run the code ‘sudo apt update sudo apt install ufw’

● This will get you the necessary packages for UFW

3. Raspberry pi check firewall status through UFW

● Check the status of your firewall

● Using the following code to do so: ‘sudo ufw status verbose’

4. Understand the output

● If the firewall is not active, it will show something like ‘Status: inactive’

● If it is active, something like this should appear on the screen

Status: active

Logging: on (low)

Default: deny (incoming), allow (outgoing), disabled (routed)

New profiles: skip

● If the firewall is active, it tells you the following:

- It is filtering the traffic

- Even though it is optional, logging is on, which is useful for diagnostics

- Outgoing traffic is permitted; however, incoming is not

- Any routed traffic is not getting filtered.

5. Enabling/disabling the raspberry pi firewall settings

● If your firewall is active, then enable it using the code ‘sudo ufw enable’

● Also note that if you are using SSH, ensure that it is not locked out using the code: ‘sudo ufw allow ssh’

● To disable the firewall, use the code ‘sudo ufw disable’- You must re-enable the firewall if you want to use it.

6. Use UFW to configure the firewall

● To allow HTTP, use ‘sudo ufw allow 80/tcp’

● To allow HTTPS use ‘sudo ufw allow 443/tcp’

● To allow custom port use ‘sudo ufw allow 8080’

● To not allow any port in particular, use ‘sudo ufw deny 23’

● To delete or remove a rule, use ‘sudo ufw delete allow 8080’

● To reset all the rules, use ‘sudo ufw reset’

7. Check the rules of the firewall using iptables: this is for advanced users

● To see the rules, use ‘sudo iptables -L -v -n’

● Through this, you can see all the rules related to byte sizes, settings of the policies, and protocols

8. Nftables (replacements for iptables)

● Use code ‘which nft’ to check if it is installed

● Use the code ‘sudo nft list ruleset’ to check the rules

9. Firewall settings

● This is to check the persistence of the firewall settings.

● Any configuration gets automatically saved by UFW, and to verify, you can use the following code: ‘sudo systemctl is-enabled ufw’

● If this code shows enabled, it is a sign that it will remain persistent throughout the reboots.

● If you are using iptables and you want it to save the rules through the reboots, use the code ‘sudo apt install iptables-persistent sudo netfilter-persistent save’

10. Test the configuration of your firewall

● This is the last and final step

● There are tools that you can use to test your firewall once it has been configured

● nmap: if your raspberry pi allow port, this will check it for you, just use the code ‘nmap raspberry-pi-ip-address’

● To access any particular service, use the code ‘curl http://raspberry-pi-ip:8080’

● If your logging is enabled, then you can find the UFW logging at ‘/var/log/ufw.log’

● These tools help you access ports that you intend to.

Guide for keywords

To remember and better access all the keywords, with their codes and functions, refer to the following guide, which makes it easy to navigate and follow for raspberry pi check firewall status

Action: Installing UFW

Command: sudo apt intsall ufw

Action: checking the status

Command: sudo ufw status verbose

Action: enabling the firewall

Command: sudo ufw enable

Action: Allowing SSH

Command: sudo ufw allow ssh

Action: allowing port

Command: sudo ufw allow 8080

Action: deleting the rule

Command: sudo ufw delete allow 808

Action: listing out the iptables rules

Command: sudo iptables -L -v -n

Action: checking the ruleset for nftables

Command: sudo nft list ruleset

Action: enabling UFW at boot

Command: sudo systemc1 enable ufw

Troubleshooting

While raspberry pi check firewall status, you can run into some issues. Here are some troubleshooting tips for a smooth process. If you have any other issues feel free to check out the official raspberry pi website.

1. Problem: Blocking your SSH access by mistake on raspberry pi 3 firewall

Solution: If you have blocked port 22, no need to get frustrated, just pre-allow your SSH before you enable UFW by running the code ‘sudo ufw allow ssh’. However, losing your SSH might pose a problem if your Pi is headless.

2. Problem: recovering if you get locked out

Solution: Start by connecting a keyboard and monitor to your Pi. Then, locally log in to disable the firewall using the code ‘sudo ufw disable’. Consequently, reconfigure the SSH and re-enable the firewall using the code ‘sudo ufw allow ssh sudo ufw enable’

3. Problem: Checking out the services and ports that have been blocked

Solution: First, check if the service runs through the code ‘sudo systemctl status apache2’. Followed by checking if that port is opening up locally by running the code ‘sudo netstat -tuln | grep 80’. For that particular port, test the UFW through ‘sudo ufw status’ (ensure that the port for HTTP is allowed), and disable the UFW temporarily. If the service can be accessed after all this, then there might be an issue with the firewall.

4. Problem: If you are either mixing UFW with other tools or not using it altogether, then you might face some issues

Solution: There's no need to panic, though. Just check the iptables rules and the nftables conflicts to see the UFW behaviour.

Conclusion

If you intend to utilize your Raspberry Pi in an integrated setting, whether locally or online, it is essential for raspberry pi check firewall status. Although the Raspberry Pi OS does not by default enable a firewall, there are several ways to do it, with UFW being the most accessible. It connects with the system's services to remain active throughout reboots and enables fast and easy rule configuration.

You can use more advanced tools like iptables and nftables if you are not a beginner. These are more complicated to understand but offer better control over the firewall. Installing a firewall is essential in keeping your system safe while working on some IoT project or browsing the web. Configuring and installing your firewall might be crucial, but it is also important to audit the rules of your firewall, test out the network, and review the logs. Take time to configure and monitor the firewall to ensure a headache-free tomorrow.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.

People Also Ask

How do I check if the UFW firewall has already been installed?

You can run the code to check the status, and depending on the response, you can find out

Can I start over by resetting the firewall settings?

Yes, you can reset your UFW by using the code ‘sudo ufw reset’ and then allowing the essential ports, like SSH, to function by enabling them

After I change the rules, do I have to restart my Pi or the firewall?

No, you do not need to reboot anything. UFW and iptables apply any rule changes as and when you change them. However, note that if you are using iptables, you will have to manually save the updated rules so they do not change after reboots.

What is the difference between UFW and iptables/nftables?

UFW is beginner-friendly and has an easy-to-manage interface. iptables is for advanced users and is low-level. nftables are also for advanced users and are a replacement for iptables.

How do I check if my firewall is working?

To do this, use nmap to scan all the open ports. Also, use netcat to test out the specific ports. The connection will not work if the port has been blocked.

Is it better to keep the ports closed by default?

Use the default deny option and then allow the services you require for a better and safer configuration.

How do I allow access for a particular network interface?

You can do so by referring to the rules in UFW and using the code ‘sudo ufw allow in on wlan0 to any port 22’. Replace wlan0 with your particular network interface.

Can UFW be used on Raspberry Pi OS Lite?

Yes, as long as you install it using the code.

If my Pi does not access the internet, do I need a firewall?

It is recommended that you do so for safety reasons.

Do all models still support iptables?

nftables are replacing iptables in the latest Linux systems.