Skip to content

Fail2ban

105 commands

Watches log files for repeated failures — SSH, web logins, mail — and bans offending IPs with firewall rules.

  • intrusion prevention
  • SSH
  • firewall

Install Fail2ban

sudo apt install fail2ban

Service Management6

systemctl start fail2ban

Starts the Fail2Ban intrusion prevention service daemon.

systemctl stop fail2ban

Stops the running Fail2Ban service daemon.

systemctl restart fail2ban

Restarts the Fail2Ban service to apply new configurations.

systemctl status fail2ban

Displays the current operational status and recent logs of the Fail2Ban service.

fail2ban-server --version

Outputs the installed version of the Fail2Ban server.

fail2ban-server -t

Tests the configuration files for syntax errors without starting the server.

Client Connectivity2

fail2ban-client ping

Checks if the Fail2Ban server process is alive and responsive.

fail2ban-client echo 'test'

Sends an echo message to the server to verify communication channels.

Link

Global Status1

fail2ban-client status

Displays the global status including the number of currently active jails.

Global Operations2

fail2ban-client reload

Reloads the complete configuration and all jails without restarting the process.

Link
fail2ban-client reload --all

Explicitly reloads all configuration files and jails.

Link

Jail Inspection1

fail2ban-client status <JAIL>

Shows detailed statistics for a specific jail including current ban count and list.

Link

Jail Management3

fail2ban-client start <JAIL>

Manually starts a specific jail that is currently stopped.

Link
fail2ban-client stop <JAIL>

Manually stops a running jail without stopping the entire service.

Link
fail2ban-client reload <JAIL>

Reloads the configuration for a single specific jail.

Link

Logging Configuration5

fail2ban-client get loglevel

Retrieves the current logging verbosity level of the server.

Link
fail2ban-client set loglevel <LEVEL>

Sets the logging verbosity to a specific level (CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG).

Link
fail2ban-client get logtarget

Displays the current file path or target where Fail2Ban writes its logs.

Link
fail2ban-client set logtarget <TARGET>

Changes the log output destination to a specified file path.

Link
fail2ban-client set logtarget STDOUT

Redirects log output to standard output for debugging purposes.

Link

Database Operations2

fail2ban-client get dbfile

Shows the location of the SQLite database used for persistent ban storage.

Link
fail2ban-client flushlogs

Flushes the internal logs to the disk.

Link

Manual Banning2

fail2ban-client set <JAIL> banip <IP>

Manually bans a specific IP address in the specified jail immediately.

Link
fail2ban-client set <JAIL> banip <CIDR>

Manually bans an entire CIDR block in the specified jail.

Link

Manual Unbanning3

fail2ban-client set <JAIL> unbanip <IP>

Manually unbans a specific IP address from the specified jail.

Link
fail2ban-client unban <IP>

Unbans a specific IP address across all jails (requires version 0.11+).

Link
fail2ban-client unban --all

Removes all active bans across all jails immediately.

Link

Configuration Inspection7

fail2ban-client get <JAIL> logpath

Retrieves the log file path currently being monitored by a specific jail.

Link
fail2ban-client get <JAIL> failregex

Displays the list of failure regexes currently loaded for the jail.

Link
fail2ban-client get <JAIL> ignoreregex

Displays the list of ignore regexes currently loaded for the jail.

Link
fail2ban-client get <JAIL> maxretry

Shows the number of failures required to trigger a ban for the jail.

Link
fail2ban-client get <JAIL> findtime

Shows the time window in seconds during which failures are counted.

Link
fail2ban-client get <JAIL> bantime

Shows the duration in seconds for which an IP is banned.

Link
fail2ban-client get <JAIL> usedns

Checks if DNS resolution is enabled (yes, warn, no, raw) for the jail.

Link

Runtime Tuning3

fail2ban-client set <JAIL> maxretry <N>

Updates the maximum retry threshold for a running jail dynamically.

Link
fail2ban-client set <JAIL> findtime <SECONDS>

Updates the lookback window duration for a running jail dynamically.

Link
fail2ban-client set <JAIL> bantime <SECONDS>

Updates the ban duration for a running jail dynamically.

Link

Whitelist Management3

fail2ban-client get <JAIL> ignoreip

Lists all IP addresses and CIDR blocks currently ignored (whitelisted) by the jail.

Link
fail2ban-client set <JAIL> addignoreip <IP>

Adds a specific IP address to the ignore list of a running jail.

Link
fail2ban-client set <JAIL> delignoreip <IP>

Removes a specific IP address from the ignore list of a running jail.

Link

Action Management1

fail2ban-client get <JAIL> actions

Lists the names of the actions configured for the specified jail.

Link

Filter Validation7

fail2ban-regex <LOGLINE> <FILTER>

Tests a single log line string against a specific filter configuration.

Link
fail2ban-regex <LOGFILE> <FILTER>

Tests an entire log file against a specific filter and reports match statistics.

Link
fail2ban-regex --print-all-missed <LOGFILE> <FILTER>

Runs a regex test and prints all log lines that did NOT match the filter.

Link
fail2ban-regex --print-all-matched <LOGFILE> <FILTER>

Runs a regex test and prints only the log lines that successfully matched the filter.

Link
fail2ban-regex --print-all-ignored <LOGFILE> <FILTER>

Runs a regex test and prints lines that matched but were ignored due to ignoreregex.

Link
fail2ban-regex -D <LOGFILE> <FILTER>

Runs regex testing in debug mode, showing detailed internal processing logic.

Link
fail2ban-regex -v <LOGFILE> <FILTER>

Runs regex testing with verbose output to analyze processing speed and stats.

Link

Advanced Configuration2

fail2ban-client -d

Dumps the current configuration in a readable format for debugging start-up issues.

Link
fail2ban-client -i

Enters the interactive Fail2Ban shell mode.

Link

Log Inspection5

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

Searches the primary Fail2Ban log file for all ban events.

Link
grep 'Unban' /var/log/fail2ban.log

Searches the primary Fail2Ban log file for all unban events.

Link
tail -f /var/log/fail2ban.log

Follows the Fail2Ban log file in real-time to monitor activity.

Link
zgrep 'Ban' /var/log/fail2ban.log*

Searches for ban events in both current and rotated (gzipped) Fail2Ban logs.

Link
grep 'Found' /var/log/fail2ban.log

Searches logs for 'Found' entries indicating a filter match before a ban threshold is reached.

Link

Runtime Action Tuning3

fail2ban-client set <JAIL> action <ACTION> actionstart <CMD>

Dynamically overrides the 'actionstart' command for a specific action in a running jail.

Link
fail2ban-client set <JAIL> action <ACTION> actionban <CMD>

Dynamically overrides the 'actionban' command for a specific action in a running jail.

Link
fail2ban-client set <JAIL> action <ACTION> actionunban <CMD>

Dynamically overrides the 'actionunban' command for a specific action in a running jail.

Link

Python API1

fail2ban-python --version

Checks the version of the embedded Python interpreter used by Fail2Ban.

Link

Configuration Validation1

fail2ban-client --test

Runs a basic test to verify that the client can load the configuration modules.

Link

Database Analysis3

sqlite3 /var/lib/fail2ban/fail2ban.sqlite3 'SELECT * FROM bans LIMIT 5;'

Directly queries the persistent Fail2Ban database to view the first 5 bans.

Link
sqlite3 /var/lib/fail2ban/fail2ban.sqlite3 'SELECT count(*) FROM bans;'

Counts the total number of bans recorded in the persistent database history.

Link
sqlite3 /var/lib/fail2ban/fail2ban.sqlite3 'SELECT ip FROM bans WHERE jail="sshd";'

Retrieves all IPs ever banned by the sshd jail from the database.

Link

Backend Tuning2

fail2ban-client set <JAIL> backend <BACKEND>

Changes the log monitoring backend (polling, pyinotify, systemd, auto) for a running jail.

Link
fail2ban-client get <JAIL> backend

Displays the current log monitoring backend method being used by the jail.

Link

Log Maintenance1

truncate -s 0 /var/log/fail2ban.log

Clears the Fail2Ban log file content without deleting the file (useful during testing).

Link

SOC Monitoring5

fail2ban-client set <JAIL> addlogpath <FILE>

Dynamically adds an additional log file to be monitored by a running jail.

Link
fail2ban-client set <JAIL> dellogpath <FILE>

Dynamically removes a log file from being monitored by a running jail.

Link
awk '($(NF-1) == "Ban"){print $NF}' /var/log/fail2ban.log | sort | uniq -c | sort -n

Extracts banned IPs from logs and displays frequency counts for threat analysis.

Link
fail2ban-client set <JAIL> addfailregex <REGEX>

Injects a new failure regex into a running jail without reloading config files.

Link
fail2ban-client set <JAIL> delfailregex <INDEX>

Removes a failure regex from a running jail by its index.

Link

Blue Team Validation6

fail2ban-client set <JAIL> banip <TEST_IP>

Tests the ban action mechanism by banning a reserved test IP address.

Link
iptables -L -n | grep f2b

Verifies that Fail2Ban has correctly inserted its chains into the iptables firewall.

Link
ipset list

Lists IP sets if Fail2Ban is configured to use ipset for higher performance banning.

Link
nft list ruleset | grep f2b

Verifies Fail2Ban rules in the nftables ruleset if using nftables backend.

Link
logger -p auth.warn -t sshd 'Failed password for root from 192.0.2.200 port 22 ssh2'

Injects a fake log entry into syslog to trigger Fail2Ban detection logic.

Link
fail2ban-regex --print-all-matched <LOG> <FILTER> | wc -l

Counts the total number of historical matches for a specific filter in a log file.

Link

System Maintenance1

rm /var/lib/fail2ban/fail2ban.sqlite3

Deletes the persistent database to reset all historical ban data (requires service stop).

Link

Automation & Scripting3

fail2ban-client status | grep 'Jail list' | sed 's/.*: //g' | tr ',' '\n'

Parses the status output to generate a clean, newline-separated list of active jails.

Link
fail2ban-client set <JAIL> idle on

Sets the jail to idle mode, effectively pausing it without stopping the service.

Link
fail2ban-client set <JAIL> idle off

Resumes the jail from idle mode to active monitoring.

Link

Incident Response4

fail2ban-client get <JAIL> banip --with-time

Retrieves the list of banned IPs along with the timestamp of when they were banned.

Link
fail2ban-client -v -v start

Starts the Fail2Ban client in high verbosity mode for troubleshooting startup failures.

Link
grep 'restore ban' /var/log/fail2ban.log

Checks logs to verify if bans were successfully restored from the database after a restart.

Link
fail2ban-client set <JAIL> action <ACTION> actioncheck

Manually triggers the 'actioncheck' command to verify firewall chain integrity.

Link

Compliance & Policy2

fail2ban-client set <JAIL> banaction <ACTION>

Dynamically changes the banning action used by a jail (e.g., switching from multiport to allports).

Link
fail2ban-client get <JAIL> action <ACTION> actionban

Inspects the exact command string that will be executed when a ban occurs.

Link

Advanced Troubleshooting5

fail2ban-client --str2sec <TIME_STRING>

Converts a time string (e.g., 1d12h) into seconds to verify configuration parsing logic.

Link
fail2ban-client -x start

Starts the server and forces the removal of the socket file if it exists (socket cleanup).

Link
fail2ban-client set <JAIL> usedns no

Disables DNS resolution for a jail to improve performance and prevent DNS timeouts.

Link
fail2ban-client set <JAIL> usedns raw

Sets DNS mode to raw, processing IPs exactly as they appear in logs without resolution.

Link
python3 -c 'import fail2ban.server.failmanager; print(fail2ban.server.failmanager)'

Verifies that the Fail2Ban python modules are correctly installed and importable.

Link

Recidive Handling2

fail2ban-client set recidive banip <IP>

Manually adds an IP to the recidive jail (long-term ban) usually reserved for repeat offenders.

Link
fail2ban-regex /var/log/fail2ban.log /etc/fail2ban/filter.d/recidive.conf

Tests the recidive filter against the Fail2Ban log itself to verify repeat offender detection.

Link

Advanced Regex1

fail2ban-regex 'Dec 10 10:00:00 server sshd[123]: Invalid user test from 1.2.3.4' '<HOST>'

Tests the built-in <HOST> macro against a log line to ensure it extracts the IP 1.2.3.4.

Link

Performance Tuning1

nice -n -10 fail2ban-server -b

Starts the Fail2Ban server in the background with high CPU priority.

Link

System Integration1

fail2ban-client get <JAIL> action <ACTION> actionstop

Displays the command executed when the jail stops, useful for checking cleanup logic.

Link

State Management1

fail2ban-client set <JAIL> addignoreip <DNS_NAME>

Adds a DNS hostname to the ignore list (requires usedns to be enabled).

Link

Visual Monitoring1

watch -n 1 'fail2ban-client status sshd'

Runs the status command every second to create a real-time dashboard of the sshd jail.

Link

Bulk Operations1

for jail in $(fail2ban-client status | grep 'Jail list' | sed 's/.*: //g' | tr ',' ' '); do fail2ban-client status $jail; done

Loops through all active jails and prints the detailed status for each one.

Link

Log Rotation Support1

fail2ban-client set <JAIL> logpath <NEW_LOG_FILE>

Updates the log file path for a running jail, useful during complex log rotation schemes.

Link

Advanced Config Dump1

fail2ban-client --dump-pretty

Dumps the current configuration in a human-readable, indented format (requires newer versions).

Link

Version Compatibility1

fail2ban-client -V

Short flag to display the version of the client.

Link

Process Management1

pgrep -a fail2ban

Lists the process ID and full command line of the running Fail2Ban server.

Link

Cleanup1

fail2ban-client stop && rm -f /var/run/fail2ban/fail2ban.sock

Stops the client and forcefully removes the socket file to resolve connection refused errors.

Link