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 log writes every new connection of one rule to the log

Ufw
~/tips/tip-ufw-allow-log-per-rule

sudo ufw allow log 22/tcp

Logs connections that match one specific rule, without raising the global log level.

Raising ufw logging to medium or high logs a lot of traffic you don’t care about. To see who uses one port, add logging to that one rule.

Put log after the action:

sudo ufw allow log 22/tcp
sudo ufw allow log-all from 192.0.2.50 to any port 5432 proto tcp

log writes a line for each new connection that matches, so every SSH login attempt appears once. log-all logs every packet of the rule, which I’d keep for short debugging sessions, because a busy connection fills the log quickly.

The lines go to the kernel log with the prefix [UFW ALLOW], so you can follow them with:

sudo journalctl -k -f | grep 'UFW ALLOW'

On Ubuntu they also land in /var/log/ufw.log through rsyslog.

To add logging to an existing rule, you have to replace it: ufw delete the old one and add the new one with log. Since a new rule is appended at the end, check ufw status numbered to make sure it doesn’t end up below a deny rule that matches first.

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