Securing your Linux server is a critical task for every sysadmin. One of the simplest yet most effective ways to guard against brute-force login attempts is by using Fail2Ban. In this article, we’ll walk through how to install, configure, and customize Fail2Ban on Ubuntu.
Fail2Ban is a Python-based intrusion prevention system that monitors system logs and automatically bans IP addresses that show malicious signs, such as too many failed login attempts. It does this by updating firewall rules to reject those IPs for a defined period.
Fail2Ban is available in Ubuntu’s default repository. To install it:
sudo apt update
sudo apt install fail2ban
Once installed, enable and start the service:
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
Instead of editing the default config file, we copy it to a local configuration file to preserve upgrades:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Now open the jail.local
file:
sudo nano /etc/fail2ban/jail.local
Key parameters to review:
Example:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 192.168.1.100
By default, Fail2Ban comes with a jail for SSH. You can enable it by modifying or adding this section in jail.local
:
[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
maxretry = 3
This will ban an IP after 3 failed SSH login attempts.
Check that Fail2Ban is running:
sudo systemctl status fail2ban
Check active jails:
sudo fail2ban-client status
To see SSH jail statistics:
sudo fail2ban-client status sshd
If a legit IP is blocked, you can manually unban it:
sudo fail2ban-client set sshd unbanip 192.168.1.101
Want email alerts when Fail2Ban blocks an IP? In the jail.local
file:
destemail = you@example.com
sender = fail2ban@example.com
action = %(action_mwl)s
Make sure you have a mail service like sendmail
or postfix
configured.
Fail2Ban is one of the easiest ways to improve your Ubuntu server’s security. With a few lines of configuration, you can automatically block brute-force attackers and stay informed with alerts.
For advanced users, Fail2Ban can also protect services like Apache, Nginx, and even custom applications — simply by pointing to the correct log files.
Stay safe and automate your defenses!
If you run a VPS or dedicated server with Linux, you probably know that managing…
Short summary: Use built-in Linux tools (`who`, `last`, `journalctl`, `/var/log/auth.log`) and optional audit logs to…
Whether you're a new Linux user or a beginner sysadmin, mastering the terminal is a…
Choosing the right Content Management System (CMS) is key when you're building a blog, portfolio,…
As a Linux system administrator, understanding what happens on your network is crucial. One of…
MegaCLI: useful commands Here are some useful commands: View information about the RAID adapter For…
This website uses cookies.