Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

nftables

  1. User

    nftables

  2. Assistant

    nftables is a subsystem of the Linux kernel that provides packet filtering, network address translation (NAT), and other packet mangling. It is intended to replace the older iptables, ip6tables, arptables and ebtables frameworks. The main advantage of nftables over 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:

    1. Simplified and unified syntax for all protocols (IPv4, IPv6, ARP, etc.).
    2. Improved performance due to reduced context switches between user-space and kernel-space.
    3. Support for inet family, which allows to create rules that can process both IPv4 and IPv6 traffic.
    4. Better support for dynamic updates of rules without the need to reload the entire table.
    5. Enhanced error reporting for easier debugging of rules.
    6. Support for sets and maps, allowing efficient management of large sets of data (such as IP addresses, ports, etc.).
    7. Event-based interface that allows monitoring of changes to rules.

    Here's an example of how to use nftables to 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 accept
    

    This set of commands creates a new table called mytable in the inet family, adds a chain called input to 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 nftables configuration, use:

    sudo nft list ruleset
    

    To save the current nftables ruleset 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.conf
    

    The nftables tool 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.