To set up Fail2ban on a Linux server, install the package, create a jail.local configuration, enable and tune the sshd jail (bantime, findtime, maxretry, ignoreip), select the right firewall backend (UFW, firewalld, iptables/nftables), then start and enable the service. Finally, verify jails, test bans, and monitor logs for intrusions.
In this guide, you’ll learn how to setup Fail2ban on Linux server step by step to stop brute-force attacks and malicious traffic. As a hosting professional, I’ll show you practical, production-safe settings for Ubuntu/Debian and RHEL/AlmaLinux/CentOS, plus tuning tips, troubleshooting, and real-world best practices we use at YouStable.
What is Fail2ban and Why Use it?

Fail2ban is an intrusion-prevention tool that scans logs for repeated failed logins and other suspicious behavior. When an IP triggers a rule, Fail2ban temporarily bans it via your firewall. It’s lightweight, highly configurable, and ideal for protecting SSH, Nginx/Apache, Postfix/Dovecot, and more on Linux servers.
Prerequisites
- A Linux server (Ubuntu/Debian or RHEL/AlmaLinux/CentOS) with sudo access
- OpenSSH running and accessible
- Firewall available: UFW (Ubuntu), firewalld (RHEL/AlmaLinux), or iptables/nftables
- Server updated: apt or dnf/yum packages current
- Your admin IP to whitelist (to avoid locking yourself out)
Install Fail2ban
Ubuntu/Debian
sudo apt update
sudo apt install -y fail2ban
RHEL/AlmaLinux/Rocky/CentOS
# RHEL 8+/AlmaLinux/Rocky
sudo dnf install -y fail2ban fail2ban-firewalld
# CentOS 7 (EOL – consider upgrading)
sudo yum install -y epel-release
sudo yum install -y fail2ban
Verify Installation
fail2ban-client -V
# Example: 0.11.x (supports nftables, bantime.increment, etc.)
Create and Tune jail.local (Core Configuration)
Fail2ban reads defaults from jail.conf. Don’t edit that file directly. Instead, create jail.local to override settings and enable jails.
Step 1: Create /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
Use this safe, cross-distro starting point. Adjust IPs, emails, and ports as needed.
[DEFAULT]
# Ban timing
bantime = 12h
findtime = 10m
maxretry = 5
bantime.increment = true
bantime.factor = 1.5
bantime.maxtime = 1w
# Avoid locking yourself out
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
# Logs and notifications
loglevel = INFO
logtarget = /var/log/fail2ban.log
destemail = admin@example.com
sender = fail2ban@example.com
mta = sendmail
action = %(action_mwl)s
# Choose your firewall backend (pick ONE based on your system)
# For UFW on Ubuntu
# banaction = ufw
# For firewalld on RHEL/AlmaLinux
# banaction = firewallcmd-ipset
# For iptables (legacy)
# banaction = iptables-multiport
# For nftables (modern Debian/Ubuntu/RHEL)
banaction = nftables-multiport
# Use systemd journal if available for more reliable log reading
backend = systemd
[sshd]
enabled = true
port = ssh
# Use distro-provided variable that maps to correct SSH log file
logpath = %(sshd_log)s
maxretry = 5
findtime = 10m
bantime = 24h
Key Options Explained
- bantime, findtime, maxretry: How long to ban, time window for counting failures, and how many failures trigger a ban.
- bantime.increment: Gradually increases ban time for repeat offenders; helpful against persistent bots.
- ignoreip: Whitelist your office/VPN IPs to prevent lockouts.
- banaction: Must match your firewall (ufw, firewalld, iptables, nftables).
- backend: “systemd” reads from the journal; more reliable than log files on modern systems.
Firewall Backends and Ports
- UFW: Set
banaction = ufwand ensure UFW is enabled. - firewalld: Use
banaction = firewallcmd-ipsetand keep firewalld running. - nftables/iptables: On newer distros,
nftables-multiportworks best. Legacy systems can useiptables-multiport. - Changed SSH port? Update
[sshd]port(e.g.,port = 2222).
Enable and Start Fail2ban
sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban
If you change the config later, reload:
sudo fail2ban-client reload
Protect Common Services with Jails
Beyond SSH, enable jails for your web server and mail stack. Only enable what you use.
Nginx
[nginx-http-auth]
enabled = true
logpath = /var/log/nginx/error.log
[nginx-botsearch]
enabled = true
logpath = /var/log/nginx/error.log
maxretry = 10
findtime = 10m
bantime = 24h
Apache
[apache-auth]
enabled = true
logpath = /var/log/apache2/error.log
# RHEL-based:
# logpath = /var/log/httpd/error_log
[apache-badbots]
enabled = true
logpath = /var/log/apache2/access.log
Mail (Postfix/Dovecot)
[postfix]
enabled = true
logpath = /var/log/mail.log
# RHEL-based: /var/log/maillog
[dovecot]
enabled = true
logpath = /var/log/mail.log
After editing, reload and confirm jails are active:
sudo fail2ban-client reload
sudo fail2ban-client status
sudo fail2ban-client status sshd
Testing, Monitoring, and Managing Bans
List Jails and See Banned IPs
sudo fail2ban-client status
sudo fail2ban-client status sshd
Unban or Ban an IP Manually
# Unban
sudo fail2ban-client set sshd unbanip 198.51.100.25
# Ban immediately
sudo fail2ban-client set sshd banip 198.51.100.25
Dry-Run a Filter Against Logs
# Test if a filter catches malicious lines
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf
Best Practices and Hardening Tips
- Whitelist admin IPs: Add office/VPN IPs to
ignoreip. - Use incremental bans:
bantime.increment = truewith a sensiblebantime.maxtime(e.g., one week). - Enable recidive: Catch repeat offenders across multiple jails over time.
- Move SSH to a non-standard port: Reduce noise; reflect that change in your
[sshd]jail. - Keep logs healthy: Ensure logrotate is working so Fail2ban reads fresh logs; with
backend = systemd, rely on journal. - Layer security: Combine Fail2ban with strong passwords/keys, 2FA where possible, and cloud firewalls or provider-level filters.
- Email alerts: Use
action = %(action_mwl)sfor ban emails that include whois and log excerpts.
Recidive Jail (Highly Recommended)
Recidive re-bans IPs that have been banned by any jail multiple times within a period. This is excellent for persistent attackers.
[recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
findtime = 1d
bantime = 7d
maxretry = 5
Troubleshooting Common Issues
- No bans happening: Check logs (
/var/log/fail2ban.log), increaseloglevel = DEBUG, confirmbackend = systemdor correctlogpath, verify that failed attempts are present in logs. - Firewall not updating: Ensure matching
banaction(ufw/firewalld/nftables/iptables) and that the firewall service is active. - Locked out of SSH: Use console/serial access from your provider, add your IP to
ignoreip, and reducebantimeormaxretrytemporarily. - Changed SSH port but still no bans: Update
portin the[sshd]jail to your custom port. - SELinux issues (RHEL-based): Check
audit.logand usesetsebool/semanageif required; typically, Fail2ban works fine out of the box.
Real-World Tuning Examples
Quiet a High-Traffic SSH Server
[sshd]
enabled = true
port = ssh
findtime = 15m
maxretry = 6
bantime = 48h
bantime.increment = true
bantime.factor = 2
bantime.maxtime = 2w
NFTables on Modern Distros
[DEFAULT]
banaction = nftables-multiport
backend = systemd
This ensures Fail2ban manipulates nftables chains rather than legacy iptables, matching how modern kernels handle packet filtering.
Managed Security with YouStable (Optional but Helpful)
If you’d prefer not to manage server security alone, YouStable’s managed VPS and dedicated servers can ship with Fail2ban pre-configured, along with hardened SSH, firewall policies, and 24/7 monitoring. This frees you to focus on your apps while we handle patching and intrusion prevention.
With these steps, you now know exactly how to setup Fail2ban on Linux server for robust, low-maintenance protection against brute-force attacks and common intrusion attempts. Keep your system updated, review logs regularly, and tune thresholds as your traffic evolves.
FAQs: How to Setup Fail2ban on Linux Server
What is Fail2ban and how does it work?
Fail2ban monitors service logs for repeated failures (like SSH login attempts). When an IP exceeds thresholds, it adds a temporary firewall rule to block that IP. After bantime expires, the rule is removed. It’s highly customizable via jails for SSH, web servers, and mail services.
How do I install and configure Fail2ban on Ubuntu 22.04?
Run sudo apt install fail2ban, create /etc/fail2ban/jail.local with your [DEFAULT] and [sshd] settings, set banaction = ufw or nftables-multiport, then sudo systemctl enable --now fail2ban. Verify with fail2ban-client status and adjust thresholds as needed.
Does Fail2ban work with UFW and firewalld?
Yes. For UFW, use banaction = ufw. For firewalld, use banaction = firewallcmd-ipset. On nftables-based systems, nftables-multiport is recommended. Ensure your firewall service is running and enabled at boot.
How can I unban an IP or see who is banned?
Use sudo fail2ban-client status to list jails and counts, sudo fail2ban-client status sshd to view banned IPs in that jail, and sudo fail2ban-client set sshd unbanip <IP> to unban. All actions are logged in /var/log/fail2ban.log.
What are recommended Fail2ban settings for SSH?
For most servers: findtime = 10–15m, maxretry = 5–6, bantime = 12–48h, with bantime.increment = true. Whitelist your IP via ignoreip, and consider moving SSH to a non-standard port. Enable recidive to catch persistent attackers over longer periods.