[Qubes OS 4.2] Easily NAT qubes port to external network

Original forum link
https://forum.qubes-os.org/t/24958
Original poster
Solène R
Created at
2024-03-09 09:44:08
Last wiki edit
2024-03-09 16:07:49
Revisions
1 revision
Posts count
18
Likes count
15

Hello,

I made a script to ease the work of doing a NAT to expose a qube port to the physical network interface. I wrote a blog post about it. I often need to do that when developing.

https://dataswamp.org/~solene/2024-03-06-qubes-os-nat.html

Source code available on sourcehut: https://git.sr.ht/~solene/qubes-os-nat

It takes 2 mandatory parameters and 1 optional parameter:

  1. qube name
  2. port number
  3. protocol, either tcp or udp, tcp by default

The script will crawl the qube netVm up to the last netvm holding the physical interface, even if it's not named sys-net, and establish the port redirection on all netvm in the path.

The script does not alter qvm-firewall rules, so if you blocked all ports, the redirection will not bypass the firewall. This is on purpose.

The redirection is temporary, if you reboot a qube in the network path or the qube itself, the redirection will stop working. If you want a permanent redirection, you should script your qubes to do so.

Installation

A simple method to copy the script to dom0 is to run this command on dom0:

qvm-run --pass-io sys-firewall "curl https://git.sr.ht/~solene/qubes-os-nat/blob/master/nat.sh" > nat.sh
chmod +x nat.sh

You can put the script wherever you prefer.

Usage

If you want to expose port TCP 8080 of a qube named Server type:

./nat.sh Server 8080 tcp

It also works for UDP, replace tcp with udp in the command line.

Limitations

Currently, there is no way to undo added rules, this is not a huge issue as rebooting remove the rules, but it's not nice. The rules are added at the top of the related chains, so if you run the script to forward the same port you used before to another qube, it will have a higer priority over the older rules. 👍