Fail2Ban Reference & Useful Commands
Category: All About Ubuntu
Last Updated: May 11, 2025
Applies To: Ubuntu Server 22.04+
ThisFail2Ban pageJail outlinesConfiguration
Fail2Ban
jails fail2ban-clientcommandscontrol how long an IP remains banned after matching filters. To increase ban duration (e.g., to help48 monitor,hours):
Configuration File
sudo nano /etc/fail2ban/jail.local
Example Jail Settings for SSH and interactUFW withBlock:
[sshd]
jails.enabled These= aretrue
especiallyport useful= whenssh
securinglogpath SSH= access%(sshd_log)s
orbantime reviewing= suspicious172800
loginfindtime attempts.= 600
maxretry = 3
[ufw-block]
enabled = true
filter = ufw-block
logpath = /var/log/ufw.log
bantime = 172800
findtime = 600
maxretry = 3
✅ bantime
is in seconds → 172800
equals 48 hours
✅ findtime
is the window (in seconds) to detect repeated offenses
✅ maxretry
is the number of failed attempts before banning
After changes:
sudo systemctl restart fail2ban
Useful Commands
📊
Task | Command |
---|---|
Check fail2ban service status | sudo systemctl status fail2ban |
Start fail2ban | sudo systemctl start fail2ban |
Restart fail2ban | sudo systemctl restart fail2ban |
View all jail statuses | sudo fail2ban-client status |
View a |
sudo fail2ban-client status sshd |
See
|
sudo fail2ban-client get sshd banned |
Unban an IP from a jail |
sudo fail2ban-client set sshd unbanip < |
Get ignore list for a |
sudo |
Manually test a filter (dry run) | fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf |
Filter & Jail File Paths
File Purpose | Path |
---|---|
Jail configuration | /etc/fail2ban/jail.local |
Custom filters | /etc/fail2ban/filter.d/ |
Fail2Ban main log | /var/log/fail2ban.log |
UFW log (for ufw-block) | /var/log/ufw.log |
Notes
-
Use
thisignoreip
tomonitorexemptloginsafefailuresIPsand(includingban actions in real time.local/DDNS).
🔁 Restart Fail2Ban
sudo systemctl restart fail2ban
RestartConsidertherotating logs weekly to avoid bloated logs.Fail2Ban
servicecanafterbemodifyingextendedconfigstoorcoverjailotherfiles.services (Apache, Postfix, etc.).