brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ufw allow with comment puts the reason for a rule next to it

Ufw
~/tips/tip-ufw-rule-comment

sudo ufw allow 8096/tcp comment 'Jellyfin'

Adds a short note to a firewall rule so you remember why the port is open.

A firewall with twenty rules is fine until you look at port 8096 and can’t remember who asked for it. ufw has a small feature that deserves more attention: you can write the reason on the rule itself.

Add comment and a quoted text at the end of any rule. It works with the short syntax and with the long one:

sudo ufw allow 8096/tcp comment 'Jellyfin'
sudo ufw allow from 192.168.1.0/24 to any port 9443 proto tcp comment 'Portainer, LAN only'
sudo ufw status

In ufw status the text shows up behind the rule after a hash sign, so the second rule reads something like 9443/tcp ALLOW 192.168.1.0/24 # Portainer, LAN only. The ufw manual has the same idea with ufw limit 2222/tcp comment 'SSH port'.

Already have a pile of rules without a label? Run the exact same rule again with a comment and ufw updates the comment of the existing rule instead of adding a second one. An empty comment, comment '', removes it again. My advice is to put the name of the service in there, or the name of whoever asked for the port to be opened.

The caveat is in the small print of the man page: insert and prepend can’t be used to update a comment. If a rule was placed with ufw insert 1, give it its comment by repeating the rule without the insert part. Also quote the text whenever it has a space in it, or the shell splits it and ufw complains about the syntax. To delete a commented rule you can leave the comment off; the manual says delete works with or without it.

Further reading: ufw manual page (Ubuntu)

More Ufw tips sudo ufw app update all sudo ufw show raw sudo ufw allow to 10.0.0.1 proto esp sudo ufw allow to 224.0.0.0/24 from 10.0.0.1 proto vrrp sudo ufw show listening All Ufw tips (32) All topics

Link