Using Blocklistd with FreeBSD to protect jails
2026-11-18Table of Contents
If you are publishing any kind of service to the public internet, you absolutely want to make sure incoming requests are handled responsibly, and ultimately, protect yourself against repeated unauthorized access attempts.
In an example of a standard ssh service, this can be quite easy - just set up blocklistd or fail2ban to monitor the requests and block appropriately, but when it comes to blocking guest services (in this example I am protecting FreeBSD jails) there's a little more to it.
When I've set this up myself in various places I have found myself having to reference multiple different documents. I intend to write some notes for myself here for a complete solution, which I hope also help other people when trying to do something similar.
Blocklistd
Blocklistd (previously blacklistd) is a piece of software that can
help protect services such as ssh, ftp, and many more, by detecting brute force attempts and adding them to a block list. A firewall or packet filter (i.e. pf)
can then reference this block list to block further incoming traffic.
It's quite similar in result to fail2ban but, instead of reading a log file and acting on activity, services such as sshd can interact directly with blocklistd, in effect
"telling" blocklistd about incoming requests.
Use Case
The example I am using here is for protecting ssh access to both a FreeBSD host machine, and ssh on a different port to a jail running Forgejo - the code forge software.
Protecting the host machine
Let's protect the host machine first, which will set some fundamental things up.
Blocklistd is part of FreeBSD's base so you can just enable it as you would anything else.
> sysrc blocklistd_enable="YES"
> service blocklistd start
We also need to tell sshd to UseBlocklist
> echo "UseBlocklist yes" >> /etc/ssh/sshd_config
> service sshd restart
Now, sshd will "tell" blocklistd about every incoming request it receives. This doesn't block yet, we need to tell pf to do that.
Edit /etc/pf.conf and make sure you have the blocklistd/* anchor. This anchor must come before any block or pass rules (pf will error if not).
# /etc/pf.conf
...
anchor "blocklistd/*" in on $ext_if
block in
pass out
...
And it's as simple as that! After 3 unsuccessful attempts to access the server via ssh, blocklistd will add the remote IP address to its blocklist and "tell" pf to block futher incoming requests from that IP address.
The default block time for ssh is 24 hours, but this and the amount of unsuccessful attempts can be configured by editing the /etc/blocklistd.conf file.
You can see blocked IPs using the command blocklistctl dump -b.
Protecting Jails
I am running Forgejo within a jail and I have ssh access to it on an alternative port (let's just say that port is 2222/tcp) - how can I protect this ssh server?
Note: I am not using forgejo's internal ssh server; it cannot interact with blocklistd. I am instead using openssh in the jail which is configured to interface with forgejo directly. If you'd like information on how to do this, get in touch and I'd be happy to offer some advice!
Although you can enable and run blocklistd in a jail, you can't run pf (unless you have a VNET jail, in which case you can just follow the steps in Protecting the Host Machine ) so nothing will get blocked. Instead, we configure the host machine to place a blocklistd socket into the jail, which sshd can interface with and then allow the host machine's pf to perform the blocking.
Creating a blocklistd socket within the jail
On the host machine, add some blocklistd flags to /etc/rc.conf.
> sysrc blocklistd_flags="-r -P /etc/blocklistd-sockets"
Flag meanings from the manpage:
-r: Re-read the firewall rules from the internal database, then remove and re-add them. This helps for packet filters that do not retain state across reboots.-P /etc/blocklistd-sockets: A file containing a list of pathnames, one per line that blocklistd will create sockets to listen to. This is useful for chrooted environments.
-P is obviously also useful for jails!
Next we create the /etc/blocklistd-sockets file and enter our socket locations into it
> touch /etc/blocklistd-sockets
> echo /var/run/blocklistd.sock >> /etc/blocklistd-sockets
> echo /$jailpath/var/run/blocklistd.sock >> /etc/blocklistd-sockets
Where $jailpath is the location of your jail's root filesystem. For example, if you are using bastille to manage jails, you'd do something like the below:
> echo /usr/local/bastille/jails/forgejo/root/var/run/blocklistd.sock >> /etc/blocklistd-sockets
Restart the blocklistd service and you should see the socket file appear in your jail's /var/run directory.
> service blocklistd restart
Now, tell your jail's sshd service to UseBlocklist and it will communicate with that socket, telling your host machine's blocklistd about incoming connections.
jail> echo "UseBlocklist yes" >> /etc/ssh/sshd_config
jail> service sshd restart
We also need to configure blocklistd on the host to handle port 2222/tcp the same way as the standard ssh port
# /etc/blocklistd.conf
# Blocklist rule
# adr/mask:port type proto owner name nfail duration
[local]
ssh stream * * * 3 24h
2222 stream * * * 3 24h
And restart blocklistd a final time.
> service blocklistd restart
And we're done - incoming ssh traffic to both the host machine on port 22 and the jail on port 2222 will now be communicated back to blocklistd which will in turn handle whether the incoming connection has tried too many times unsuccessfully and then tell pf to block further incoming connections for 24 hours.
A quick pf "Gotcha!"
The above should work perfectly fine in most cases.
However, it turns out due to the ordering of pf rules,
you may need to consider how these are handled.
Specifically, in my case, I am talking about rdr pass rules.
I have rules in my /etc/pf.conf like the below:
# /etc/pf.conf
...
rdr pass on $ext_if proto tcp from any to port 2222 -> $forgejo_jail
...
This rule redirects all traffic destined for port 2222/tcp to my Forgejo jail's IP address with a convenience pass keyword in it. (It's probably quite silly on my part to have rules like this!)
As the blocklistd anchor rule must come after the rdr rules, pf won't actually block any traffic because the rdr pass rule circumvents it.
Fortunately, the fix here is quite simple - split the rdr and pass parts up:
# /etc/pf.conf
...
rdr on $ext_if proto tcp from any to port 2222 -> $forgejo_jail
...
anchor "blocklistd/*" in on $ext_if
block all
pass out
...
pass in on $ext_if proto tcp to port 2222 keep state
...
Now we have a single redirect rule (note it's now rdr on... instead of rdr pass on...), the blocklistd anchor, and later the pass rule to allow the incoming traffic. When blocklistd and pf need to block an IP, a new block rule is temporarily added which prevents the pass rule for the connecting IP address.