How To Protect SSH With Fail2Ban on Ubuntu 26.10
What is Fail2Ban?
Fail2Ban is an intrusion prevention tool written in Python. It monitors logs from services like SSH, ProFTP, and Apache and uses firewall rules to ban IP addresses that show malicious behavior, such as repeated failed login attempts.
Installing the Fail2Ban Package
Update the System
Update and upgrade your system packages:
apt update -y
apt upgrade -y
Install Fail2Ban
Install Fail2Ban using the following command:
apt install fail2ban -y
Enable the Fail2Ban service to start at boot:
systemctl enable fail2ban
Start the Fail2Ban service:
systemctl start fail2ban
Check the service status:
systemctl status fail2ban
Output:
● fail2ban.service - Fail2Ban Service
Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; preset:>
Active: active (running) since Wed 2026-10-21 04:53:10 UTC; 11h ago
Invocation: 7964aca8feaa4206b19b2cf3af917ee3
Docs: man:fail2ban(1)
Main PID: 916 (fail2ban-server)
Tasks: 7 (limit: 8830)
Memory: 26.5M (peak: 31M)
CPU: 1min 59.039s
CGroup: /system.slice/fail2ban.service
└─916 /usr/bin/python3 /usr/bin/fail2ban-server -xf start
Oct 21 04:53:10 vps.server.com systemd[1]: Started fail2ban.service - Fail2Ban >
Oct 21 04:53:12 vps.server.com fail2ban-server[916]: Server ready
Configuring Fail2Ban
Create a Local Configuration File
Instead of editing the default jail.conf file, create a local configuration file:
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Open the jail.local file for editing:
nano /etc/fail2ban/jail.local
Whitelisting IP Addresses
Find the ignoreip setting and configure it to exclude trusted IP addresses from bans:
ignoreip = 127.0.0.1/8 ::1 192.168.0.100 192.168.1.0/24
You can add multiple IP addresses or subnets separated by spaces. Replace the example addresses with your trusted IP addresses.
Configure Ban Settings
Adjust the following parameters in the [DEFAULT] section of the configuration file:
bantime = 10m
findtime = 10m
maxretry = 5
bantime: Duration for which an IP address is banned, such as 10 minutes.findtime: Time window during which repeated failures are counted.maxretry: Number of failed attempts allowed before an IP address is banned.
Note: Setting
bantimeto a negative value creates a permanent ban. Use this carefully to avoid blocking legitimate users.
Enable Email Notifications (Optional)
Note: An SMTP server and the required mail utilities must be configured for email notifications to work.
Find the action_mwl setting and configure it as follows:
action_mwl = %(action_)s
%(mta)s-whois-lines[sender="%(sender)s", dest="%(destemail)s", logpath="%(logpath)s", chain="%(chain)s"]
Configure the sender and recipient email addresses:
destemail = [email protected]
sender = [email protected]
Replace these example addresses with your actual email addresses.
Apply the Configuration Changes
Restart Fail2Ban to apply the changes:
systemctl restart fail2ban
Verify that the service is running:
systemctl status fail2ban
Using the Fail2Ban Client
Check Available Commands
View the available Fail2Ban client options:
fail2ban-client -h
Check Jail Status
Check the status of the SSH protection jail:
fail2ban-client status sshd
Unban an IP Address
To unban an IP address from the SSH jail:
fail2ban-client set sshd unbanip "IP_ADDRESS_HERE"
Replace IP_ADDRESS_HERE with the actual IP address.
Ban an IP Address
To manually ban an IP address:
fail2ban-client set sshd banip "IP_ADDRESS_HERE"
Replace IP_ADDRESS_HERE with the IP address you want to ban.
Important: Before applying these settings, make sure your own IP address is trusted and that you have an alternative way to access your server in case you accidentally block your SSH connection.
Your Ubuntu 26.10 server is now better protected against SSH brute-force attacks using Fail2Ban.
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS