Fail2ban: protecting SSH with nftables
Configuring a fail2ban sshd jail with nftables, all-ports banning and common commands.
Fail2ban protects SSH against brute-force attacks by banning offending IP addresses. This article configures fail2ban with nftables as the ban action. See also the Linode guide on fail2ban and SSH.
Configuration
Default settings, for example in /etc/fail2ban/jail.local:
[DEFAULT]
banaction = nftables
banaction_allports = nftables[type=allports]
bantime = 1d
SSH jail, for example in /etc/fail2ban/jail.d/sshd.local:
[sshd]
enabled = true
filter = sshd
banaction = nftables
backend = systemd
maxretry = 5
findtime = 1d
bantime = 2w
port = 2222
ignoreip = 127.0.0.1/8
To ban on all ports (not only the SSH one), use the nftables-allports action:
[sshd]
enabled = true
filter = sshd
#banaction = nftables
banaction = nftables-allports
backend = systemd
maxretry = 5
findtime = 1d
bantime = 2w
port = 2222
ignoreip = 127.0.0.1/8
If the reject block type causes an error
Force the drop block type in /etc/fail2ban/jail.local:
[DEFAULT]
banaction = nftables-multiport[blocktype=drop]
banaction_allports = nftables-allports[blocktype=drop]
Checking that it works
sudo tail -f /var/log/fail2ban.log
tail -f /var/log/auth.log
sudo fail2ban-client status sshd
Everyday usage
sudo fail2ban-client status sshd
sudo zgrep 'Ban' /var/log/fail2ban.log*
sudo nft list set inet f2b-table addr-set-sshd
# unban an address
sudo fail2ban-client unban 192.0.2.10
# ban an address manually
sudo fail2ban-client -vvv set sshd banip 203.0.113.10
To list the most active addresses in a web access log:
cat access.log | awk '{print $1}' | sort -n | uniq -c | sort -nr | head -20
Notes
- The file names above are examples; any
.localfile read by fail2ban will do. - Port
2222is an example: use the port of your SSH service.