Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux fail2ban Command

Linux fail2ban Command

Linux Command Reference Linux Command Reference


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:

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:

  1. Copy the default SSH filter:

    sudo cp /etc/fail2ban/filter.d/sshd.conf /etc/fail2ban/filter.d/sshd-custom.conf
  2. Edit the custom filter and modify the regular expression to match specific failure patterns.


Practical fail2ban examples

Protect the SSH service

  1. Edit the jail.local file:

    sudo nano /etc/fail2ban/jail.local
  2. 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

  1. 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

  1. Create a custom filter file:

    sudo nano /etc/fail2ban/filter.d/myapp.conf
  2. 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

  1. 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

IP not banned

IP wrongly banned


fail2ban best practices

  1. Update regularly: keep fail2ban updated to get the latest security fixes and feature improvements.

  2. Reasonable configuration:

  3. Set appropriate maxretry and bantime values
  4. Do not set bantime too long, to avoid banning legitimate users by mistake
  5. nor too short, or the protection will be limited
  6. - Set appropriate maxretry and bantime values - Do not set bantime too long, to avoid banning legitimate users by mistake - Nor too short, or the protection will be limited
    1. Monitoring and review:

    2. Regularly check the list of banned IPs
    3. Analyze logs to understand attack patterns
    4. - Regularly check the list of banned IPs - Analyze logs to understand attack patterns
      1. Layered protection:

      2. Combine fail2ban with other security measures (such as firewalls and strong password policies)
      3. Consider changing the default SSH port
      4. - Combine fail2ban with other security measures (such as firewalls and strong password policies) - Consider changing the default SSH port
        1. Back up configuration:

        2. Back up custom configuration files and filters
        3. Record all changes for disaster recovery
        4. - Back up custom configuration files and filters - Record all changes for disaster recovery

          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 Linux Command Reference