Fortify your server: Protect against brute-force attacks with Fail2Ban for SSH, Apache, FTP, and MySQL.
Estimated Time: Approximately 30 - 60 minutes
Fail2Ban is an intrusion prevention framework that protects computer servers from brute-force attacks. It works by monitoring log files (e.g., SSH, Apache, FTP, MySQL error logs) for malicious activity, such as repeated failed login attempts, and then automatically updates firewall rules to block the originating IP addresses for a specified period.
Think of Fail2Ban as a proactive security guard for your server, automatically detecting suspicious behavior and temporarily locking out potential attackers.
Why is Fail2Ban Essential?
30 - 60 minutes
(For installation, basic configuration, and testing common jails.)
Intermediate
Assumes basic familiarity with Linux terminal commands, text editors, and firewall (UFW) management.
sudo
privileges.Always start by ensuring your system is up-to-date.
sudo apt update && sudo apt upgrade -y
Install the Fail2Ban package from the Ubuntu repositories.
sudo apt install fail2ban -y
Once installed, Fail2Ban will automatically start. You can check its status:
sudo systemctl status fail2ban
Fail2Ban's main configuration is in `/etc/fail2ban/jail.conf`. **Never edit `jail.conf` directly,** as your changes will be overwritten during updates. Instead, create a `jail.local` file, which overrides settings from `jail.conf`.
1. Create a `jail.local` file:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
2. Open `jail.local` for editing:
sudo nano /etc/fail2ban/jail.local
3. Configure Global Settings (at the top of the file):
# Default banning action (uses firewall)
banaction = ufw # Use 'ufw' for Ubuntu's Uncomplicated Firewall
# IPs to ignore (YOUR_TRUSTED_IP_ADDRESS is CRITICAL)
ignoreip = 127.0.0.1/8 YOUR_TRUSTED_IP_ADDRESS/32 # Add your own public IP here!
# Default ban time (e.g., 1 hour)
bantime = 1h
# Default find time (e.g., 10 minutes)
findtime = 10m
# Default max retries
maxretry = 5
4. Enable Specific Jails:
Scroll down in `jail.local` to find the `[DEFAULT]` section, then further down to individual `[jail_name]` sections. To enable a jail, change `enabled = false` to `enabled = true`.
Enable `sshd` jail (highly recommended):
[sshd]
enabled = true
# port = ssh
# logpath = %(sshd_log)s
# backend = %(sshd_backend)s
Enable other common jails (if you use these services):
[apache-auth]
enabled = true
[apache-badbots]
enabled = true
[vsftpd]
enabled = true
[mysqld-auth]
enabled = true
[nginx-http-auth]
enabled = true
Save the `jail.local` file (`Ctrl+O`, `Enter`) and exit `nano` (`Ctrl+X`).
After modifying `jail.local`, you must restart the Fail2Ban service for the changes to take effect.
sudo systemctl restart fail2ban
Verify service status:
sudo systemctl status fail2ban
Use the `fail2ban-client` command to check which jails are enabled and their current status.
1. Check overall Fail2Ban status:
sudo fail2ban-client status
2. Check status of a specific jail (e.g., `sshd`):
sudo fail2ban-client status sshd
To ensure Fail2Ban is working, you can deliberately trigger a ban from a non-whitelisted IP address. **Do NOT use your whitelisted IP for this test!** Use a different machine or a mobile hotspot if available.
1. From a different machine/IP, attempt failed SSH logins:
ssh baduser@YOUR_SERVER_PUBLIC_IP
2. After exceeding the `maxretry`, attempt another login:
3. Verify the ban on your server:
sudo fail2ban-client status sshd
sudo tail -f /var/log/fail2ban.log
# Watch Fail2Ban's log for activity
If you accidentally ban a legitimate IP address (or want to manually unban your test IP), you can do so before the `bantime` expires.
sudo fail2ban-client set sshd unbanip YOUR_BANNED_IP_ADDRESS
Confirm your Fail2Ban setup is functional and actively protecting your server:
You have successfully installed and configured Fail2Ban on your Ubuntu server, significantly enhancing its protection against brute-force attacks. This crucial step in server hardening will keep your services more secure and stable.
Consider these advanced steps and best practices for even more robust security:
Need Expert Server Security or Hardening Solutions? Contact Us!