How to Install nftables on Ubuntu 26.04 and 24.04

Share

To install nftables on Ubuntu 26.04 or 24.04, use APT to install the Ubuntu package. nftables controls network traffic using rules in the Linux kernel.

This guide checks the existing firewall, then shows a small host firewall, persistence and removal. If you need help, leave a comment or contact us.

To install nftables, refresh the package list and install the package:

Terminal
sudo apt update
sudo apt install nftables

Installing the package is separate from applying a firewall policy. Before starting the nftables service or changing active rules, inspect the existing firewall and read the precautions below.

Before you begin
  • Ubuntu 26.04 or 24.04, internet access and sudo privileges.
  • For firewall changes, use a dedicated test machine or VM with a local or provider recovery console.
  • Keep your current SSH session open and know its actual port. Test a second connection before closing it.
  • Do not apply the example to a host using UFW, Docker, Kubernetes, VPN rules or another firewall manager. Review and migrate those configurations separately.

1. Check the Existing Firewall

Installing the package and activating a firewall are separate tasks. A ruleset is the collection of active nftables tables and rules. Inspect the host before changing either the service or its configuration:

Terminal
sudo nft list ruleset
sudo ufw status verbose
sudo systemctl status nftables --no-pager
sudo ss -ltnp

If nft is not installed yet, its first command may say it was not found; continue with installation and run it again. UFW may also be absent. The listening socket list helps identify SSH and other services, but check your SSH server configuration and provider firewall too. An existing SSH session may remain connected even when new connections are blocked.

Stop before applying the example if you find existing firewall rules, an active UFW or another rule manager, or if you cannot identify how the host is protected. Container and VPN software can manage rules even when UFW is inactive. Ubuntu advises against mixing native nftables rules with UFW or iptables-based managers. If you use UFW, see our UFW firewall commands guide instead. Do not disable UFW remotely as a shortcut; plan a migration with console access.

2. Install and Verify nftables

Step 1   Install from Ubuntu repositories

If you have not installed it yet, use the APT commands near the beginning. After installation, check the tool and existing rules below before changing the firewall.

Step 2   Check the installation

Check the installed tool and inspect the active rules again:

Terminal
nft --version
sudo nft list ruleset

The version command prints the installed version. An empty ruleset means there are no native nftables tables in this network namespace; it does not prove there is no other firewall or upstream filtering.

Do not start, stop, restart or enable the service yet. Ubuntu’s packaged /etc/nftables.conf can contain flush ruleset, which removes all nftables rules when loaded, including rules created by other software. The service stop action can also flush the entire ruleset. Inspect the unit, the file and every included file first:

Terminal
sudo cat /etc/nftables.conf
sudo systemctl cat nftables

If the configuration contains include directives, open the referenced files and any nested includes too. Do not overwrite a configuration you do not understand.

3. Create a Small Host Firewall

The following optional example is for a dedicated host with no existing firewall rules or firewall manager. It allows loopback, established connections, ICMP and new SSH connections, then drops other inbound traffic. It does not filter forwarded or outgoing traffic; other firewall rules and system settings may still affect them. It is not a router or container firewall.

Step 1   Prepare access and a backup

Confirm that the recovery console works. Identify your real SSH port and any required inbound services. This example uses TCP port 22; replace it before loading if yours differs. It does not start SSH or open a provider firewall. Save a copy of your existing configuration without overwriting a previous backup:

Terminal
sudo cp -an /etc/nftables.conf /etc/nftables.conf.before-uf-guide
sudo ls -l /etc/nftables.conf.before-uf-guide

The backup preserves the file, not the complete firewall state. If the backup already existed, inspect it and make a separate dated backup before continuing. Do not proceed if the uf_guard table already exists.

Step 2   Edit the configuration

Only on the dedicated, clean host described above, open the file. Replace its contents with the example below after reviewing its original contents and includes. Save with Ctrl+O, Enter, then exit with Ctrl+X:

Terminal
sudo nano /etc/nftables.conf
What this example does

Use this only on the dedicated host described above, without another firewall manager or existing rules. It applies to IPv4 and IPv6.

  • Allows: loopback traffic, established or related connections, IPv4 ICMP and IPv6 ICMP, and TCP port 22 for SSH.
  • Blocks: invalid connections and all other incoming traffic not accepted by a rule. Change port 22 in the example if SSH uses another port, and add any other required services before loading it.
  • Outgoing and forwarded traffic: this example has no output or forward chain, so it does not filter that traffic. Other firewall layers and system settings can still affect it.
/etc/nftables.conf
table inet uf_guard
flush table inet uf_guard

table inet uf_guard {
    chain input {
        type filter hook input priority filter; policy drop;
        iifname "lo" accept
        ct state established,related accept
        ct state invalid drop
        ip protocol icmp accept
        meta l4proto ipv6-icmp accept
        tcp dport 22 accept
    }
}

The inet table applies to IPv4 and IPv6. ICMP is allowed for connectivity and network error messages, including IPv6 neighbor discovery. The first two lines create the named table if needed and clear its rules when reloading; they do not flush the entire ruleset. Every other incoming port is dropped by this chain. Review IPv6 exposure as well as IPv4.

Step 3   Check and load

Check the file first. A successful check normally prints nothing. It cannot prove that the rules fit your network or preserve access:

Terminal
sudo nft -c -f /etc/nftables.conf

Only if the check succeeds and the console is ready, apply the file in one transaction:

Terminal
sudo nft -f /etc/nftables.conf
sudo nft list ruleset

In the output, look for table inet uf_guard, its input chain and policy drop, followed by the rules you configured. This confirms what is listed, not that SSH works or that other firewall layers are correct.

Keep the original session open. From a second terminal or another computer, establish a new SSH connection using the real host and port. Test required services over both address families where applicable. If the test fails, use the recovery console below. Do not enable persistence until access works.

4. Allow a Web Service

If this host actually serves HTTP and HTTPS, add the following line inside the input chain, before its closing brace. Opening a port exposes a listening service to reachable networks; use only the ports you need:

Rule to add
tcp dport { 80, 443 } accept

Save the file, run sudo nft -c -f /etc/nftables.conf, then apply it with sudo nft -f /etc/nftables.conf only if the check succeeds. Verify the service from another machine and test a new SSH login again. Other firewall layers can still block traffic even when this chain accepts it.

5. Keep Rules After a Restart

Once the example works and the saved configuration has been reviewed, enable the service for future boots:

Terminal
sudo systemctl enable nftables.service
systemctl is-enabled nftables.service

The service loads /etc/nftables.conf at boot. Enabling it does not itself apply the file now; the previous step did that. On a later reboot, verify the active rules and test a new connection. Ensure no other service changes the firewall during boot. Direct nft commands change the running rules only; edit and reload the saved file to make intended changes persistent.

Troubleshooting and Recovery

1   Syntax check fails

Read the reported file and line. Check braces, semicolons and your SSH port. Do not load a file that fails the check. A successful check is not a connectivity test.

2   New SSH connection fails

Keep the existing session open. Check the SSH listener, port, provider firewall and the active rules. Use the provider or local console if you cannot log in.

Recovery for this example only: From the local or provider console, remove the tutorial table and disable its boot service. This removes the example firewall protection, so restore a suitable firewall promptly. Do not use these commands if another configuration owns the table or service:

Terminal
sudo nft delete table inet uf_guard
sudo systemctl disable nftables.service

Deleting uf_guard removes this example from the running rules immediately. Disabling the service prevents its automatic start at boot; disabling it alone does not remove active rules. Check access using a fresh SSH connection while keeping the console or original session open.

To restore persistent protection, inspect the saved configuration and any included files, correct the SSH port and required services, and check the intended file with sudo nft -c -f /etc/nftables.conf. With recovery access ready, apply the reviewed policy, test a fresh SSH connection and required services, and only then re-enable boot loading or reboot. A backup of a config file does not recreate other managers’ rules. Avoid nft flush ruleset: it removes all nftables tables, including rules belonging to other software.

Update or Remove nftables

To receive Ubuntu package updates, refresh the package list and upgrade the package:

Terminal
sudo apt update
sudo apt install --only-upgrade nftables

Before removal, arrange and verify replacement protection if needed. Restrict the following removal steps to the dedicated example host. Package removal can stop the nftables service, whose stop action may flush the entire nftables ruleset, including rules managed by other software. On shared hosts, plan removal with the administrator and inspect the service unit first. On the dedicated example host, use the recovery commands above to remove the example table and disable boot loading. Confirm what remains with sudo nft list ruleset; do not delete tables owned by other software.

1   Review package dependencies

APT shows packages it would remove. Stop if another tool or service needs nftables:

Terminal
apt -s remove nftables
2   Remove the package

After reviewing the simulation and arranging replacement protection, remove the package:

Terminal
sudo apt remove nftables

Removal is not a guarantee that every active kernel rule or saved configuration is gone. The remove operation normally retains configuration files. Do not purge or delete backups until you have checked what you need.

Helpful Resources

Read the Ubuntu nftables documentation and the nftables atomic rule replacement guide.

Frequently Asked Questions

Does installing nftables activate a firewall?

Installing the tool alone does not establish a useful firewall policy. Review the current rules and service configuration before loading or enabling anything.

Can I use UFW and native nftables rules together?

Ubuntu recommends choosing one firewall management approach. Mixing them can create unexpected interactions, including blocked traffic.

Are nftables rules saved automatically?

Changes made directly with nft affect the running rules. Save the intended configuration and arrange for it to load at boot.

Does the example protect IPv6?

The inet input chain processes both IPv4 and IPv6 traffic, but test both paths and review other firewall layers and services.

Will removing the package remove every firewall rule?

Do not assume so. Inspect active rules and the configuration of other firewall managers, and arrange replacement protection before removal.

Related guides: Install Ubuntu Server | Install a LAMP stack | Install a LEMP stack.