[Tutoriall 4.2] DomU in 4.2 firewalls have completely switched to nftables - a modern firewall for advanced users revisions

Go back to topic: [Tutoriall 4.2] DomU in 4.2 firewalls have completely switched to nftables - a modern firewall for advanced users

  1. v6 anchor; v6 full version
  2. v5 anchor; v5 full version
  3. v4 anchor; v4 full version
  4. v3 anchor; v3 full version
  5. v2 anchor; v2 full version

Revision #6

Edited on
2023-10-20
Edited by user
Pawelek85
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.](https://pushsec.pl/nftables-czyli-nowoczesny-firewall-dla-zaawansowanych-uzytkownikow/)** <br><br>Hi, 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. <h5> Summary: nfttables w Qubes 4.2: </h5> <h6> * [[4.2 changelog]:](https://www.qubes-os.org/doc/releases/4.2/release-notes/) **DomU firewalls have completely switched to nftables.** ([#5031](https://github.com/QubesOS/qubes-issues/issues/5031), [#6062](https://github.com/QubesOS/qubes-issues/issues/6062)) * > @Sven : [Firewall works differently in R4.2. You are using a pre-release version. Instructions that worked with R4.1 will no longer work with R4.2.](https://forum.qubes-os.org/t/4-2-leakage-of-network-traffic-from-vm-any-joke/21125/20) * [Qubes OS 4.2 nftables / nft firewall guide](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933) </h6>
> **The article comes from [this site.](https://pushsec.pl/nftables-czyli-nowoczesny-firewall-dla-zaawansowanych-uzytkownikow/)**
Nftables 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. <h5> nfttables w Qubes 4.2: </h5> * [[4.2 changelog]:](https://www.qubes-os.org/doc/releases/4.2/release-notes/) **DomU firewalls have completely switched to nftables.** ([#5031](https://github.com/QubesOS/qubes-issues/issues/5031), [#6062](https://github.com/QubesOS/qubes-issues/issues/6062)) * [Qubes OS 4.2 nftables / nft firewall guide](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933) Nftables 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.

Revision #5

Edited on
2023-10-20
Edited by user
Pawelek85
Pro tip: When experimenting with firewall rules, here is a practical method: * run `nft list ruleset | tee original_ruleset | tee new_ruleset` * add `flush ruleset` at the top of both file, that will clear all the rules before loading the new rules * make changes to `new_ruleset` for your experimentations * load the new ruleset with `nft --file new_ruleset` * rollback with `nft --file original_ruleset` when you want to revert all changes (if you do that on a remote server using ssh, make sure to not lock you out, you may want to run `sleep 3600 && nft flush ruleset` to disable all rules after 1h if you don’t stop the timer, or load the original ruleset :wink: instead of flushing everything temporarily) From @solene: > Pro tip: When experimenting with firewall rules, here is a practical method: > > * run `nft list ruleset | tee original_ruleset | tee new_ruleset` > * add `flush ruleset` at the top of both file, that will clear all the rules before loading the new rules > * make changes to `new_ruleset` for your experimentations > * load the new ruleset with `nft --file new_ruleset` > * rollback with `nft --file original_ruleset` when you want to revert all changes > > (if you do that on a remote server using ssh, make sure to not lock you out, you may want to run `sleep 3600 && nft flush ruleset` to disable all rules after 1h if you don’t stop the timer, or load the original ruleset :wink: instead of flushing everything temporarily)
<h1>5. [4.2] instructions for configuring nftables in Qubes </h1> [link](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933) <h1>5. [4.2] configuring nftables in Qubes </h1> [Qubes OS 4.2 nftables / nft firewall guide](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933) by @solene
<h5> nfttables w Qubes 4.2: </h5> * [[4.2 changelog]:](https://www.qubes-os.org/doc/releases/4.2/release-notes/) **DomU firewalls have completely switched to nftables.** ([#5031](https://github.com/QubesOS/qubes-issues/issues/5031), [#6062](https://github.com/QubesOS/qubes-issues/issues/6062)) * [Qubes OS 4.2 nftables / nft firewall guide](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933)
* ArchLinux Wiki has a fairly simple and easy-to-read guide related to the nftables command, which is available [here](https://wiki.archlinux.org/title/nftables)* [ArchLinux Wiki](https://wiki.archlinux.org/title/nftables) has a fairly simple and easy-to-read guide related to the nftables command, which is available [here](https://wiki.archlinux.org/title/nftables)

Revision #4

Edited on
2023-10-20
Edited by user
Pawelek85
* **prerouting** - packets that have reached the device and have not been processed by nftables, * **input** - packets that have reached the device and have been processed by nftables, * **forward** - packets to be forwarded to another device, * **output** - outgoing packets, * **postrouting** - outgoing packets that have been processed by nftables. Pro tip: When experimenting with firewall rules, here is a practical method: * run `nft list ruleset | tee original_ruleset | tee new_ruleset` * add `flush ruleset` at the top of both file, that will clear all the rules before loading the new rules * make changes to `new_ruleset` for your experimentations * load the new ruleset with `nft --file new_ruleset` * rollback with `nft --file original_ruleset` when you want to revert all changes (if you do that on a remote server using ssh, make sure to not lock you out, you may want to run `sleep 3600 && nft flush ruleset` to disable all rules after 1h if you don’t stop the timer, or load the original ruleset :wink: instead of flushing everything temporarily)
<h1>5. Examples of nftables configuration in practice</h1> [Gentoo Linux Wiki](https://wiki.gentoo.org/wiki/Main_Page) contains some practical examples of nftables configuration. [Here](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933) See the instructions for configuring nftables in Qubes 4.2 <h1>5. [4.2] instructions for configuring nftables in Qubes </h1> [link](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933)
Nftables 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.Nftables 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. <h5> Additional valuable learning resource nftables </h5> * [Gentoo Linux Wiki](https://wiki.gentoo.org/wiki/Main_Page) contains some practical examples of nftables configuration. * ArchLinux Wiki has a fairly simple and easy-to-read guide related to the nftables command, which is available [here](https://wiki.archlinux.org/title/nftables)

Revision #3

Edited on
2023-10-20
Edited by user
Pawelek85
The article comes from [this site.](https://pushsec.pl/nftables-czyli-nowoczesny-firewall-dla-zaawansowanych-uzytkownikow/) ------------- > **The article comes from [this site.](https://pushsec.pl/nftables-czyli-nowoczesny-firewall-dla-zaawansowanych-uzytkownikow/)** -------------
<h4>The command in iptables will look as follows:</h4> <h4>The command in iptables will look as follows:</h4>
<br><h4>To add them to nftables, use the command:</h4> <br><h4>To add them to nftables, use the command:</h4>
[Gentoo Linux Wiki](https://wiki.gentoo.org/wiki/Main_Page) contains some practical examples of nftables configuration. They are available here and I recommend you take a look at them J. [Here](https://wiki.gentoo.org/wiki/Nftables/Examples) is an example of a basic firewall configuration for a workstation. <h4> The configuration consists of a few simple assumptions:</h4> 1. Packets sent by the firewall host machine are allowed. 2. Incoming packets from the LAN are allowed. 3. Incoming packets from the WAN are discarded unless the LAN device initiated the connection. 4. Outgoing packets from the LAN are allowed. <h4>The configuration is as follows: </h4> ``` #!/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 } } ``` The configuration should be saved to a file. For the firewall to work properly, you need to enable packet forwarding from the WAN and LAN with the following command: **sysctl -w net.ipv4.ip_forward = 1** <br>To load the configuration from a file, use the command: **`sudo nft --file [file name]`** [Gentoo Linux Wiki](https://wiki.gentoo.org/wiki/Main_Page) contains some practical examples of nftables configuration. [Here](https://forum.qubes-os.org/t/qubes-os-4-2-nftables-nft-firewall-guide/20933) See the instructions for configuring nftables in Qubes 4.2
ArchLinux Wiki has a fairly simple and easy-to-read guide related to the nftables command, which is available [here](https://wiki.archlinux.org/title/nftables) <h3>Highlights include: </h3> * **`nft list ruleset`** - Displays the current set of rules * **`nft flush ruleset`** - Removing rules, may leave device without working firewall * **`nft list tables`** - Displays the currently defined tables in the system * **`nft add table [family name] [table name]`** - Creates a table for a given family (options - ip, arp, etc.) with a given name ArchLinux Wiki has a fairly simple and easy-to-read guide related to the nftables command, which is available [here](https://wiki.archlinux.org/title/nftables) **nft list ruleset** - Displays the current set of rules **nft flush ruleset** - Removing rules, may leave device without working firewall **nft list tables** - Displays the currently defined tables in the system **nft add table [family name] [table name]** - Creates a table for a given family (options - ip, arp, etc.) with a given name

Revision #2

Edited on
2023-10-20
Edited by user
Pawelek85
**1. Introduction** <h1>1. Introduction </h1>
**2. Installing** <h1>2. Installing</h1>
`sudo apt install nftables` ![nft install|441x186](upload://fC7HJJ7PkCBSFsVnvLfT0QWC1nM.png) **`sudo apt install nftables`** ![nft install|441x186](upload://fC7HJJ7PkCBSFsVnvLfT0QWC1nM.png)
**3. Why choose nftables?** <h1>3. Why choose nftables?</h1>
The possible families (protocol-related options) are as follows: * List item * ip - intended for IP version 4, * ipv6 - intended for IP version 6, * inet - intended for IP versions 4 and 6, * arp - intended for ARP, * bridge - intended for connections using a bridge. Nftables also has three types of chain. They are: * filter - a chain intended for packet filtering, * nat - a chain intended for NAT, or address translation, * route - a chain intended for routing packets. We can also specify the range of packets to which the rules apply as follows: * prerouting - packets that have reached the device and have not been processed by nftables, * input - packets that have reached the device and have been processed by nftables, * forward - packets to be forwarded to another device, * output - outgoing packets, * postrouting - outgoing packets that have been processed by nftables. **4. Relatedness and differences with iptables - syntax** <h4>The possible families (protocol-related options) are as follows:</h4> * **ip** - intended for IP version 4, * **ipv6** - intended for IP version 6, * **inet** - intended for IP versions 4 and 6, * **arp** - intended for ARP, * **bridge** - intended for connections using a bridge. <h4>Nftables also has three types of chain. They are:</h4> * **filter** - a chain intended for packet filtering, * **nat** - a chain intended for NAT, or address translation, * **route** - a chain intended for routing packets. <br><h4>We can also specify the range of packets to which the rules apply as follows:</h4> * **prerouting** - packets that have reached the device and have not been processed by nftables, * **input** - packets that have reached the device and have been processed by nftables, * **forward** - packets to be forwarded to another device, * **output** - outgoing packets, * **postrouting** - outgoing packets that have been processed by nftables. <h1>4. Relatedness and differences with iptables - syntax</h1>
The command in iptables will look as follows: <h4>The command in iptables will look as follows:</h4>
To add them to nftables, use the command: <br><h4>To add them to nftables, use the command:</h4>
**5. Examples of nftables configuration in practice** <h1>5. Examples of nftables configuration in practice</h1>
[Here](https://wiki.gentoo.org/wiki/Nftables/Examples) is an example of a basic firewall configuration for a workstation. The configuration consists of a few simple assumptions: 1. Packets sent by the firewall host machine are allowed. 2. Incoming packets from the LAN are allowed. 3. Incoming packets from the WAN are discarded unless the LAN device initiated the connection. 4. Outgoing packets from the LAN are allowed. The configuration is as follows: [Here](https://wiki.gentoo.org/wiki/Nftables/Examples) is an example of a basic firewall configuration for a workstation. <h4> The configuration consists of a few simple assumptions:</h4> 1. Packets sent by the firewall host machine are allowed. 2. Incoming packets from the LAN are allowed. 3. Incoming packets from the WAN are discarded unless the LAN device initiated the connection. 4. Outgoing packets from the LAN are allowed. <h4>The configuration is as follows: </h4>
**`sysctl -w net.ipv4.ip_forward = 1`** To load the configuration from a file, use the command: **sysctl -w net.ipv4.ip_forward = 1** <br>To load the configuration from a file, use the command:
**6. Useful commands - quick sheet** ArchLinux Wiki has a fairly simple and easy-to-read guide related to the nftables command, which is available [here.](https://wiki.archlinux.org/title/nftables) Highlights include: `**nft list ruleset**` - Displays the current set of rules `**nft flush ruleset**` - Removing rules, may leave device without working firewall `**nft list tables**` - Displays the currently defined tables in the system `**nft add table [family name] [table name].**` - Creates a table for a given family (options - ip, arp, etc.) with a given name **7. Summary** <h1>6. Useful commands - quick sheet</h1> ArchLinux Wiki has a fairly simple and easy-to-read guide related to the nftables command, which is available [here](https://wiki.archlinux.org/title/nftables) <h3>Highlights include: </h3> * **`nft list ruleset`** - Displays the current set of rules * **`nft flush ruleset`** - Removing rules, may leave device without working firewall * **`nft list tables`** - Displays the currently defined tables in the system * **`nft add table [family name] [table name]`** - Creates a table for a given family (options - ip, arp, etc.) with a given name <h1>7. Summary</h1>