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 minutesThe 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_ 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 -Vshows(nf_), and they appear intables) nft list rulesetas tables such asip 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.
ipis IPv4,ip6is IPv6, andinetcovers both, so one table protects both protocols. - Base chain: a chain attached to a hook such as
input, with a priority and a policy,acceptordrop, for packets no rule matched. - Rule: a match, such as
tcp dport 22, and a verdict, such asaccept. 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
| System | Rules file |
|---|---|
| Ubuntu, Debian | /etc/ |
| AlmaLinux, Rocky Linux | /etc/ |
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 12add 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.
More Guides
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.

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