Linux fail2ban Command
fail2ban is an open-source intrusion prevention tool used to protect Linux servers from brute-force attacks. It monitors system log files (such as /var/log/auth.log) to detect malicious behavior, such as repeated failed SSH login attempts, and then automatically updates firewall rules to block the IP addresses of these attackers.
Core features of fail2ban
Real-time log monitoring
fail2ban continuously monitors specified log files for malicious behavior matching predefined patterns.
Automatic IP banning
When multiple failed attempts from the same IP are detected (a configurable threshold), fail2ban automatically adds that IP to the firewall block list.
Configurable ban time
Administrators can set an initial ban time and an increasing ban time after repeated violations.
Multi-service support
In addition to SSH, it protects many services such as Apache, Nginx, FTP, and mail services.
Email notification
Can be configured to send an email notification to the administrator when an IP is banned.
Installation and configuration of fail2ban
Installation Method
On Debian/Ubuntu-based systems:
Example
sudo
apt update
sudo
apt
install
fail2ban
On RHEL/CentOS-based systems:
Example
sudo
yum install
epel-release
sudo
yum install
fail2ban
Basic configuration
The main configuration files of fail2ban are located in:
/etc/fail2ban/jail.conf- Main configuration file (not recommended to modify directly)/etc/fail2ban/jail.local- User-defined configuration (recommended to modify here)
Create a custom configuration file:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Common fail2ban commands
Start/stop/restart the service
Example
sudo
systemctl start fail2ban
Start the service
sudo
systemctl stop fail2ban
Stop a service
sudo
systemctl restart fail2ban
Restart the service
sudo
systemctl
enable
fail2ban
Enable startup on boot
Check service status
sudo systemctl status fail2ban
View banned IPs
sudo fail2ban-client status sshd
Unban a specific IP
sudo fail2ban-client set sshd unbanip 192.168.1.100
Manually ban an IP
sudo fail2ban-client set sshd banip 192.168.1.100
Detailed explanation of fail2ban configuration files
Main configuration parameters
Example
[DEFAULT]
Ignored IP addresses (whitelist)
ignoreip
=
127.0.0.1/8 ::1 192.168.1.0/24
Ban time (seconds)
bantime
=
600
Detection time window (seconds)
findtime
=
600
Maximum number of attempts
maxretry
=
3
Firewall backend to use
banaction
=
iptables-multiport
[sshd]
Whether to enable SSH protection
enabled
=
true
Log file path
logpath
=
%(sshd_log)s
Filter name
filter
=
sshd
Port number
port
=
ssh
Custom filters
Filters are defined in the /etc/fail2ban/filter.d/ directory. For example, to create a custom SSH filter:
-
Copy the default SSH filter:
sudo cp /etc/fail2ban/filter.d/sshd.conf /etc/fail2ban/filter.d/sshd-custom.conf
-
Edit the custom filter and modify the regular expression to match specific failure patterns.
Practical fail2ban examples
Protect the SSH service
-
Edit the jail.local file:
sudo nano /etc/fail2ban/jail.local
-
Add or modify the following content:
[sshd] enabled = true port = ssh filter = sshd logpath = /var/log/auth.log maxretry = 3 bantime = 3600 findtime = 600 3.
Restart the fail2ban service:
sudo systemctl restart fail2ban
Protect the Apache service
-
Make sure jail.local contains the following:
[apache] enabled = true port = http,https filter = apache-auth logpath = /var/log/apache2/error.log maxretry = 3 bantime = 86400 2.
Restart the service to apply the configuration.
Advanced usage of fail2ban
Use fail2ban to protect a custom service
-
Create a custom filter file:
sudo nano /etc/fail2ban/filter.d/myapp.conf
-
Add filtering rules (example):
[Definition] failregex = ^.* .* "POST /login.php.* 401 ignoreregex = 3.
Add the corresponding jail in jail.local:
enabled = true port = http,https filter = myapp logpath = /var/log/myapp/access.log maxretry = 5 bantime = 3600
Set up email notifications
-
Edit the jail.local file:
[DEFAULT] destemail = admin@example.com sender = fail2ban@example.com mta = sendmail action = %(action_mwl)s 2.
Make sure a mail-sending tool (such as sendmail or postfix) is installed and configured on the system.
fail2ban logs and troubleshooting
View fail2ban logs
sudo tail -f /var/log/fail2ban.log
Common Problem Solutions
fail2ban not working
- Check whether the service is running:
sudo systemctl status fail2ban - Check the logs for errors:
sudo journalctl -u fail2ban
IP not banned
- Verify the log path is correct
- Check whether the filter's regular expression matches log entries
- Increase the log level for debugging: set in jail.local
loglevel = DEBUG
IP wrongly banned
- Add trusted IPs to the ignoreip list
- Decrease maxretry or increase findtime
fail2ban best practices
-
Update regularly: keep fail2ban updated to get the latest security fixes and feature improvements.
-
Reasonable configuration:
-
Monitoring and review:
-
Layered protection:
-
Back up configuration:
Summary
fail2ban is an important tool for Linux system security; by automatically detecting and blocking malicious behavior, it effectively prevents brute-force attacks. Properly configuring and using fail2ban can significantly improve server security while reducing the need for manual administrator intervention. Through this introduction, you should have mastered the basic usage and advanced configuration techniques of fail2ban, and be able to customize your own security protection strategy according to actual needs.
Linux Command Reference