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 reject answers right away, where deny lets the other side wait

Ufw
~/tips/tip-ufw-reject-instead-of-deny

sudo ufw reject 8080/tcp

Refuses a connection with an immediate reply instead of silently dropping it.

ufw has two ways to say no, and I think most people only ever type the first one. deny drops the packet without a word. reject sends an answer back, so the other side knows straight away that the door is shut.

The syntax is the same as for any other rule:

sudo ufw reject 8080/tcp
sudo ufw reject out smtp
sudo ufw status

The ufw source shows what happens underneath. A reject rule becomes an iptables REJECT target, and for TCP ufw adds --reject-with tcp-reset, following TCP’s normal behaviour of sending a reset. A client trying port 8080 gets an immediate “connection refused” instead of hanging until its timeout runs out. For UDP and other protocols iptables falls back to its default ICMP reply.

Where I’d use it: on my own LAN and for outgoing rules. The second line above blocks outgoing mail from the server itself, and a script that tries to send mail fails fast with a clear error instead of hanging for a minute. The manual also mentions reject for the case where you want the sender to know traffic is being denied. You can even make it the default with ufw default reject incoming.

The caveat: for ports facing the internet I’d stick with deny. A reject tells a scanner that a host is there and that it actively refused, and every refused probe costs you a reply packet. Silence gives away less. So deny on the public side, reject where the people knocking are your own scripts and machines.

Further reading: ufw manual page (Ubuntu), iptables-extensions: REJECT target

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