Hey, I'd like to point out right away that this tutorial is not mine - I received it in the mail and thought it would also be useful for members of the Qubes community.
The article comes from this site.
Nftables appeared in version 3.13 of the Linux kernel, which was released in January 2014. Like UFW and firewalld, nftables is used to configure a firewall. It is the most difficult to configure firewall of all the previously mentioned, so using it is recommended for those with Linux experience (and angelic patience).
Nftables should be installed by default in most Linux distributions. However, if it is not installed, you can use the command:
sudo apt install nftables
Nftables are managed via the nft command. To view the instruction associated with this command, type man nft in the console. It is worth noting that the provided instruction is quite extensive - it has almost 4000 lines.
Nftables is based primarily on configuration files (*.conf), which can be created through commands using nft. You can define many rules described in tables, which are based on protocols.
Nftables has a different syntax from iptables, but the commands can be translated from iptables to nftables quite simply. If we want to use a command from iptables in nftables, we can use the iptables-nft command. For example - we want to block traffic on port 80 for packets that come from the address 192.168.25.150.
sudo iptables -A INPUT -p tcp --source 192.168.25.150 --dport 80 -j DROP
sudo iptables-nft -A INPUT -p tcp --source 192.168.25.150 --dport 80 -j DROP
Gentoo Linux Wiki contains some practical examples of nftables configuration. They are available here and I recommend you take a look at them J.
Here is an example of a basic firewall configuration for a workstation.
#!/sbin/nft -f
flush ruleset
table ip filter {
# allow all packets sent by the firewall machine itself
chain output {
type filter hook output priority 100; policy accept;
}
# allow LAN to firewall, disallow WAN to firewall
chain input {
type filter hook input priority 0; policy accept;
iifname "lan0" accept
iifname "wan0" drop
}
# allow packets from LAN to WAN, and WAN to LAN if LAN initiated the connection
chain forward {
type filter hook forward priority 0; policy drop;
iifname "lan0" oifname "wan0" accept
iifname "wan0" oifname "lan0" ct state related,established accept
}
}
sudo nft --file [file name]
ArchLinux Wiki has a fairly simple and easy-to-read guide related to the nftables command, which is available here
nft list ruleset
- Displays the current set of rulesnft flush ruleset
- Removing rules, may leave device without working firewallnft list tables
- Displays the currently defined tables in the systemnft add table [family name] [table name]
- Creates a table for a given family (options - ip, arp, etc.) with a given nameNftables is quite a complex tool and it is no mean feat to describe its functionalities in a short article. That's why I encourage you to test different nftables configurations in a controlled environment in order to get most familiar with the tool. The fact that it is quite easy to translate rules from iptables may help many people to "switch" to nftables.