|
| 1 | +--- |
| 2 | +title: "Protect Your Linux Server with Fail2ban" |
| 3 | +date: 2026-04-01 15:00:00+08:00 |
| 4 | +description: "Fail2ban is a powerful tool to protect your server from brute-force attacks. This tutorial guides you through installing and configuring Fail2ban." |
| 5 | +categories: |
| 6 | + - Tutorials |
| 7 | +tags: |
| 8 | + - Linux |
| 9 | +--- |
| 10 | + |
| 11 | +## What is Fail2ban? |
| 12 | + |
| 13 | +Fail2ban is an intrusion prevention software framework that protects computer servers from brute-force attacks. It works by scanning log files (e.g., `/var/log/auth.log`) and banning IP addresses that show malicious signs, such as too many password failures, seeking for exploits, etc. |
| 14 | + |
| 15 | +## Installing Fail2ban |
| 16 | + |
| 17 | +Choose the installation method according to your Linux distribution. |
| 18 | + |
| 19 | +### 1. Using Package Manager |
| 20 | + |
| 21 | +#### Debian / Ubuntu |
| 22 | +```bash |
| 23 | +sudo apt update |
| 24 | +sudo apt install fail2ban |
| 25 | +``` |
| 26 | + |
| 27 | +#### CentOS / RHEL (Using yum or dnf) |
| 28 | +On CentOS/RHEL, you typically need to install the EPEL repository first: |
| 29 | +```bash |
| 30 | +# CentOS 7 |
| 31 | +sudo yum install epel-release |
| 32 | +sudo yum install fail2ban |
| 33 | + |
| 34 | +# CentOS 8 / RHEL 8 / Fedora (Using dnf) |
| 35 | +sudo dnf install epel-release |
| 36 | +sudo dnf install fail2ban |
| 37 | +``` |
| 38 | + |
| 39 | +### 2. From Source (tar.gz) |
| 40 | +If you need a specific version or your distribution doesn't provide a package, you can install from source: |
| 41 | + |
| 42 | +```bash |
| 43 | +# Download source (replace with the latest version link) |
| 44 | +wget https://github.com/fail2ban/fail2ban/archive/refs/tags/1.0.2.tar.gz |
| 45 | +tar -xvzf 1.0.2.tar.gz |
| 46 | +cd fail2ban-1.0.2 |
| 47 | + |
| 48 | +# Install |
| 49 | +sudo python3 setup.py install |
| 50 | +``` |
| 51 | +*Note: Source installation typically requires manual configuration of systemd service files and log paths.* |
| 52 | + |
| 53 | +After installation, it is recommended to set Fail2ban to start on boot: |
| 54 | +```bash |
| 55 | +sudo systemctl enable fail2ban |
| 56 | +sudo systemctl start fail2ban |
| 57 | +``` |
| 58 | + |
| 59 | +## Configuring Fail2ban |
| 60 | + |
| 61 | +The default configuration file for Fail2ban is `/etc/fail2ban/jail.conf`. However, it's not recommended to modify this file directly, as it may be overwritten during package upgrades. Instead, you should create a local configuration file, `/etc/fail2ban/jail.local`, or new `.conf` files in the `/etc/fail2ban/jail.d/` directory to override the defaults. |
| 62 | + |
| 63 | +### Create a Local Configuration File |
| 64 | + |
| 65 | +First, copy `jail.conf` to `jail.local`: |
| 66 | + |
| 67 | +```bash |
| 68 | +sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local |
| 69 | +``` |
| 70 | + |
| 71 | +Now you can safely edit the `jail.local` file. |
| 72 | + |
| 73 | +### Configure SSH Protection |
| 74 | + |
| 75 | +Open the `/etc/fail2ban/jail.local` file and find the `[sshd]` section. You can customize the following parameters as needed: |
| 76 | + |
| 77 | +```ini |
| 78 | +[sshd] |
| 79 | +enabled = true |
| 80 | +port = ssh |
| 81 | +logpath = %(sshd_log)s |
| 82 | +backend = %(sshd_backend)s |
| 83 | +maxretry = 5 |
| 84 | +findtime = 10m |
| 85 | +bantime = 1d |
| 86 | +``` |
| 87 | + |
| 88 | +- `enabled`: `true` enables this jail. |
| 89 | +- `port`: The port for the SSH service. |
| 90 | +- `logpath`: The path to the SSH authentication log file. |
| 91 | +- `maxretry`: The number of failures before a ban is imposed. |
| 92 | +- `findtime`: The time window during which the failures must occur. |
| 93 | +- `bantime`: The duration for which the IP address is banned. `1d` means one day. |
| 94 | + |
| 95 | +### More Practical Scenarios |
| 96 | + |
| 97 | +In addition to SSH, Fail2ban can protect many other services. Add the following to `jail.local`: |
| 98 | + |
| 99 | +#### Nginx Prevention of Malicious Scans (Too many 404 errors) |
| 100 | +```ini |
| 101 | +[nginx-404] |
| 102 | +enabled = true |
| 103 | +port = http,https |
| 104 | +filter = nginx-404 |
| 105 | +logpath = /var/log/nginx/access.log |
| 106 | +findtime = 600 |
| 107 | +maxretry = 5 |
| 108 | +bantime = 1h |
| 109 | +``` |
| 110 | +*Note: This requires you to define an `nginx-404.conf` filter under `/etc/fail2ban/filter.d/`.* |
| 111 | + |
| 112 | +#### MySQL/MariaDB Protection |
| 113 | +```ini |
| 114 | +[mariadb-jail] |
| 115 | +enabled = true |
| 116 | +port = 3306 |
| 117 | +filter = mysqld-auth |
| 118 | +logpath = /var/log/mysql/error.log |
| 119 | +maxretry = 3 |
| 120 | +``` |
| 121 | + |
| 122 | +## Common Fail2ban Management Commands |
| 123 | + |
| 124 | +`fail2ban-client` is the primary tool for managing Fail2ban. |
| 125 | + |
| 126 | +### 1. Check Status |
| 127 | +```bash |
| 128 | +# Check overall service running status |
| 129 | +sudo fail2ban-client ping |
| 130 | + |
| 131 | +# Check the list of enabled jails |
| 132 | +sudo fail2ban-client status |
| 133 | + |
| 134 | +# Check detailed status of a specific jail (e.g., sshd) |
| 135 | +sudo fail2ban-client status sshd |
| 136 | +``` |
| 137 | + |
| 138 | +### 2. Manage Banned IPs |
| 139 | +```bash |
| 140 | +# Manually ban an IP (in the sshd jail) |
| 141 | +sudo fail2ban-client set sshd banip 1.2.3.4 |
| 142 | + |
| 143 | +# Manually unban an IP |
| 144 | +sudo fail2ban-client set sshd unbanip 1.2.3.4 |
| 145 | + |
| 146 | +# Unban an IP from all jails |
| 147 | +sudo fail2ban-client unban 1.2.3.4 |
| 148 | +``` |
| 149 | + |
| 150 | +### 3. Reload Configuration |
| 151 | +When you modify `.local` files or filters, you can apply changes without restarting the entire service: |
| 152 | +```bash |
| 153 | +sudo fail2ban-client reload |
| 154 | +``` |
| 155 | + |
| 156 | +## FAQ & Tips |
| 157 | +- **Whitelist**: Set `ignoreip = 127.0.0.1/8 ::1 <Your Fixed IP>` in the `[DEFAULT]` section to prevent locking yourself out. |
| 158 | +- **Persistence**: By default, bans expire after a service restart. For persistence, you can configure database storage. |
| 159 | +- **Email Notifications**: Fail2ban supports sending email alerts to administrators when an IP is banned. |
| 160 | + |
| 161 | +## Summary |
| 162 | + |
| 163 | +Fail2ban is a simple yet effective tool that adds a significant layer of security to your server. With proper configuration, you can greatly reduce the risk of brute-force attacks. |
0 commit comments