A firewall is a network security system that monitors and controls incoming and outgoing traffic based on rules you define. For a server exposed to the internet, a properly configured firewall blocks unauthorized access attempts, port scans, and unwanted connections before they reach the operating system. If you operate a virtual or dedicated server, running a firewall is not optional.
How a Firewall Works
Firewalls act as packet filters. Each incoming or outgoing packet is checked against rules based on source IP, destination IP, port number, and protocol (TCP, UDP, ICMP). When a rule matches, the packet is allowed (ACCEPT), rejected with a response (REJECT), or silently dropped (DROP). Modern firewalls are stateful: they track connection state and only allow packets that belong to established sessions. Stateless firewalls evaluate every packet independently, while stateful ones maintain a connection table and reject spoofed or out-of-sequence packets. For example, if you allow outgoing HTTP requests, a stateful firewall automatically allows the corresponding incoming responses, so you don’t need to open a separate rule for return traffic.
Types of Firewalls
Firewalls can be hardware or software. Hardware firewalls typically sit at the network edge and handle high traffic with low latency. Software firewalls run on the operating system and provide per-server control. In hosting environments, OS-level software firewalls are the most common. Network-based firewalls protect an entire network; host-based firewalls protect a single server. For virtual server users, a host-based firewall is mandatory.
Linux Firewall Tools
Linux offers three main tools: iptables, firewalld, and UFW.
UFW (Uncomplicated Firewall)
UFW is the default frontend for iptables on Ubuntu and Debian. It simplifies common rules. Basic setup:
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
This opens SSH, enables the firewall, and shows the status. After enabling, only allowed services can receive connections. You can also allow specific ports, such as sudo ufw allow 8080/tcp.
firewalld
On RHEL-based distributions (AlmaLinux, Rocky Linux, RHEL 9+), firewalld is the default management tool. It uses zones and supports dynamic rule updates:
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload
The first command allows HTTP traffic permanently; the second applies changes immediately. The default zone is public; you can change zones or add services to specific zones.
iptables
iptables is the classic tool that talks directly to the kernel’s netfilter framework. It has largely been replaced by nftables, which is the modern backend used by UFW and firewalld. For most new installations, UFW or firewalld is easier to manage.
Firewall for Virtual Servers
When you use a virtual server (VDS), you are fully responsible for the OS-level firewall configuration. You can choose any of the tools above to restrict traffic. If you run game servers, web applications, or databases, you need to open only the necessary ports. For guidance on selecting infrastructure for game servers, see our article on game server hosting. A properly configured firewall is part of a secure hosting setup.
Configuration Best Practices
- Set default policies: incoming traffic to DROP, outgoing to ACCEPT.
- Open only the ports you need. For a web server, that is 80 and 443; for SSH, 22.
- Restrict SSH access to your office IP or VPN range if possible.
- Use fail2ban alongside the firewall to block brute-force attempts.
- Review rules regularly and close unused ports.
- Complement the firewall with other layers: strong passwords, regular updates, and SSL certificates. Learn how to set up SSL with Certbot.
Conclusion
A firewall is the security guard at your server’s door. With correct rules, it blocks many internet threats before they reach your applications. Every virtual server user should activate at least one host-based firewall tool and apply the basic rules above.