fail2ban commands cheat sheet
Installing and starting
-
sudo apt install fail2banInstalls fail2ban on Ubuntu and Debian, from the official distribution repositories.
-
sudo dnf install epel-releaseOn RHEL-family distributions, such as AlmaLinux and Rocky Linux, the fail2ban package comes from the EPEL repository, so add it first.
-
sudo dnf install fail2banInstalls fail2ban on Fedora, and on AlmaLinux and Rocky Linux after adding the EPEL repository.
-
sudo systemctl enable --now fail2banStarts the service now and sets it to start automatically when the server boots.
-
sudo systemctl status fail2banShows whether the service is running and its latest messages, for example an error in a configuration file.
-
sudo systemctl restart fail2banRestarts the whole service. To apply configuration changes, a reload with fail2ban-client reload is usually enough.
-
sudo fail2ban-client pingChecks that the fail2ban service is running and responds to commands.
-
sudo fail2ban-client versionShows the installed fail2ban version.
Configuration files
-
/etc/fail2ban/jail.confThe 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.localThe 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.localChanges 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 -tTests the configuration. Run it after every edit and before reloading.
-
sudo fail2ban-client --dpShows 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 = 1hHow 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 = 10mThe time window in which failed attempts are counted. The default is 10m.
-
maxretry = 5The 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.10Addresses 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 = truePrevents banning the server's own addresses. The default is true.
-
backend = systemdReads 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.logThe 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 = ufwBans through UFW. The UFW firewall must be enabled, or the ban has no effect.
-
banaction = firewallcmd-ipsetBans through firewalld, on servers where firewalld manages the firewall.
-
banaction = nftables-multiportBans 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-allportsThe 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.localCreates 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 = 2222When 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 = aggressiveThe detection level of the sshd filter: normal (the default), ddos, extra, or aggressive, which combines them all.
-
maxretry = 3In the [sshd] section only: a stricter limit for SSH than for the other jails.
Incremental bans
-
bantime.increment = trueExtends 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 = 604800The maximum time a ban can reach, here one week in seconds.
-
bantime.overalljails = trueLooks 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.localEnables 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 statusShows the number of active jails and their names.
-
sudo fail2ban-client status sshdShows 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 --allShows the status of all jails at once. Available in recent versions of fail2ban.
-
sudo fail2ban-client statsShows 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 bannedShows the jails that have banned addresses, and the addresses in each of them.
-
sudo fail2ban-client banned 203.0.113.10Checks 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-timeShows the banned addresses in the jail, along with the ban time and the unban time.
-
sudo fail2ban-client get sshd maxretryShows an active setting of the jail. Likewise: get sshd bantime and get sshd findtime.
-
sudo fail2ban-client get sshd ignoreipShows the list of addresses that are never banned in the jail.
-
sudo fail2ban-client get sshd logpathShows the log files the jail watches.
-
sudo fail2ban-client get sshd actionsShows the actions the jail runs when it bans.
Banning and unbanning addresses
-
sudo fail2ban-client set sshd unbanip 203.0.113.10Unbans an address in a specific jail. You can write several addresses separated by spaces.
-
sudo fail2ban-client unban 203.0.113.10Unbans the address in all jails and also deletes it from the fail2ban database.
-
sudo fail2ban-client unban --allUnbans 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.7Bans an address manually in the jail, for the jail's ban time.
-
sudo fail2ban-client set sshd addignoreip 203.0.113.10Adds 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.10Removes an address from the list of addresses that are never banned, in the running service.
-
sudo fail2ban-client set sshd bantime 3600Changes the jail's ban time in the running service, in seconds. This change is also lost on reload.
Reloading and stopping
-
sudo fail2ban-client reloadReloads the configuration without restarting the service. Run it after every change to the configuration files.
-
sudo fail2ban-client reload sshdReloads the configuration of a single jail only.
-
sudo fail2ban-client restart sshdFully restarts a single jail, rather than only reloading its configuration.
-
sudo fail2ban-client stop sshdStops 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.confRuns 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 sshdTests 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 sshdShows all the lines the filter detected. The --print-all-missed option shows the lines that weren't detected.
-
sudo tail -f /var/log/fail2ban.logShows the fail2ban log in real time: addresses found, bans, and unbans.
-
sudo grep 'Ban ' /var/log/fail2ban.logShows all the bans recorded in the log.
-
sudo journalctl -u fail2banShows the service messages in the systemd journal, for example errors at startup.
-
sudo fail2ban-client get logtargetShows where fail2ban writes its log. In the settings that ship with the software, it's /var/log/fail2ban.log.
-
sudo fail2ban-client get dbfileShows the location of the database where bans are stored, so they're restored after a restart.
No matching commands.
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.
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.
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.