VMoreCloud›Linux›Tutorials›Fail2ban for SSH
LINUX • CYBERSECURITY • SERVER HARDENING

How to Set Up Fail2ban for SSH on Linux

Protect your Linux server against repeated SSH authentication failures with Fail2ban. Learn how to install it, enable the SSH jail, configure ban thresholds, verify blocked IP addresses, and troubleshoot common problems on Ubuntu, Debian, and RHEL-based systems.

Author: Khurram Shahzad
Platform: Linux servers
Technology: Fail2ban & OpenSSH
Level: Beginner–Intermediate
Time: 15–25 minutes
Last reviewed: October 16, 2026
Quick answer: how to configure Fail2ban for SSH Install Fail2ban, create a local configuration that enables the sshd jail, configure reasonable retry and ban thresholds, and start the service. Verify the jail with sudo fail2ban-client status sshd. Fail2ban monitors authentication failures and invokes a firewall action to block offending IP addresses according to your policy.

Add an Automatic Defense Against SSH Brute-Force Attempts

Public-facing SSH servers can receive repeated login attempts from automated scanners and bots. Fail2ban watches relevant authentication logs and temporarily blocks IP addresses that exceed your configured failure threshold. Combined with SSH keys, restricted users, updates, and firewall rules, it adds another useful layer of defense.

FAIL2BAN
FOR SSH
MONITOR • DETECT • BAN
Security reminder Fail2ban is not a replacement for secure authentication. A ban may occur only after failed attempts are recorded and detected. Use key-based authentication, disable unnecessary root and password access, and restrict SSH to trusted networks where practical. See our SSH hardening guide before exposing a server to the internet.
01

Monitor SSH Failures

Use the SSH jail to detect repeated authentication failures in supported logs or the systemd journal.

02

Apply Temporary Bans

Define how many failures trigger a ban, the observation window, and the ban duration.

03

Verify and Maintain

Check jail status, inspect logs, test your firewall action, and recover legitimate access safely.

What Is Fail2ban?

Fail2ban is an open-source intrusion-prevention tool that monitors log events for patterns associated with repeated authentication failures. When a configured jail detects enough matching failures from a source IP address within a specified period, Fail2ban runs an action to block that address for a configured duration.

For SSH protection, the commonly used jail is named sshd. A jail combines a filter that recognizes relevant log entries with an action that applies the ban. The exact behavior depends on your Fail2ban version, distribution configuration, logging backend, and firewall action.

TermMeaningExample
JailConfiguration for a service, filter, and action.[sshd]
FilterIdentifies log entries that match the configured failure patterns.SSH authentication failures
maxretryNumber of matching failures that trigger a ban within the window.5
findtimeWindow during which failures are counted.10m
bantimeInitial duration for which an offending address is banned.1h
ActionMechanism that applies and removes the ban.Compatible firewall action
What Fail2ban does not do Fail2ban does not make weak passwords safe, patch OpenSSH vulnerabilities, or guarantee that all suspicious traffic will be detected. It helps limit repeated failures when the relevant events are logged and the configured filter and action work correctly. The official project recommends strong authentication rather than relying on rate-limiting bans alone.

Requirements and Safety Checks

  • A Linux server with administrative privileges.
  • An SSH server such as OpenSSH, with authentication events logged.
  • Package-manager access to install Fail2ban.
  • A supported firewall and compatible Fail2ban ban action.
  • A second trusted session or console/recovery access in case a legitimate client is blocked.
  • Knowledge of your actual SSH port and server logging configuration.

The commands below cover Ubuntu/Debian and Rocky Linux/AlmaLinux-style systems. Package names, service behavior, firewall tools, and logging defaults can differ by distribution and version.

Step 1: Install Fail2ban

1

Install on Ubuntu or Debian

sudo apt update sudo apt install fail2ban

The distribution package normally provides the Fail2ban service, client utility, default configuration, filters, and actions. Review the package version and local distribution documentation if your server uses a customized image.

2

Install on Rocky Linux or AlmaLinux

sudo dnf install epel-release sudo dnf install fail2ban

On RHEL-compatible systems, the availability of Fail2ban and its repository can depend on the operating-system release and enabled repositories. If the package is not found, check the supported repositories for your exact distribution and version rather than installing an untrusted package.

3

Check that the client is available

fail2ban-client version

The output should show the installed Fail2ban version. If the command is not found, confirm the package installation and the executable’s location.

Do not assume the jail is active just because installation succeeded The package can be installed while the SSH jail remains disabled or the service is stopped. You will explicitly enable the jail and verify its status in the following steps.

Step 2: Configure the SSH Jail

Fail2ban recommends keeping distribution-provided .conf files intact and placing local customizations in a .local file. This helps preserve your settings during package upgrades.

4

Create a local SSH jail configuration

On common Linux package installations, you can create a dedicated local file under /etc/fail2ban/jail.d/:

sudo nano /etc/fail2ban/jail.d/sshd.local

Add the following baseline configuration:

[sshd] enabled = true maxretry = 5 findtime = 10m bantime = 1h

Save the file. This configuration enables the SSH jail, counts five matching failures within ten minutes, and sets an initial one-hour ban. These are example starting values; adjust them to your operational requirements.

5

Alternative: use jail.local

If you prefer a single local configuration file, you can instead edit:

sudo nano /etc/fail2ban/jail.local

Add the same [sshd] section. Use either this method or the dedicated sshd.local file, and avoid maintaining conflicting duplicate settings in multiple files.

Why use a local file? The upstream Fail2ban documentation recommends customizations in .local files or appropriate local snippets rather than editing the packaged jail.conf directly. Later-loaded local configuration can override packaged defaults. Always inspect your actual configuration when a value does not behave as expected.

Step 3: Understand Ban Thresholds

Before enabling a ban policy on a production server, understand the three most important settings.

maxretry = 5

Sets the number of matching failures needed to trigger a ban within the configured observation window. Lower values react faster but can increase accidental bans.

findtime = 10m

Sets the window in which matching failures are counted. Use a duration that matches your expected authentication behavior and risk tolerance.

bantime = 1h

Sets the initial ban duration. A longer ban can reduce repeated attempts but may disrupt legitimate users behind shared or changing IP addresses.

ignoreip

Defines trusted addresses or networks that should not be banned. Add only carefully verified management addresses and avoid broad exceptions.

Example starting policy

SettingExampleOperational effect
maxretry5Ban after five matching failures during the observation window.
findtime10 minutesCount failures within a ten-minute period.
bantime1 hourBlock the offending address for the initial one-hour ban period.

These values are examples, not universal security recommendations. Consider your support processes, VPN or NAT architecture, authentication policy, and the consequences of blocking a shared IP address.

Step 4: Check Log and Backend Compatibility

Fail2ban must be able to read the SSH authentication events generated by your system. Some distributions write these events to files such as /var/log/auth.log or /var/log/secure; others may use the systemd journal without maintaining the expected log file.

6

Check the SSH service logs

On Ubuntu or Debian, you can inspect the journal with:

sudo journalctl -u ssh --since "1 hour ago"

On systems using the sshd unit, try:

sudo journalctl -u sshd --since "1 hour ago"

Some systems also record authentication events in a general authentication log or system log. Confirm where your SSH service actually records failed logins.

7

Use the systemd backend when appropriate

If your system logs SSH authentication events to the systemd journal and Fail2ban reports that it cannot find the expected log file, edit your local SSH jail file:

sudo nano /etc/fail2ban/jail.d/sshd.local

Use this configuration for a systemd-journal setup, keeping your existing thresholds:

[sshd] enabled = true backend = systemd maxretry = 5 findtime = 10m bantime = 1h

Only choose this backend if the required systemd journal integration is available and appropriate for your system. Depending on the distribution and Python packaging, the systemd bindings may also be required.

Do not add backend = systemd automatically to every server If Fail2ban is correctly monitoring an authentication log file, forcing a different backend may be unnecessary. Choose the backend based on the actual logging source and the installed Fail2ban package. A wrong backend or missing dependency can prevent the jail from starting.

Step 5: Enable and Start Fail2ban

8

Enable the service at boot

sudo systemctl enable --now fail2ban

This command enables Fail2ban to start automatically and starts the service immediately.

9

Check the service status

sudo systemctl status fail2ban --no-pager

Confirm that the service is active. If it fails to start, inspect the logs before changing the configuration again:

sudo journalctl -u fail2ban -n 100 --no-pager
Service running is only the first check A running Fail2ban service does not guarantee that the SSH jail is enabled or reading the right log source. Verify the jail and its counters in the next step.

Step 6: Verify the SSH Jail

10

Display the active jails

sudo fail2ban-client status

Look for sshd in the list of active jails. If it is absent, inspect the configuration files and Fail2ban startup logs.

11

Inspect the SSH jail in detail

sudo fail2ban-client status sshd

The output normally includes the filter status, total failed attempts, currently failed attempts, and currently banned IP addresses. The exact output varies by Fail2ban version and action.

12

Inspect effective jail configuration

sudo fail2ban-client get sshd maxretry sudo fail2ban-client get sshd findtime sudo fail2ban-client get sshd bantime

These commands help confirm the active threshold values. If a command is not supported by your installed version, consult the local fail2ban-client help and configuration.

Do not create a ban just to prove the jail works Avoid repeatedly entering incorrect credentials on a production server or attempting to ban your own management IP. Check the jail status, logs, filter matches, and firewall action first. If a functional test is required, use a controlled test environment or a safe test address and a documented procedure.

Step 7: Inspect and Manage Banned IPs

View currently banned IP addresses

sudo fail2ban-client status sshd

Review the banned-IP list and confirm that it matches the events you expect to see in your SSH authentication logs.

Check Fail2ban logs

Depending on the installation, Fail2ban may log to a file or to the systemd journal. For a systemd-managed installation, inspect:

sudo journalctl -u fail2ban --since "1 hour ago"

If your configuration writes to a file, use the actual log path defined by your installation. Check for ban and unban messages, filter errors, missing logs, and action failures.

Inspect the firewall action

Fail2ban relies on an action to apply the ban. The exact firewall mechanism can differ by distribution, Fail2ban version, and configuration. Depending on your setup, it may use nftables, iptables, or another supported action.

Important Do not flush firewall rules or manually delete firewall chains to troubleshoot Fail2ban. Doing so may remove other security controls or disrupt remote connectivity. Identify the configured action and use supported Fail2ban commands to manage its bans.

Step 8: Unban a Legitimate IP Address

A trusted administrator may be temporarily blocked after repeated failed authentication attempts. Verify the source address and the reason for the ban before removing it.

13

Remove an IP from the SSH jail ban list

sudo fail2ban-client set sshd unbanip 203.0.113.25

203.0.113.25 is a documentation-only example address. Replace it with the exact legitimate IP address you have verified. Do not copy the example literally.

14

Verify the unban

sudo fail2ban-client status sshd

Confirm that the address is no longer listed as banned. If access is still unavailable, investigate firewall rules, routing, SSH authentication settings, and the source IP actually observed by the server.

Use the narrowest recovery action Unban only the verified address that needs access. Avoid broad changes to ignoreip or the system firewall simply to resolve one administrator’s login issue.

Troubleshooting Common Fail2ban Problems

Fail2ban service will not start

Check sudo journalctl -u fail2ban -n 100 --no-pager. Inspect syntax, duplicate settings, unsupported options, missing modules, and jail startup errors.

SSH jail is missing

Confirm that enabled = true is in the correct local configuration file and that the service has loaded the changes. Inspect the active jail list and service logs.

No SSH log file found

Determine whether SSH writes to a file or systemd journal. Configure a compatible backend and install any required integration package for your distribution.

Failed attempts are not counted

Check the SSH logs, filter configuration, source addresses, and backend. Confirm that the relevant failures match the active SSH filter.

IP appears banned but can still connect

Check the active jail action, firewall backend, rule application, IPv4/IPv6 behavior, and whether traffic is reaching a different host or port.

A legitimate administrator is blocked

Verify the source IP, unban that address with the Fail2ban client, and review why authentication failures occurred before reconnecting.

Useful diagnostic commands

sudo systemctl status fail2ban --no-pager sudo journalctl -u fail2ban -n 100 --no-pager sudo fail2ban-client status sudo fail2ban-client status sshd

For an additional filter-level diagnostic, the fail2ban-regex utility can test whether a supported log source matches the configured SSH filter. The exact command depends on whether the source is a file or systemd journal and on your installed version.

SSH Security Best Practices

Fail2ban works best as part of a layered server security strategy. Combine it with strong authentication, access restrictions, network controls, and regular monitoring.

Disable direct root login

Use a named administrator account and sudo rather than logging in remotely as root.

Prefer public-key authentication

Test key-based access and disable SSH password authentication when compatible with your access policy.

Restrict permitted users

Use OpenSSH’s AllowUsers or an appropriate access policy to limit which accounts can connect.

Restrict network access

Use a VPN, management network, or firewall allowlist when possible. Confirm the rules before applying remote changes.

Keep the system patched

Apply supported operating-system, OpenSSH, and Fail2ban updates through a controlled patching process.

Monitor security events

Review authentication logs and bans, and forward important events to a central logging system when appropriate.

Continue with SSH hardening Read the companion guide: How to Harden SSH on Linux: Disable Root and Password Login. It explains root-login restrictions, password authentication, AllowUsers, configuration validation, and safe testing.

Frequently Asked Questions

What is Fail2ban used for on Linux?

Fail2ban monitors configured logs for repeated patterns such as failed SSH authentication attempts. When a jail reaches its configured threshold, an action can block the source IP address for a specified period.

Does Fail2ban automatically protect SSH after installation?

Not necessarily. You must confirm that the service is running, the SSH jail is enabled, the logging backend is compatible, and the ban action is working. Verify with fail2ban-client status and fail2ban-client status sshd.

What are reasonable Fail2ban settings for SSH?

Five failures within ten minutes with an initial one-hour ban is a possible starting configuration. These are example values rather than universal recommendations. Adjust them for your access patterns, shared IP addresses, operational needs, and security policy.

Does Fail2ban stop all SSH brute-force attacks?

No. It can reduce repeated attempts from sources detected by its filters, but it does not eliminate all attack paths. Strong SSH authentication, limited access, software updates, and network controls remain important.

Can Fail2ban work without /var/log/auth.log?

Yes, depending on the distribution and configuration. If SSH events are recorded in the systemd journal, a compatible systemd backend and its required dependencies may be appropriate. Confirm the logging source before changing the backend.

How do I check whether an IP address is banned?

Run sudo fail2ban-client status sshd and inspect the banned-IP list and jail counters. Check the Fail2ban logs as well if you need to understand why the address was blocked.

How do I unban an IP address?

After verifying the address is legitimate, run sudo fail2ban-client set sshd unbanip YOUR_IP, replacing the placeholder with the exact address. Then check the jail status again.

Should I change SSH from port 22?

Changing the port may reduce some routine automated scan noise, but it is not a substitute for strong authentication or firewall controls. If you change it, update the firewall, client configuration, and Fail2ban jail port setting where needed.

Can Fail2ban block users behind the same public IP?

Yes. If several people or systems share a public IP address, repeated failures from one of them can cause the shared address to be banned. Consider this risk when choosing thresholds and durations.

Conclusion: Protect SSH with Fail2ban

Fail2ban provides a practical way to respond automatically to repeated SSH authentication failures. Install the package, enable the sshd jail, choose sensible ban thresholds, ensure the correct log source is monitored, and verify the active jail and firewall action.

For a stronger security posture, combine Fail2ban with public-key authentication, restricted SSH users, disabled direct root login, firewall controls, and regular security updates. These controls work together; no single setting or tool guarantees that a server is secure.

Final deployment checklist
  • Install Fail2ban from a trusted distribution repository.
  • Create a local SSH jail configuration without modifying packaged defaults.
  • Enable the sshd jail and set suitable retry, window, and ban values.
  • Verify the logging backend and SSH event source.
  • Enable and start the Fail2ban service.
  • Confirm the jail appears in fail2ban-client status.
  • Inspect the SSH jail, logs, and configured ban action.
  • Test changes without risking your only management connection.
  • Document the safe procedure for checking and removing legitimate bans.
  • Harden SSH authentication using the companion SSH hardening guide.

Build a Stronger Linux Security Baseline

Explore more practical guides covering Linux administration, cloud infrastructure, virtualization, networking, and cybersecurity.

Browse All Tutorials →

Official Sources and Further Reading

Khurram Shahzad, Hybrid Cloud and Virtualization Engineer at VMoreCloud
👤 About the Author
Khurram Shahzad
Hybrid Cloud & Virtualization Engineer
Hybrid Cloud & Virtualization Engineer specializing in VMware, Azure, AWS, Windows Server, Microsoft 365, Linux, networking, backup and disaster recovery. I share practical guides, technical projects and insights into modern IT infrastructure through VMoreCloud.
🖥️ VMware 🔷 Azure ☁️ AWS 🪟 Windows Server 🐧 Linux 🛡️ Cybersecurity
📄
VMoreCloud
Cloud, IT and technical tutorials
View all articles →
Learn | Build | Share
☁️ VMoreCloud|Tech Knowledge for a Better Tomorrow
IT • Cloud • Virtualization • Security • Open Source

Technical disclaimer: Fail2ban behavior depends on the installed version, distribution defaults, log source, jail configuration, and firewall action. Test changes in a controlled environment when possible. Apply these instructions only to systems you are authorized to administer.

Leave a Reply

Your email address will not be published. Required fields are marked *