Network

The nft command

Manages nftables firewall rules, the modern replacement for iptables.

Syntax

nft [opciones] {orden | -f archivo}Try it in the terminal →

Most useful options

-f, --file ARCHIVOReads the commands from a file. With - it reads from standard input
-c, --checkChecks the commands without applying them, to review a file before loading it
-a, --handleShows the handle of each rule, which you need to delete it
-n, --numericShows addresses and ports numerically, without resolving names
-j, --jsonPrints the output in JSON, to process it with a script
-e, --echoShows what was added, removed or replaced after applying a command

Copy-ready examples

$ sudo nft list rulesetshows every table, chain and rule loaded in the firewall.
$ sudo nft -a list rulesetthe same, with the handle of each rule so you can delete it later.
$ sudo nft -c -f reglas.nftchecks the syntax of a rules file without applying it.
$ sudo nft add rule inet filter input tcp dport 22 acceptCAREFUL: it changes the live firewall. If the default policy blocks traffic, you can lose SSH access to a remote machine. It requires the inet filter table and its input chain to exist.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
Error: Could not process rule: No such file or directoryThe table or chain you name does not exist yet.Create them with nft add table and nft add chain, or load the whole ruleset with nft -f.

Typical mistakes

  • Rules loaded with nft live in memory: they are lost on reboot unless you saved them to a file that is loaded at boot.
  • add rule fails if the table or the chain does not exist: create them first, or load the whole ruleset with nft -f.
  • flush ruleset deletes every rule at once, including the remote access ones. Do not run it on a machine without knowing what is loaded.

Differences from similar commands

  • iptables: uses fixed tables (filter, nat...) with its own command per protocol. nft handles everything with one tool and an inet family that covers IPv4 and IPv6.
  • ufw: is a simple command layer over the firewall. nft writes the rules directly, without that layer.
  • firewall-cmd: manages firewalld, with zones and services. nft works directly with tables, chains and rules.

Security

A firewall change can close SSH access or open ports to the outside. Check with -c before applying, and keep a local console at hand in case you get locked out.

Related commands

FAQ

How do I delete a single rule?

List with sudo nft -a list ruleset to see its handle, then delete it with sudo nft delete rule inet filter input handle N, using the number the list shows.

What is the inet family?

A table family that serves IPv4 and IPv6 at the same time, so you do not repeat the same rules with ip and ip6.

How do I save my rules to load them again?

Dump the set with sudo nft list ruleset > rules.nft and load it later with sudo nft -f rules.nft. According to the manual, the output of list ruleset can be used as input to nft -f.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course