Skip to content
Log In Sign Up info@muvcloud.com

Before You Load Rules

A drop policy without an SSH rule cuts your connection the moment it loads. Allow SSH on the port it really uses, keep your session open, find the server console in your provider's control panel, and schedule a rollback first:

sudo sh -c 'echo "flush ruleset" > /root/nftables-backup.nft; nft list ruleset >> /root/nftables-backup.nft'
echo 'nft -f /root/nftables-backup.nft' | sudo at now + 5 minutes

The backup starts with flush ruleset, and nft -f applies a whole file as one transaction, so the restore replaces the new rules in a single step. If you lose access, the job restores the old rules within five minutes. Once a new SSH login works from a second terminal, cancel it: sudo atq shows the job number, and sudo atrm JOB_NUMBER removes it. at needs the at package and the atd service, installed and started as in iptables basics.

nftables, iptables-nft, and Front Ends

  • iptables-nft: on current distributions the iptables command writes nftables rules underneath (iptables -V shows (nf_tables)), and they appear in nft list ruleset as tables such as ip filter. See iptables basics.
  • ufw and firewalld: front ends that own the ruleset while active; change rules through them, as in how to use firewalld, not with nft.
  • One tool: choose ufw, firewalld, iptables, or a plain nftables file, and use only that one.

See the Current Rules

sudo nft list ruleset
sudo nft list tables
sudo nft -a list chain inet filter input

-a adds a handle number to each rule; you need it to delete a single rule.

Tables, Chains, and Rules

  • Table: a container with a family. ip is IPv4, ip6 is IPv6, and inet covers both, so one table protects both protocols.
  • Base chain: a chain attached to a hook such as input, with a priority and a policy, accept or drop, for packets no rule matched.
  • Rule: a match, such as tcp dport 22, and a verdict, such as accept. Rules are read from the top.

A Basic Server Ruleset

This file allows loopback, replies to the server's own connections, ICMP and ICMPv6, SSH, HTTP, and HTTPS, and drops all other incoming traffic, for IPv4 and IPv6 at once:

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

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

Replace 22 with your SSH port. Because the file is loaded as one transaction, the drop policy and the SSH and established rules take effect together, which is safe. Typing the same chain as single nft commands is not: a chain with a drop policy cuts your session before the next command adds the SSH rule. The forward chain drops routed traffic, which is right for a server that does not route; see the mistakes below for container and VPN hosts.

Check, Load, and Enable at Boot

SystemRules file
Ubuntu, Debian/etc/nftables.conf
AlmaLinux, Rocky Linux/etc/sysconfig/nftables.conf

On AlmaLinux and Rocky Linux that file mostly holds commented include lines: save the ruleset as, for example, /etc/nftables/server.nft, add include "/etc/nftables/server.nft" to the file, and stop firewalld with sudo systemctl disable --now firewalld so that only one tool manages the rules. Then, with your system's path:

sudo nft -c -f /etc/nftables.conf
sudo nft -f /etc/nftables.conf
sudo systemctl enable nftables

-c checks the file without loading it. The second command loads it; test a new SSH login, then cancel the rollback job. The last loads the file at every boot. Install the nftables package first if it is missing.

Add and Delete Rules While Running

sudo nft add rule inet filter input tcp dport 8080 accept
sudo nft -a list chain inet filter input
sudo nft delete rule inet filter input handle 12

add appends a rule, insert places it at the top. Such changes are lost at reboot; put them in the rules file too.

Common Mistakes

  • flush ruleset on a Docker host: it deletes every table, including the ones Docker, libvirt, or a VPN created, and the forward drop policy breaks their traffic. On such hosts, manage only your own table and leave the forward chain out.
  • No ICMPv6: without it, IPv6 neighbor discovery fails and the server loses IPv6 connectivity.

Afterward, confirm from another machine which ports answer: how to check open ports in Linux.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations