Security

How can we help?

Guides and technical documentation for your Server4You dedicated server or VPS.

nftables Configuration & Security

This guide shows how to set up a default-deny firewall with nftables, the successor to iptables, on Debian, Ubuntu, AlmaLinux or Rocky Linux. It covers a complete /etc/nftables.conf, opening ports, rate limiting and saving rules persistently.

iptables or nftables? On current distributions such as Debian 13, Ubuntu 24.04, AlmaLinux and Rocky Linux, nftables is the default framework. If you are running an existing server with iptables you can stay with it — for new servers we recommend nftables. View the iptables guide →

nftables vs. iptables — Key Differences

Featureiptablesnftables
ToolingSeparate tools (ip6tables, arptables…)One unified tool for all protocols
ConfigurationLine-based commandsStructured rule files (readable like code)
PerformanceGoodBetter — optimized kernel bytecode
FutureLegacy, no longer actively developedActively developed, default on modern distros

1. Installation & Preparation

On most modern distributions nftables is already pre-installed. Install it if needed:

Install nftables (Debian/Ubuntu)
apt update && apt install nftables systemctl enable --now nftables
Install nftables (AlmaLinux/Rocky Linux)
dnf install nftables systemctl disable --now firewalld systemctl enable --now nftables

If existing iptables rules are active, reset the policies and flush the rules first to avoid conflicts and lockouts:

Flush existing iptables rules (optional)
iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT iptables -F iptables -X

2. Basic Rule Structure

nftables uses tables and chains. A table groups multiple chains together. Unlike iptables, tables and chains must be created explicitly before use.

Show current ruleset
nft list ruleset
Flush all rules (clean start)
nft flush ruleset

3. Set Up a Default-Deny Firewall

The recommended approach is to write a complete configuration file and load it. Create or edit /etc/nftables.conf:

/etc/nftables.conf
#!/usr/sbin/nft -f flush ruleset table inet filter { chain input { type filter hook input priority 0; policy drop; # Allow loopback iif lo accept # Allow established connections ct state established,related accept # Drop invalid packets ct state invalid drop # Allow SSH (port 22) tcp dport 22 accept # Allow HTTP and HTTPS tcp dport { 80, 443 } accept # Allow ICMP (ping) ip protocol icmp accept ip6 nexthdr icmpv6 accept } chain forward { type filter hook forward priority 0; policy drop; } chain output { type filter hook output priority 0; policy accept; } }

Load the configuration:

Load configuration
nft -f /etc/nftables.conf

4. Opening Individual Ports

Rules can either be added directly to the configuration file or applied via command at runtime:

Open an additional port (e.g. for a game server)

Add port to running configuration
nft add rule inet filter input tcp dport 25565 accept

Restrict database access to a specific IP

Critical ports like MySQL should only be opened for known, trusted IP addresses:

Allow MySQL for one IP only
nft add rule inet filter input ip saddr 192.0.2.5 tcp dport 3306 accept

Open a UDP port (e.g. for a game server)

Allow UDP port
nft add rule inet filter input udp dport 27015 accept

5. Rate Limiting & Protection

SSH Brute-Force Protection

Limits new SSH connection attempts to 5 per minute (in total, not per IP). In the input chain of your configuration file, replace the line tcp dport 22 accept with these two lines:

SSH rate limit
tcp dport 22 ct state new limit rate 5/minute accept tcp dport 22 ct state new drop

Drop Invalid Packets

Already included in the base configuration — drops packets that do not belong to any known connection:

Drop invalid packets
ct state invalid drop

Log Dropped Packets

Add this rule at the end of the input chain, just before the implicit DROP policy. The limit statement must come before log, otherwise every packet is logged:

Enable logging
limit rate 5/minute log prefix "nftables dropped: " level warn

6. Verify & Save Rules Persistently

Show active rules

List all active rules
nft list ruleset

Save running rules to file

Export the current running configuration directly to the persistent config file:

Save configuration
nft list ruleset > /etc/nftables.conf

Enable service (auto-load on boot)

Enable nftables on startup
systemctl enable nftables systemctl restart nftables

When the nftables service is enabled, it automatically loads /etc/nftables.conf on every boot. Make sure the configuration file is always up to date after adding rules via command line, and add flush ruleset as the first line again after exporting. On AlmaLinux and Rocky Linux, the service loads /etc/sysconfig/nftables.conf instead — save your rules there or include your file from it.

Further Documentation

For a complete reference of all nftables options, refer to the official documentation.