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!
As a Linux System Administrator, mastering the command line is key to efficiently managing servers,…
Introduction WireGuard is a fast, lightweight, and modern VPN protocol designed for simplicity and performance.If…
Securing your website with HTTPS is essential for privacy, SEO ranking, and user trust. Let’s…
If you manage Linux servers regularly, you know how important SSH (Secure Shell) is. Whether…
If you’re new to Linux or thinking about switching from Windows or macOS, this guide…
Introduction Site Reliability Engineering (SRE) has become a critical discipline in modern IT operations. The…
This website uses cookies.