A properly configured firewall is one of the most important defenses for your server. It controls which network traffic is allowed in and out, blocking unauthorized access while permitting legitimate services. This guide covers firewall configuration on Ubuntu/Debian (UFW) and CentOS/AlmaLinux (firewalld).
Firewall Basics
A firewall filters network traffic based on rules:
- Inbound rules — Control traffic coming into your server (e.g., web requests, SSH connections).
- Outbound rules — Control traffic leaving your server (e.g., API calls, DNS lookups).
- Default policy — What happens to traffic that does not match any rule (typically deny).
UFW (Ubuntu / Debian)
UFW (Uncomplicated Firewall) provides a simple interface for managing iptables rules.
Initial Setup
# Install UFW (usually pre-installed)
sudo apt install ufw -ySet default policies
sudo ufw default deny incoming
sudo ufw default allow outgoingAllow SSH before enabling (critical!)
sudo ufw allow sshEnable the firewall
sudo ufw enableCommon Rules
# Allow SSH (port 22)
sudo ufw allow sshAllow HTTP (port 80)
sudo ufw allow 80/tcpAllow HTTPS (port 443)
sudo ufw allow 443/tcpAllow a custom port
sudo ufw allow 8080/tcpAllow from a specific IP
sudo ufw allow from 203.0.113.50Allow from a specific IP to a specific port
sudo ufw allow from 203.0.113.50 to any port 3306Allow a port range
sudo ufw allow 6000:6010/tcpDeny a specific port
sudo ufw deny 23/tcpDelete a rule
sudo ufw delete allow 8080/tcpView Active Rules
sudo ufw status verbose
sudo ufw status numberedRate Limiting SSH
Protect against brute-force SSH attacks:
sudo ufw limit sshThis limits SSH connections to 6 per 30 seconds from a single IP.
firewalld (CentOS / AlmaLinux)
firewalld uses zones and services for flexible firewall management.
Initial Setup
# Start and enable firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalldCheck default zone
sudo firewall-cmd --get-default-zoneList current rules
sudo firewall-cmd --list-allCommon Rules
# Allow SSH
sudo firewall-cmd --permanent --add-service=sshAllow HTTP and HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=httpsAllow a custom port
sudo firewall-cmd --permanent --add-port=8080/tcpAllow from a specific IP
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.50" accept'Allow a specific IP to a specific port
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.50" port port="3306" protocol="tcp" accept'Remove a rule
sudo firewall-cmd --permanent --remove-service=httpReload to apply changes
sudo firewall-cmd --reloadEssential Firewall Rules by Service
Web Server
# UFW
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpfirewalld
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reloadDatabase (Allow Only Localhost)
Databases should not be exposed to the internet. Keep port 3306 (MySQL) or 5432 (PostgreSQL) blocked externally.
If you need remote database access, allow only specific IPs:
# UFW — allow MySQL from a specific server
sudo ufw allow from 203.0.113.20 to any port 3306firewalld
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.20" port port="3306" protocol="tcp" accept'Mail Server
# SMTP, IMAP, POP3 with SSL
sudo ufw allow 25/tcp
sudo ufw allow 465/tcp
sudo ufw allow 587/tcp
sudo ufw allow 993/tcp
sudo ufw allow 995/tcpAdvanced Hardening
Block ICMP Flood (Ping)
For servers that do not need to respond to ping:
# UFW — edit /etc/ufw/before.rules and change ACCEPT to DROP for ICMP
sudo nano /etc/ufw/before.rulesLog Dropped Packets
Enable logging to monitor blocked connections:
# UFW
sudo ufw logging onfirewalld
sudo firewall-cmd --set-log-denied=allLogs appear in /var/log/syslog (Ubuntu) or /var/log/messages (CentOS).
Block Known Malicious IPs
sudo ufw deny from 192.0.2.100Docker and Firewalls
Docker bypasses UFW by default, manipulating iptables directly. See Installing Docker & Docker Compose for Docker-specific firewall configuration.
Testing Your Firewall
After configuring rules:
# From another machine, test if a port is open
nc -zv 203.0.113.10 22
nc -zv 203.0.113.10 3306What to Do Next
- SSH Key Best Practices — Secure SSH access.
- DDoS Protection — Additional network protection.
- Complete Server Security Checklist — Full security review.
- Firewall Blocking Legitimate Traffic — Troubleshoot rules.