In an age where connectivity is omnipresent, safeguarding our digital boundaries has become paramount. Among the crucial defenses in this realm stands the firewall—an indispensable guardian fortifying our systems against an array of potential threats.
What is a Firewall?
A Firewall serves as a barrier between your computer/network and the vast, potentially hazardous expanse of the internet. Much like a security checkpoint, it monitors incoming and outgoing traffic, permitting or blocking data packets based on predefined security rules. This vital piece of software discerns between safe and malicious activity, curbing unauthorized access and protecting sensitive data from cyber threats.
The Role of Firewalls
Firewalls come in various forms, from software-based solutions integrated into operating systems to dedicated hardware appliances. Regardless of the form, their role remains consistent: shielding devices and networks by filtering traffic, preventing unauthorized access, and minimizing the risk of cyber attacks.
Setting Up a Basic Firewall
By default, SSH connections are typically established through Port 22 (TCP). The main advantage of using the default SSH-2 protocol (compared to VNC) is the encrypted connection using an AES algorithm with a 128-bit key length. Therefore, it is generally recommended to use SSH instead of VNC whenever possible.
In Linux, you should consider using the built-in software package ‘iptables’, which comes with the initial installation on most distributions by default.
There are two main advantages to using iptables:
The required software package is installed by default on most Linux distributions.
It is compatible with any Linux distribution (Ubuntu, Debian, CentOS, etc.) without restrictions.
You can also define IPv4 and IPv6 rules according to your own needs.
Checking the Current Firewall Configuration
Before configuring your firewall, there are no rules defined. To check this, use the following command:
iptables -L
The output will look similar to this:
As you can see, there are currently no rules defined. The Software-Firewall will not block anything.
Here’s an example of how to use the iptables command to allow incoming traffic at port 80:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
Example Firewall Configuration for Linux
Here is an example script that will open the most commonly used ports on your Linux server:
#!/bin/bash# Delete the current firewall setup:
iptables -F
# Define default rules for all chains:
iptables -P INPUT DROP
iptables -P FORWARD DROP
# Allow incoming/outgoing localhost frames for tests (e.g. Webserver, Mailserver):
iptables -A INPUT -d 127.0.0.1 -j ACCEPT
iptables -A OUTPUT -s 127.0.0.1 -j ACCEPT
# Allow loopback frames for the internal process management:
iptables -A INPUT -i lo -j ACCEPT
# Allow incoming/outgoing related-established connections:
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# Allow incoming PING-Requests:
iptables -A INPUT -p icmp -j ACCEPT
# Allow incoming SSH connections:
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Allow incoming HTTP/HTTPS requests:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# Allow incoming DNS requests:
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
To use this script:
Login to your Linux server as root.
Create a shellscript with this command:
nano firewall.sh
Paste in the content shown above.
Save the file with [CTRL] + O and exit the editor with [CTRL] + X.
Make the script executable with this command:
chmod +x firewall.sh
To run the script and open the ports provided by the script, use the following command:
./firewall.sh
Making the Firewall Rules Permanent
Please note that if you use the sample script, all firewall rules will not be added permanently. After a server reboot, every rule would have to be set manually again.
To make the firewall rules permanent, you can run the following commands depending on your operating system:
Permanent Firewall Settings for CentOS 7 or higher: