fail2ban commands cheat sheet

This cheat sheet brings together the commands and settings of fail2ban, the tool that watches the logs on a Linux server and uses the firewall to ban IP addresses that try to guess passwords: installation, configuration files, SSH protection, ban times, an allowlist of addresses, status, unbanning addresses, and testing filters. You can search for a command, filter by topic, and copy it with one click.

Installing and starting

  • sudo apt install fail2ban

    Installs fail2ban on Ubuntu and Debian, from the official distribution repositories.

  • sudo dnf install epel-release

    On RHEL-family distributions, such as AlmaLinux and Rocky Linux, the fail2ban package comes from the EPEL repository, so add it first.

  • sudo dnf install fail2ban

    Installs fail2ban on Fedora, and on AlmaLinux and Rocky Linux after adding the EPEL repository.

  • sudo systemctl enable --now fail2ban

    Starts the service now and sets it to start automatically when the server boots.

  • sudo systemctl status fail2ban

    Shows whether the service is running and its latest messages, for example an error in a configuration file.

  • sudo systemctl restart fail2ban

    Restarts the whole service. To apply configuration changes, a reload with fail2ban-client reload is usually enough.

  • sudo fail2ban-client ping

    Checks that the fail2ban service is running and responds to commands.

  • sudo fail2ban-client version

    Shows the installed fail2ban version.

Configuration files

  • /etc/fail2ban/jail.conf

    The configuration file that ships with the software. Don't edit it, because a software update may replace it. Write your changes in .local files.

  • /etc/fail2ban/jail.local

    The file for your own changes. What you write in it overrides the same settings in jail.conf, so you only need to write the settings you change.

  • /etc/fail2ban/jail.d/

    A folder for additional configuration files, for example a separate file for each jail. Read order: jail.conf, then jail.d/*.conf, then jail.local, and finally jail.d/*.local. A setting in a file read later wins.

  • /etc/fail2ban/filter.d/

    The filters: files that define which log lines count as a failed attempt, for example sshd.conf.

  • /etc/fail2ban/action.d/

    The actions: files that define how to ban and unban an address, for example in the ufw, firewalld, or nftables firewall.

  • /etc/fail2ban/fail2ban.local

    Changes to the settings of the service itself, such as the log level and the log location. Here too, don't edit fail2ban.conf.

  • sudo fail2ban-client -t

    Tests the configuration. Run it after every edit and before reloading.

  • sudo fail2ban-client --dp

    Shows the full configuration as fail2ban reads it, after merging all the files. Handy for checking whether a setting was picked up.

Main settings

  • [DEFAULT]

    The section header in jail.local for settings that apply to all jails. Each jail can override them in its own section, for example [sshd].

  • bantime = 1h

    How long a ban lasts. The default is 10m. The value is written in seconds or with units: m for minutes, h for hours, d for days, and w for weeks.

  • findtime = 10m

    The time window in which failed attempts are counted. The default is 10m.

  • maxretry = 5

    The number of failed attempts within findtime after which the address is banned. The default is 5.

  • ignoreip = 127.0.0.1/8 ::1 203.0.113.10

    Addresses that are never banned. Add the static address you connect to the server from. You can write addresses, networks in CIDR notation, and host names, separated by spaces or commas.

  • ignoreself = true

    Prevents banning the server's own addresses. The default is true.

  • backend = systemd

    Reads events from the systemd journal instead of log files. With systemd, you don't set logpath. The default is auto, and in the sshd jail on recent Debian and Fedora releases, the setting is already systemd.

  • logpath = /var/log/auth.log

    The log file the jail watches, when you don't use systemd. On Ubuntu and Debian, the login log is /var/log/auth.log, and on RHEL-family distributions it's /var/log/secure.

  • banaction = ufw

    Bans through UFW. The UFW firewall must be enabled, or the ban has no effect.

  • banaction = firewallcmd-ipset

    Bans through firewalld, on servers where firewalld manages the firewall.

  • banaction = nftables-multiport

    Bans directly in nftables, on the jail's ports only. In the configuration files of recent Debian and Fedora releases, the default is nftables.

  • banaction_allports = nftables-allports

    The action for jails that ban the address on all ports, such as recidive.

SSH protection

  • printf '[sshd]\nenabled = true\n' | sudo tee /etc/fail2ban/jail.d/sshd.local

    Creates a file that enables the SSH jail. Caution: the command replaces an existing file with the same name. Afterward, run sudo fail2ban-client reload.

  • port = 2222

    When the SSH service runs on a different port, write the port in the [sshd] section, so the ban applies to the right port. The default is port = ssh, which means port 22.

  • mode = aggressive

    The detection level of the sshd filter: normal (the default), ddos, extra, or aggressive, which combines them all.

  • maxretry = 3

    In the [sshd] section only: a stricter limit for SSH than for the other jails.

Incremental bans

  • bantime.increment = true

    Extends the ban for an address that was banned before, based on the ban history stored in the fail2ban database. By default, the time doubles each time: 1, 2, 4, 8 times, and so on.

  • bantime.maxtime = 604800

    The maximum time a ban can reach, here one week in seconds.

  • bantime.overalljails = true

    Looks for earlier bans of the address in all jails, not only in the current jail. The default is false.

  • printf '[recidive]\nenabled = true\n' | sudo tee /etc/fail2ban/jail.d/recidive.local

    Enables recidive, which watches the fail2ban log itself and bans for a week, on all ports, addresses that were banned again and again in the last day. Caution: the command replaces an existing file with the same name.

Status and banned addresses

  • sudo fail2ban-client status

    Shows the number of active jails and their names.

  • sudo fail2ban-client status sshd

    Shows the jail status: the watched files, the number of failures, the number of bans, and the list of addresses banned right now.

  • sudo fail2ban-client status --all

    Shows the status of all jails at once. Available in recent versions of fail2ban.

  • sudo fail2ban-client stats

    Shows a summary table for all jails: how the log is read, the number of failures, and the number of bans. Available in recent versions of fail2ban.

  • sudo fail2ban-client banned

    Shows the jails that have banned addresses, and the addresses in each of them.

  • sudo fail2ban-client banned 203.0.113.10

    Checks which jails the address is banned in. Handy when a customer or an employee reports that they can't reach the server.

  • sudo fail2ban-client get sshd banip --with-time

    Shows the banned addresses in the jail, along with the ban time and the unban time.

  • sudo fail2ban-client get sshd maxretry

    Shows an active setting of the jail. Likewise: get sshd bantime and get sshd findtime.

  • sudo fail2ban-client get sshd ignoreip

    Shows the list of addresses that are never banned in the jail.

  • sudo fail2ban-client get sshd logpath

    Shows the log files the jail watches.

  • sudo fail2ban-client get sshd actions

    Shows the actions the jail runs when it bans.

Banning and unbanning addresses

  • sudo fail2ban-client set sshd unbanip 203.0.113.10

    Unbans an address in a specific jail. You can write several addresses separated by spaces.

  • sudo fail2ban-client unban 203.0.113.10

    Unbans the address in all jails and also deletes it from the fail2ban database.

  • sudo fail2ban-client unban --all

    Unbans all addresses in all jails. Caution: banned attackers also regain access until they're banned again.

  • sudo fail2ban-client set sshd banip 198.51.100.7

    Bans an address manually in the jail, for the jail's ban time.

  • sudo fail2ban-client set sshd addignoreip 203.0.113.10

    Adds an address to the list of addresses that are never banned, in the running service. The change is lost on reload, so to make it permanent, also add the address to ignoreip in the configuration file.

  • sudo fail2ban-client set sshd delignoreip 203.0.113.10

    Removes an address from the list of addresses that are never banned, in the running service.

  • sudo fail2ban-client set sshd bantime 3600

    Changes the jail's ban time in the running service, in seconds. This change is also lost on reload.

Reloading and stopping

  • sudo fail2ban-client reload

    Reloads the configuration without restarting the service. Run it after every change to the configuration files.

  • sudo fail2ban-client reload sshd

    Reloads the configuration of a single jail only.

  • sudo fail2ban-client restart sshd

    Fully restarts a single jail, rather than only reloading its configuration.

  • sudo fail2ban-client stop sshd

    Stops a jail and removes it from the running service. To start it again, run sudo fail2ban-client reload.

Testing filters and logs

  • sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf

    Runs a filter on a log file and shows how many lines were detected as failed attempts. This is how you test a new or edited filter before you enable it.

  • sudo fail2ban-regex systemd-journal sshd

    Tests the sshd filter against the systemd journal, on servers where the jail reads from the journal.

  • sudo fail2ban-regex /var/log/auth.log 'sshd[mode=aggressive]'

    Tests a filter with an option, here in aggressive mode. The quotes keep the shell from interpreting the brackets.

  • sudo fail2ban-regex --print-all-matched /var/log/auth.log sshd

    Shows all the lines the filter detected. The --print-all-missed option shows the lines that weren't detected.

  • sudo tail -f /var/log/fail2ban.log

    Shows the fail2ban log in real time: addresses found, bans, and unbans.

  • sudo grep 'Ban ' /var/log/fail2ban.log

    Shows all the bans recorded in the log.

  • sudo journalctl -u fail2ban

    Shows the service messages in the systemd journal, for example errors at startup.

  • sudo fail2ban-client get logtarget

    Shows where fail2ban writes its log. In the settings that ship with the software, it's /var/log/fail2ban.log.

  • sudo fail2ban-client get dbfile

    Shows the location of the database where bans are stored, so they're restored after a restart.

Before enabling fail2ban on a remote server

  • Your address in ignoreip: a few failed login attempts, such as a wrong password or a key that didn't load, are enough to ban the address you connect from. So add your static address to ignoreip before you enable fail2ban.
  • Keep a session open: after changing settings, keep your current SSH session open and test logging in from a new window. If your address was banned, unban it from the open session with fail2ban-client unban.
  • A ban action that matches the firewall: banaction should match the firewall that manages the server, UFW, firewalld, or nftables, so bans don't conflict with the existing rules.
  • Test before reloading: after every edit, run sudo fail2ban-client -t, and only then sudo fail2ban-client reload.

The IP addresses on this page, such as 203.0.113.10 and 198.51.100.7, belong to ranges reserved for documentation, so replace them with your real addresses. The log locations on each distribution are listed on the Linux directories and logs page, and the commands for managing the firewall itself are on the UFW commands and firewalld commands pages.

Did you know?

On a Linux VPS, you connect over SSH with full root access, and install and set up fail2ban with every command on this page: SSH protection, ban times, an allowlist of addresses, and filter testing.

Linux VPS plans

fail2ban FAQ

What is fail2ban, and how does it protect the server?

fail2ban watches the logs of the services on the server, such as SSH, and detects IP addresses that try to log in again and again without success. When an address goes over the set number of attempts within the set time window, fail2ban bans it in the firewall for a set time. This way, password guessing attempts are stopped early, and the logs stay cleaner.

How do I unban an IP address in fail2ban?

Run sudo fail2ban-client set sshd unbanip with the address, replacing sshd with the name of the jail it was banned in. To unban the address in all jails at once, run sudo fail2ban-client unban with the address. If you don't know which jail the address is banned in, the sudo fail2ban-client banned command with the address shows it.

How do I keep fail2ban from banning my own IP address?

Add the address to the ignoreip setting in the /etc/fail2ban/jail.local file, in the [DEFAULT] section, for example ignoreip = 127.0.0.1/8 ::1 203.0.113.10, and reload with sudo fail2ban-client reload. You can also write a whole network in CIDR notation. It's best to add only static addresses, such as your office address.

How do I see which addresses fail2ban has banned?

The sudo fail2ban-client status sshd command shows the number of bans and the list of addresses banned in the jail right now. The sudo fail2ban-client banned command shows the banned addresses in all jails, and sudo fail2ban-client get sshd banip --with-time adds the unban time for each address.

Does fail2ban work with UFW and firewalld?

Yes. In the banaction setting, choose the action that matches the server's firewall: ufw for a server with UFW, firewallcmd-ipset for a server with firewalld, or nftables-multiport for banning directly in nftables. The commands for managing the firewall itself are on the UFW commands and firewalld commands pages.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement