Security

What Is a Firewall? How ufw, nftables and Cloud Firewalls Protect Servers

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

A firewall is a set of rules that decides which network traffic may reach a system and which traffic may leave it. On a server, its main job is to make sure that only the services you intend to publish are reachable from the internet, while everything else, such as databases, admin panels and internal tools, stays invisible. A firewall does not fix vulnerable software, but it dramatically shrinks the number of doors an attacker can try. This guide explains how firewalls work and shows practical configurations for a Linux VPS.

How a firewall makes decisions

Every packet carries information such as its source IP address, destination IP address, protocol (TCP, UDP, ICMP) and port. A firewall compares these fields against an ordered list of rules and applies an action: accept, drop (silently discard) or reject (discard and notify the sender).

Two concepts matter most:

  • Default policy: what happens to traffic that matches no rule. For inbound traffic on a server, the safe choice is "deny".
  • Stateful inspection: modern firewalls track connections. When your server opens a connection to download updates, the replies are automatically allowed because they belong to an established connection. That is why you can block all new inbound traffic without breaking outgoing requests.

Types of firewalls you will meet

TypeWhere it runsTypical use
Host firewall (ufw, firewalld, nftables)Inside the server's OSFine-grained rules per server
Cloud firewallIn the provider's network, before the serverFirst filter, shared rules across servers
Web application firewall (WAF)In front of a websiteFilters HTTP requests for attacks like SQLi
Hardware or network firewallOffice or data-center edgeProtecting whole networks

On Linux, the kernel component that actually filters packets is netfilter. Tools such as iptables, nftables, ufw and firewalld are different front ends for it. Current Ubuntu, Debian and RHEL-family releases use nftables underneath, even when you configure them through ufw or firewalld.

Example 1: ufw on Ubuntu or Debian

ufw ("uncomplicated firewall") is the simplest way to start. Allow SSH before enabling it, or you will cut off your own session:

sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp        # or your custom SSH port
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Useful extras:

sudo ufw allow from 203.0.113.10 to any port 22 proto tcp   # SSH from one IP only
sudo ufw limit 22/tcp                                       # basic rate limiting for SSH
sudo ufw status numbered
sudo ufw delete 3                                           # remove rule number 3

On AlmaLinux and Rocky Linux the default tool is firewalld, where the equivalent commands look like sudo firewall-cmd --permanent --add-service=https followed by sudo firewall-cmd --reload.

Example 2: a minimal nftables ruleset

If you prefer full control, nftables lets you write the ruleset directly in /etc/nftables.conf:

#!/usr/sbin/nft -f
flush ruleset

table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept
    ct state invalid drop
    iif "lo" accept
    meta l4proto { icmp, ipv6-icmp } accept
    tcp dport { 22, 80, 443 } accept
  }
  chain forward {
    type filter hook forward priority 0; policy drop;
  }
  chain output {
    type filter hook output priority 0; policy accept;
  }
}

Test the syntax with sudo nft -c -f /etc/nftables.conf, apply it with sudo nft -f /etc/nftables.conf and enable it at boot with sudo systemctl enable nftables. Do not mix hand-written nftables rules with ufw or firewalld on the same server; pick one tool.

Allowing ICMP is deliberate: it keeps ping and path MTU discovery working, which helps troubleshooting and avoids hard-to-diagnose connection problems.

Example 3: a cloud firewall

A cloud firewall filters traffic in the provider's network before it reaches your VPS. NileVPS servers include one, so you can define inbound rules such as "TCP 443 from anywhere" and "TCP 22 from my office IP" outside the operating system. Advantages:

  • Unwanted traffic never consumes your server's resources.
  • Rules keep working even if someone accidentally disables the host firewall.
  • A mistake inside the server cannot open ports the cloud firewall still blocks.

A good setup uses both: the cloud firewall as a coarse outer filter and a host firewall for server-specific rules.

A warning about Docker

Docker writes its own netfilter rules when you publish ports with -p 8080:80, and those rules are evaluated before ufw's. A published container port can therefore be reachable from the internet even though ufw seems to block it. Bind private containers to localhost (-p 127.0.0.1:8080:80) and put a reverse proxy in front, or rely on a cloud firewall, which Docker cannot bypass.

Common mistakes

  • Enabling the firewall before allowing SSH.
  • Leaving database ports open to the world "temporarily".
  • Opening wide port ranges instead of specific ports.
  • Forgetting IPv6: make sure your rules cover both address families (ufw does when IPV6=yes in /etc/default/ufw).
  • Never reviewing rules, so old exceptions stay open for years.

A firewall is one layer of a broader plan; see our cloud server security best practices for the rest.

FAQ

Should I use drop or reject for blocked traffic?

Both are secure. Drop gives no reply, which slows scanners slightly and reveals less. Reject answers immediately, which makes troubleshooting easier on internal networks. For public servers, drop is the common default, and the difference in protection is small.

Can a firewall stop DDoS attacks?

A host firewall can block simple floods from a few addresses, but a large attack can saturate the network link before packets reach your rules. Volumetric DDoS needs filtering upstream, at the provider network or through a protection service or CDN.

If I have a cloud firewall, do I still need ufw?

It is recommended. The cloud firewall stops traffic early, while a host firewall protects you if a cloud rule is changed by mistake and lets you apply rules specific to each server. Keeping both configured with the same intent gives defense in depth.

Related guides