
nftables is a modern packet filtering framework for Linux that replaces iptables. It offers a unified and efficient way to manage firewall rules across multiple protocols like IPv4, IPv6, and ARP. To get started with it, you need to install and configure nftables in Linux, which provides better performance and a simpler configuration through a single interface instead of multiple tables like iptables.
This article explains how to install and configure nftables on Linux. You will set up basic firewall rules, manage network traffic, and implement common security policies.
Before you begin, you need to:
nftables versus iptablesiptables works as the standard firewall management tool on Linux. However, Its limitations included a complex rule structure, performance inefficiencies, and the need for multiple tools to handle different protocols (IPv4, IPv6, ARP, and so on). To address these issues, nftables was introduced as a replacement, offering significant improvements in functionality, performance, and usability.
This section compares nftables and iptables, highlighting key differences in functionality, performance, and usability to explain why nftables is the preferred choice.
nftables on LinuxFollow the steps below to install nftables depending on your Linux distribution.
Follow the steps below to install nftables package on Ubuntu and Debian.
Update the server's package index.
Install nftables using the APT package manager.
nft Command Syntax-n: Show rule numbers in output.-N: Print more detailed output for rules.-s: Show statistics for rules.-c: Show chain name for each rule.-a: Print the rule with its address family and protocol.-e: Expand rule details.-S: Print the ruleset (configuration).-u: Enable update mode for dynamic changes.-p: Print the current ruleset in a parsable format.-y: Show detailed object types.-j: Output in JSON format.-t: Show output in table format.-T: Show tokenized output.-I directory: Use the specified directory for configuration files.-f filename: Load rules from a file.-i: Interactive mode to modify rules directly.cmd ...: Command arguments to manipulate the nftables rules.nftables System ServiceThe nftables service manages your firewall rules and ensures persistence across system reboots. Follow these steps to enable and start the nftables service on your system.
Enable the nftables service to start at boot.
Start the nftables service.
Verify the service status.
Your output should be similar to the one below:
nftablesIn nftables, rules are organized into tables, which contain chains that define how traffic is processed. Each chain is associated with a hook (INPUT, OUTPUT, FORWARD) that determines when it is evaluated. Chains also have policies that define the default action when no explicit rule matches a packet.
nftables supports multiple address families for handling different network protocols:
inet: Unified handling for both IPv4 and IPv6.ip: IPv4-only filtering.ip6: IPv6-only filtering.arp: ARP packet filtering.bridge: Ethernet bridge packet filtering.netdev: Packet filtering at the network device level.Each chain in nftables is linked to a specific traffic type:
input: Controls packets destined for the local machine.output: Manages packets leaving the machine.forward: Handles packets routed through the machine.Policy refers to the default action taken by a chain when no explicit rule matches a packet. The default policy for a chain determines whether traffic is accepted, dropped, or rejected when it doesn't match any specific rule.
policy accept - Allows all traffic by default (useful for testing or initial configurations).policy drop - It discards unmatched packets (most secure, commonly used in production environments).policy reject - Sends a rejection notice to the source (informative but reveals the presence of the server, which might not be ideal for security).Using accept as the default policy is not recommended for production environments as it allows all traffic unless explicitly blocked.
nftables ConfigurationFollow the steps below to explore the default configurations on your server.
View the list of existing nftables rules.
Your output should be similar to the one below:
View the default nftables configuration file.
Your output should be similar to the one below:
nftables Firewall RulesFollow the steps below to configure and manage nftables rules to control network traffic on your Linux machine.
Create a new firewall table for IPv4 and IPv6 traffic.
The above command creates a new table named my_table, that can process both IPv4 and IPv6 packets. Please replace my_table with your preferred table name. The inet family allows you to manage both protocols with a single set of rules.
In this article my_table table name is used. Please replace my_table with your preferred table name.
Add an input chain with a default accept policy.
The above command creates a filtering chain that applies to incoming traffic. The type filter option designates it as a filtering chain, while hook input ensures it processes incoming packets. The priority 0 setting determines the execution order, with lower values running first.
Create forward chain with default accept policy.
The above command creates a filtering chain that applies to forwarded traffic. The type filter option designates it as a filtering chain, while hook forward ensures it processes forwarded packets. The priority 0 setting determines the execution order, with lower values running first.
Allow established connections to keep existing communications.
This rule uses connection tracking to manage traffic. The ct state option enables connection tracking, while established allows packets from existing connections.
Follow the steps below to add, list, and delete nftables rules to control network access.
Add a rule to block incoming traffic from a specific IP address:
The above command adds a rule to the my_table table, blocking incoming traffic from the IP address 192.0.2.10. The rule is applied to the input chain in the inet family.
View all active nftables rules to verify configurations.
The above command lists all active nftables rules to verify your configurations.
List the rules with handle numbers.
The above command lists the rules in the input chain of the my_table table, so you can reference a specific rule for removal.
Your output should be similar to the one below:
Identify the handle number from the output and delete the corresponding rule.
The above command deletes the rule with the handle number 0 from the input chain of the my_table table.
Allow SSH access to your server.
The above command adds a rule that matches the TCP protocol packets and targets to the destination port 22 (the default SSH port). It accepts matching packets, allowing SSH connections.
Block a specific IP address that shows suspicious activity.
The above command adds a rule that matches the packets from the specified source IP address, drops them without notifying the sender, and prevents any access from that IP address.
Allow HTTP traffic on port 80.
The above command adds a rule that matches TCP protocol packets targeting port 80 (the default HTTP port). It accepts matching packets, allowing web traffic to reach the server.
Allow HTTPS traffic on port 443.
The above command adds a rule allows TCP traffic on port 443 (default for HTTPS). It ensures encrypted web traffic is allowed while blocking other types of traffic on this port.
Drop all incoming other traffic.
The above command adds a rule that drops all incoming traffic by default while allowing only the preconfigured rules (SSH, HTTP, HTTPS). It effectively blocks unwanted access.
Enable NAT functionality in the Linux kernel by editing the /etc/sysctl.conf file using any text editor such as vim.
Uncomment the following line.
Save and close the file.
Apply the changes.
Create a new NAT table to handle network address translation (NAT) for your server.
The above command creates a new nat table, where you can define NAT rules for translating addresses in outgoing traffic.
Create a postrouting chain in the NAT table to process packets after routing.
Add a masquerading rule for your internal network to enable internet connection sharing.
This rule tells nftables to perform NAT (masquerading) for outgoing traffic from the 192.0.2.0/24 network. The oif enp1s0 part specifies that the rule applies to traffic leaving the enp1s0 interface, which is typically the network interface connected to the internet.
In this section, you will save nftables rules persistently across system reboots by saving them to the configuration file.
Save your current firewall configuration to the /etc/nftables.conf file.
The above command stores the current nftables rules to the /etc/nftables.conf file, ensuring that they can be restored after a reboot.
Apply saved rules from the configuration /etc/nftables.conf file.
Reloads the firewall rules from the saved configuration file (/etc/nftables.conf).
Restart the nftables service.
This reloads the firewall rules and ensures they persist after reboots.
Implement rate limiting for SSH connections.
The above command adds a rule that targets SSH traffic (port 22), limits connections to 3 per minute, and accepts connections within the limit to help prevent brute force attacks.
Configure port forwarding for web traffic.
Create the prerouting chain in the nat table (if it doesn't exist).
Add the port forwarding rule.
The above command adds a rule that captures incoming HTTP traffic (port 80), forwards it to an internal server (192.0.2.10), and enables hosting internal web services.
Enable logging for dropped packets.
The above command adds a rule that logs packets before dropping them, adds a custom prefix for easy filtering, helps troubleshoot connection issues, and stores logs in the system journal.
Flush all the nftables rules.
Be cautious when flushing the ruleset, as this will remove all existing nftables rules, effectively resetting your firewall configuration
You have installed and configured nftables on Linux. You created tables, set up chains with different policies, and managed firewall rules. The nftables framework modernizes packet filtering by providing unified management of IPv4 and IPv6 traffic, atomic rule updates, and improved performance through optimized packet processing. With built-in features like connection tracking and advanced logging capabilities, nftables simplifies the complex task of network security management. Visit the official nftables wiki for more information and advanced configurations.
0 Comments
Be the first to comment and share your perspective with the community.