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 proto tcp with port 80,443 opens several ports in one rule

Ufw
~/tips/tip-ufw-multiple-ports-range

sudo ufw allow proto tcp from any to any port 80,443 comment 'web'

Combines a list of ports and port ranges into a single firewall rule.

Separate rules for 80 and 443, and ten more for a range, make ufw status long and hard to read. ufw accepts lists and ranges in one rule.

The extended syntax takes a comma separated list, and a range written with a colon:

sudo ufw allow proto tcp from any to any port 80,443 comment 'web'
sudo ufw allow proto udp from 192.0.2.0/24 to any port 60000:61000 comment 'mosh, LAN'
sudo ufw allow 6000:6007/tcp

The first rule opens both web ports together, the second a UDP range for mosh from one subnet only. The last line shows the short form for a range, with the protocol after a slash.

A few rules from the manual that are easy to miss. With a list or a range, the protocol is required, because ufw refuses to guess. The list must be numbers only, so no service names like http, and no spaces after the commas. You can put at most 15 ports in one rule, and a range counts as two.

One rule also means one unit when you change it. You can’t delete port 443 from the list on its own; you delete the whole rule and add a new one. Because of that I’d group ports that belong to the same service, like the web pair, and keep unrelated ports in their own rules with a comment.

Further reading: ufw(8) manual page

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