Linux Security: A Complete Guide to Firewalls and Network Rules

Last update: 26/08/2026
Author Isaac
  • Implementing packet filtering using the netfilter kernel with tools such as UFW, firewalld, and iptables.
  • Setting up a layered defense strategy that combines the cloud provider's firewall with internal system rules.
  • Implementation of system hardening measures such as the principle of least privilege, secure SSH access, and frequent updates.

Corridor of a modern data center with server racks illuminated in blue, representing the infrastructure where Linux firewalls are deployed.

If you have a Linux server, you know that leaving it open to the elements is like leaving your front door unlocked in the middle of an unfamiliar city. To prevent any curious onlookers or attackers from getting in where they shouldn't, it's essential to set up a solid security perimeter , and that's where the firewall comes in—the guardian that decides who gets in and who stays out.

Setting up a firewall doesn't have to be a headache or a job for network experts. It's really just a matter of applying some common sense and following basic filtering rules to keep your services running smoothly without exposing your system to unnecessary risks. Let's break down how to secure your system from the ground up, including the most advanced tools.

Related articles:
Complete Guide to Linux Security and Intrusion Detection Tools

What exactly is a firewall and how does filtering work?

Systems administrator configuring a server from a laptop in a professional data center environment.

Basically, a firewall is a system that monitors and controls the traffic entering and leaving your machine based on rules you define. In the Linux kernel, this is done by a component called netfilter , which resides within the kernel. It's important to understand that tools like ufw or firewalld are not the firewall itself, but rather user interfaces for easily managing netfilter.

The key process is packet filtering . Each piece of data is divided into packets, and the firewall analyzes the header to decide: accept (ACCEPT), drop (DROP), or warn and reject (REJECT). To do this, it looks at the IP address, port, and protocol.

  How to tell if someone is stealing your WiFi by looking at the router's LEDs

First steps: Mapping cloud services and security

Before writing rules, it's essential to take inventory of services. The most common practice is to leave port 22 open for SSH and port 443 for HTTPS, while blocking everything else.

If you work with cloud servers, take advantage of the security groups offered by your provider. These external firewalls filter traffic before it reaches your server, and you only need to allow specific IPs for defense in depth and network segmentation.

Rack server illuminated with blue lights showing high-speed hard drives in a modern data center
Related articles:
Complete Guide to Configuring Docker on Linux: Performance and Security

Mastering Linux tools: iptables, UFW, and firewalld

Close-up of a monitor displaying cybersecurity code and data, illustrating the technical configuration of firewall rules.

Depending on the layout, you have different utilities available. iptables It's powerful but complex; you can use commands to open ports and block traffic, for example. iptables -A INPUT -p tcp --dport 22 -j ACCEPTTo avoid it being a nightmare, Ubuntu or Debian includes UFWVery simply put: it allows and denies services in a basic way. In environments with Red Hat, CentOS, or Fedora, you have firewalld and its trusted zones that simplify dynamic management.

Making the rules survive resets

If the configuration doesn't persist, everything will be lost upon reboot. In iptables, install iptables-persistent o iptables-services and save with iptables-saveThis way your firewall remains active after a reboot.

How to use sudo in Linux
Related articles:
How to use sudo in Linux and manage it securely

Beyond the firewall: Total terminal shielding

Linux terminal with colored code, metaphor of packet flow and network logs.

Not just the firewall: apply the principle of least privilege, review accounts, and update the system regularly. For remote access, disable root login and use public keys; add 2FA, and if it's a business, integrate EDR for real-time monitoring.

Testing and preventive maintenance

Never be overconfident: check with curl that the services respond, and with nmap From another machine, only the specified ports are open. Archive the rules and clean up old entries each month to maintain control.

In short, a layered firewall, with regular updates and auditors, turns your server into an impregnable fortress.

Close-up of a modern server unit in a data center with blue lighting, representing the security infrastructure in Linux.
Related articles:
Linux Security: A Master Guide to Backups and Disaster Recovery