How to Migrate cPanel Accounts Between Servers Using WHM Transfer Tool (Zero-Downtime Guide) Moving live…
Configure nftables Linux: Firewall Setup Guide

How to Configure nftables Firewall on Linux
Configuring a Linux firewall is an essential step in securing a VPS or dedicated server. If you want to configure nftables Linux systems use to control network traffic, you need to understand how tables, chains, rules, and policies work together. A properly configured firewall can block unwanted connections while keeping essential services such as SSH, HTTP, and HTTPS accessible.
nftables is the modern Linux packet-filtering framework that replaces many traditional iptables workflows. It provides a unified syntax for managing firewall rules for IPv4 and IPv6, making it a practical choice for Linux server administration.
In this guide, you will learn how to inspect an existing firewall, create nftables tables and chains, allow essential services, apply rules safely, make the configuration persistent, and troubleshoot common problems without unnecessarily locking yourself out of your server.
What Is nftables in Linux?
nftables is a framework used to filter, classify, and manage network packets in the Linux kernel. Administrators can use it to control incoming traffic, outgoing connections, and packets forwarded between network interfaces.
Unlike older firewall configurations that may maintain separate rule sets for IPv4 and IPv6, nftables can use the inet address family to handle both protocols in a single ruleset.
Common uses include:
- Allowing or blocking connections to specific ports.
- Restricting access to SSH and administrative services.
- Allowing web traffic on ports 80 and 443.
- Filtering traffic by source IP address or network interface.
- Applying rules to both IPv4 and IPv6.
- Persisting firewall rules across system reboots.
nftables vs. iptables
| Feature | nftables | iptables |
|---|---|---|
| Firewall management | Modern unified framework | Legacy command interface |
| IPv4 and IPv6 | Can manage both through inet tables |
Traditionally separate tools or families |
| Rule management | Unified nft syntax |
Separate command syntax |
| Rule sets | Supports named sets and maps | Different mechanisms and extensions |
| Compatibility | Native nftables rules | Compatibility depends on the installed backend |
On many current Linux distributions, nftables is the recommended framework for new firewall configurations. However, existing systems may still use iptables-compatible tools, firewalld, UFW, or cloud-provider firewalls. Check the current configuration before making changes.
1. Check Your Linux Distribution and Existing Firewall
Before configuring nftables, identify your distribution and inspect the current firewall rules.
Check your operating system:
cat /etc/os-release
Check whether the nftables command is available:
command -v nft
nft --version
Display the current ruleset:
sudo nft list ruleset
If the output contains tables and chains, the server may already have firewall rules. Do not replace them blindly; they could be managed by another firewall service or hosting control panel.
Check common firewall services where applicable:
systemctl is-active nftables
systemctl is-active firewalld
systemctl is-active ufw
An inactive service does not necessarily mean there are no active kernel rules. The output of nft list ruleset is an important part of the initial inspection.
Back up the current ruleset
Before changing an existing firewall, save its current configuration:
sudo nft list ruleset | sudo tee /root/nftables-backup.nft
This records the rules currently visible to nftables. It is useful for reference and recovery, but it does not back up every external firewall layer or the configuration files used by other firewall managers.
Important: Keep an existing SSH session open while making remote firewall changes. If possible, ensure you have provider console or rescue access before applying a restrictive firewall policy.
2. Install nftables
The installation command depends on your Linux distribution.
Ubuntu and Debian
sudo apt update
sudo apt install nftables
AlmaLinux, Rocky Linux, and compatible RHEL-based systems
sudo dnf install nftables
Older releases may use yum instead of dnf.
Verify the installation:
sudo nft --version
Installing the package does not automatically mean the firewall is configured correctly or that the service is enabled. The next steps establish the rules and persistence settings.
3. Understand nftables Tables, Chains, and Rules
An nftables configuration is organized into several components.
Tables
A table groups related chains, sets, maps, and rules. For example, a table named filter can contain rules for filtering network traffic.
An inet table supports both IPv4 and IPv6.
Chains
A chain contains rules evaluated in order. Base chains attach to a particular networking hook, such as incoming packets or outgoing packets.
Common hooks include:
input— packets destined for the local machine.output— packets generated by the local machine.forward— packets routed through the machine.
A base chain can have a policy such as accept or drop. The policy applies when no rule in that chain determines the packet’s verdict.
Rules
Rules specify conditions and actions. For example, a rule can allow TCP traffic to destination port 22, which is commonly used for SSH.
A basic rule looks like this:
tcp dport 22 accept
This rule accepts packets matching TCP destination port 22 when they reach the chain containing the rule.
4. Create a Basic nftables Firewall
For a new or carefully reviewed setup, you can create an inet table with an input chain that allows established connections, loopback traffic, and SSH before dropping other unmatched incoming traffic.
First, verify your SSH port. On the server, inspect the effective SSH daemon configuration where supported:
sudo sshd -T | grep '^port '
If this command is unavailable, inspect your SSH configuration files and confirm the port through your hosting provider or existing SSH connection. Do not assume port 22 if your server uses a custom port.
The following example assumes SSH listens on port 22. Replace 22 with your actual SSH port before applying the configuration.
Create a configuration file:
sudo nano /etc/nftables.conf
For a clean installation where you have confirmed that replacing the active nftables ruleset is appropriate, use:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
# Allow loopback traffic
iifname "lo" accept
# Drop invalid connection-tracking states
ct state invalid drop
# Allow established and related connections
ct state established,related accept
# Allow SSH (replace 22 if using a custom port)
tcp dport 22 ct state new accept
# Allow ICMP and ICMPv6 diagnostic traffic
ip protocol icmp accept
meta l4proto ipv6-icmp accept
}
chain forward {
type filter hook forward priority filter; policy drop;
}
chain output {
type filter hook output priority filter; policy accept;
}
}
This example applies a restrictive inbound policy while allowing outbound traffic. It is a starting point, not a universal production firewall. Services such as web servers, DNS servers, VPNs, containers, mail servers, and monitoring agents may require additional rules.
The flush ruleset statement removes the existing nftables rules before creating the new rules. Use it only when you have confirmed that replacing the existing ruleset is safe. If firewalld, UFW, a container platform, or another service manages firewall rules, do not blindly replace its configuration.
Why these rules matter
policy dropblocks unmatched incoming packets.iifname "lo" acceptpermits local loopback communication.ct state invalid dropdiscards packets classified as invalid by connection tracking.ct state established,related acceptallows traffic belonging to existing connections and related connections.- The SSH rule allows new connections to the configured SSH port.
- ICMP and ICMPv6 rules allow useful diagnostic traffic, including some network error messages.
- The forwarding policy prevents the server from forwarding packets by default.
- The output policy permits outbound connections, which is useful for common server operations.
A drop policy can affect monitoring, remote backups, application integrations, IPv6 connectivity, and other services. Add only the rules required for your workload, and test them before closing your existing administrative session.
5. Validate and Apply the Firewall Rules
Do not apply a new firewall configuration without checking its syntax first.
Validate the file:
sudo nft -c -f /etc/nftables.conf
The -c option checks the configuration without applying its changes. Correct any reported syntax or configuration errors before proceeding.
If the configuration is intended to replace the active ruleset, apply it:
sudo nft -f /etc/nftables.conf
This operation can change network access immediately. Keep your current SSH session open and, if possible, use a second terminal to test a fresh SSH connection.
Inspect the active rules:
sudo nft list ruleset
To view the rules in a specific table:
sudo nft list table inet filter
To see rule handles, which can be useful when managing individual rules:
sudo nft -a list ruleset
Test SSH access
From a separate terminal on your local computer, try opening a new connection:
ssh -p 22 username@server_ip
Replace username, server_ip, and 22 with your actual login details and SSH port.
If the new connection succeeds, the SSH rule is working for that connection path. Do not close your original session until you have confirmed that new administrative access works.
If you connect through a VPN, a bastion host, or a provider-specific network, test from the same path you normally use for administration.
6. Allow HTTP and HTTPS Traffic
If the server hosts a website, allow inbound TCP traffic on ports 80 and 443.
Add these rules to the input chain, before relying on the chain’s drop policy:
tcp dport { 80, 443 } accept
If your SSH service runs on port 22, the relevant part of the chain could look like this:
chain input {
type filter hook input priority filter; policy drop;
iifname "lo" accept
ct state invalid drop
ct state established,related accept
tcp dport 22 ct state new accept
tcp dport { 80, 443 } accept
ip protocol icmp accept
meta l4proto ipv6-icmp accept
}
This permits incoming HTTP and HTTPS connections over TCP. It does not automatically open other application ports, and it does not configure a web server or TLS certificate.
Validate the updated file before applying it:
sudo nft -c -f /etc/nftables.conf
Then apply it only after confirming that the configuration is appropriate for your server:
sudo nft -f /etc/nftables.conf
If you already have an active firewall managed by another service, use that service’s supported configuration workflow instead of replacing the complete ruleset.
7. Add or Remove Individual nftables Rules
You can manage individual rules directly with the nft command. This is useful for testing or making controlled changes, but direct changes to the running ruleset may not survive a reboot unless the persistent configuration is updated too.
Add a rule
For example, to allow TCP port 8443 in an existing inet filter input chain:
sudo nft add rule inet filter input tcp dport 8443 accept
Confirm that the rule was added:
sudo nft list chain inet filter input
Delete a rule using its handle
Display the chain with rule handles:
sudo nft -a list chain inet filter input
Example output may include a rule such as:
tcp dport 8443 accept # handle 7
Delete that rule using the handle reported on your own server:
sudo nft delete rule inet filter input handle 7
The number 7 is only an example. Use the actual handle from your output because handles differ between rulesets and can change after modifications.
Delete an entire table
Only do this if you are certain the table is no longer needed:
sudo nft delete table inet filter
Deleting a table can remove essential firewall rules and interrupt access. Never run this command merely to troubleshoot an unfamiliar ruleset.
8. Make nftables Rules Persistent Across Reboots
Rules added directly to the active kernel ruleset are generally not a reliable persistence mechanism. Save the intended configuration in the appropriate configuration file and enable the service used by your distribution.
On many Debian and Ubuntu installations using the nftables service, the main configuration file is:
/etc/nftables.conf
Check whether the file exists and contains the configuration you want to load:
sudo cat /etc/nftables.conf
Validate it:
sudo nft -c -f /etc/nftables.conf
Enable the service at boot:
sudo systemctl enable nftables
Start it if appropriate for your distribution and firewall management setup:
sudo systemctl start nftables
Check the service status:
sudo systemctl status nftables
On systems where the service is already active, use the distribution’s supported reload or restart workflow after verifying the configuration. Be aware that a service restart may replace active rules depending on the service unit and configuration.
Do not enable multiple competing firewall managers without understanding how they interact. For example, firewalld or UFW may manage the kernel ruleset independently. Choose the intended management method and follow its supported persistence workflow.
Verify persistence
After saving the configuration and enabling the appropriate service, review its status and configuration. When a reboot test is necessary, schedule it during a maintenance window and ensure that provider console access is available.
After rebooting, check:
sudo systemctl status nftables
sudo nft list ruleset
Confirm that SSH and the services you intentionally allowed are reachable from a separate connection.
9. Troubleshoot Common nftables Problems
Firewall issues are often caused by a missing rule, an incorrect port, conflicting management tools, or a configuration that differs from the active ruleset.
Problem: SSH connection times out
Check the active rules:
sudo nft list ruleset
Look for an input-chain rule allowing the correct SSH port. Also confirm that the server’s SSH daemon is listening:
sudo ss -lntp
If you use a non-standard port, ensure the nftables rule matches it. Check provider-level firewalls, security groups, routing, and the SSH daemon configuration as well.
Problem: nftables reports a syntax error
Run the validation command:
sudo nft -c -f /etc/nftables.conf
Read the reported line number and inspect the surrounding rules. Common causes include missing semicolons, unsupported syntax, misspelled chain names, and referencing a table or chain that does not exist.
Problem: Rules disappear after reboot
Inspect the persistent configuration:
sudo cat /etc/nftables.conf
Check the service:
sudo systemctl is-enabled nftables
sudo systemctl status nftables
Make sure you changed the configuration file actually loaded by your system’s firewall service. A manually edited file may not be used if another firewall manager owns the active ruleset.
Problem: Website traffic is blocked
Check whether TCP ports 80 and 443 are allowed in the active input chain. Verify that the web server is listening:
sudo ss -lntp
Then test connectivity from outside the server. If nftables allows the traffic but the website remains unreachable, inspect the web server, DNS records, cloud firewall, and network routing.
Problem: Another firewall tool changes the rules
Check which firewall services are running:
systemctl is-active nftables
systemctl is-active firewalld
systemctl is-active ufw
These checks alone do not identify every source of firewall changes, but they help reveal common conflicts. Review the service configuration and avoid editing the same ruleset independently through multiple management tools.
10. Practical Security Recommendations
When you configure nftables on Linux, use a process that prioritizes secure access and predictable changes.
- Allow SSH before applying a restrictive input policy. Confirm the actual port and any source-IP restrictions.
- Keep an existing administrative session open while testing a new SSH connection.
- Use provider console or rescue access when changing firewall rules remotely.
- Permit only required inbound services. Avoid opening database, cache, and administration ports to the public internet unnecessarily.
- Keep IPv4 and IPv6 in mind. An
inettable can filter both, but the rules must still match the intended traffic. - Preserve ICMPv6 where needed. Overly restrictive IPv6 filtering can interfere with important network functions.
- Back up the current ruleset and understand whether another tool manages it.
- Validate configuration before applying it using
nft -c. - Maintain a persistent configuration through the firewall manager your distribution uses.
- Review rules after major changes to applications, containers, VPNs, or network interfaces.
For production systems, test firewall changes in a maintenance window and document the ports, source networks, and services each rule is intended to support.
Conclusion
Learning how to configure nftables on Linux gives you direct control over the traffic allowed to reach your server. By understanding tables, chains, rules, and default policies, you can build a firewall that exposes only the services your workload needs.
Start by inspecting the existing ruleset, confirming the SSH port, and backing up the current configuration. Create and validate the rules carefully, allow required services, and test a fresh administrative connection before closing your existing session. Finally, configure persistence through the firewall manager used by your distribution and verify the rules after a restart.
If you need a Linux environment for server administration, application deployment, or firewall testing, explore ServerNext VPS solutions and choose a setup that fits your workload.
A firewall is only one part of server security, but a carefully maintained nftables configuration is an important foundation for reducing unnecessary network exposure.
