nftables
- User
nftables
- Assistant
nftablesis a subsystem of the Linux kernel that provides packet filtering, network address translation (NAT), and other packet mangling. It is intended to replace the olderiptables,ip6tables,arptablesandebtablesframeworks. The main advantage ofnftablesover its predecessors is that it offers a more consistent syntax and improved performance. It uses a small in-kernel virtual machine that processes a pseudo-instruction set, which allows it to perform various operations on packets.Key features of
nftables:- Simplified and unified syntax for all protocols (IPv4, IPv6, ARP, etc.).
- Improved performance due to reduced context switches between user-space and kernel-space.
- Support for inet family, which allows to create rules that can process both IPv4 and IPv6 traffic.
- Better support for dynamic updates of rules without the need to reload the entire table.
- Enhanced error reporting for easier debugging of rules.
- Support for sets and maps, allowing efficient management of large sets of data (such as IP addresses, ports, etc.).
- Event-based interface that allows monitoring of changes to rules.
Here's an example of how to use
nftablesto create a simple firewall rule that allows SSH traffic:# Start by adding a new table for filtering sudo nft add table inet mytable # Now add a chain to the table where policy is to drop packets by default sudo nft add chain inet mytable input { type filter hook input priority 0 \; policy drop \; } # Finally, add a rule to accept SSH traffic (port 22) sudo nft add rule inet mytable input tcp dport 22 acceptThis set of commands creates a new table called
mytablein theinetfamily, adds a chain calledinputto that table with a default policy to drop all incoming packets, and finally adds a rule to accept incoming packets on TCP port 22, which is the default port for SSH.To list the current
nftablesconfiguration, use:sudo nft list rulesetTo save the current
nftablesruleset so that it persists across reboots, you may need to use a specific service or command depending on your Linux distribution.# For systems that use systemd, it's often done as a service sudo systemctl enable nftables.service sudo systemctl start nftables.service # Or by using the nftables command itself sudo nft list ruleset > /etc/nftables.confThe
nftablestool is continually being updated and improved, so it is advisable to consult the latest official documentation or the man pages (man nft) for the most up-to-date information.