Complete Fail2Ban Uninstall Guide

Remove Fail2Ban and all configuration files from Ubuntu Server for a clean system.

1. Stop Fail2Ban Service

sudo systemctl stop fail2ban

2. Unban All IPs

Clear all current bans before uninstalling:

sudo fail2ban-client unban --all

3. Disable the Service

sudo systemctl disable fail2ban

4. Remove Fail2Ban Package

Use purge to remove the package and its configuration files:

sudo apt purge fail2ban -y

5. Remove Dependencies

Remove packages that were installed as dependencies:

sudo apt purge python3-pyinotify python3-pyasyncore -y

6. Clean Up Unused Packages

sudo apt autoremove -y

7. Remove Configuration Directories

Delete all remaining configuration files and directories:

sudo rm -rf /etc/fail2ban

8. Remove Log Files

sudo rm -f /var/log/fail2ban.log
sudo rm -f /var/log/fail2ban.log.*

9. Remove Runtime Files

sudo rm -rf /var/run/fail2ban
sudo rm -rf /var/lib/fail2ban

10. Clean IPTables Rules

Remove any remaining iptables rules created by fail2ban:

10.1 View Current Rules

sudo iptables -L -n --line-numbers

10.2 Flush All Custom Rules (if needed)

Warning: This removes ALL custom iptables rules, not just fail2ban rules.

sudo iptables -F

10.3 Remove Specific /24 Subnet Bans

If you only want to remove the /24 subnet bans:

# List rules with line numbers
sudo iptables -L INPUT -n --line-numbers

# Remove specific rule by line number (example: line 1)
sudo iptables -D INPUT 1

10.4 Remove All DROP Rules

Remove all DROP rules from INPUT chain:

while sudo iptables -D INPUT -j DROP 2>/dev/null; do :; done

11. Remove Systemd Service Files

sudo rm -f /etc/systemd/system/fail2ban.service
sudo rm -f /etc/systemd/system/multi-user.target.wants/fail2ban.service
sudo systemctl daemon-reload

12. Verify Complete Removal

12.1 Check Package Status

dpkg -l | grep fail2ban

Expected output: No results or status shows "rc" (removed, config files remain)

12.2 Check Configuration Directory

ls -la /etc/fail2ban

Expected output: ls: cannot access '/etc/fail2ban': No such file or directory

12.3 Check Log Files

ls -la /var/log/fail2ban*

Expected output: ls: cannot access '/var/log/fail2ban*': No such file or directory

12.4 Check Service Status

sudo systemctl status fail2ban

Expected output: Unit fail2ban.service could not be found.

12.5 Check IPTables

sudo iptables -L INPUT -n

Expected output: No fail2ban related DROP rules

Quick One-Liner

Complete uninstall in a single command sequence:

sudo systemctl stop fail2ban && \
sudo fail2ban-client unban --all 2>/dev/null; \
sudo systemctl disable fail2ban 2>/dev/null; \
sudo apt purge fail2ban python3-pyinotify python3-pyasyncore -y && \
sudo apt autoremove -y && \
sudo rm -rf /etc/fail2ban /var/run/fail2ban /var/lib/fail2ban && \
sudo rm -f /var/log/fail2ban.log* && \
sudo iptables -F && \
sudo systemctl daemon-reload

Summary Checklist

  1. Stop service — systemctl stop fail2ban
  2. Unban all IPs — fail2ban-client unban --all
  3. Disable service — systemctl disable fail2ban
  4. Purge package — apt purge fail2ban
  5. Remove dependencies — apt purge python3-pyinotify python3-pyasyncore
  6. Autoremove — apt autoremove
  7. Delete /etc/fail2ban — rm -rf /etc/fail2ban
  8. Delete logs — rm -f /var/log/fail2ban.log*
  9. Delete runtime — rm -rf /var/run/fail2ban /var/lib/fail2ban
  10. Clean iptables — iptables -F
  11. Reload systemd — systemctl daemon-reload

How Fail2Ban Integrates with the System

What Fail2Ban Leaves on a System Packagefail2ban + deps Config/etc/fail2ban State DB/var/lib/fail2ban Logs/var/log/fail2ban.log Firewall chainsf2b-sshd etc. INPUT jump rules-j f2b-sshd systemd unitenabled service apt purge removes the package, its config and the unit file; the firewall rules live in the kernel and must be checked separately.

Fail2Ban is a Python daemon that tails log files (or the systemd journal), matches lines against per-service filters (regular expressions in filter.d/), and when an IP exceeds maxretry failures within findtime, runs a ban action from action.d/ for bantime. With the default iptables actions, each jail creates its own chain (for example f2b-sshd) and inserts a jump to it from INPUT; bans are individual REJECT or DROP rules inside that chain. Newer Debian and Ubuntu releases may default to the nftables actions, which create a separate inet f2b-table. Banned IPs are also persisted in a SQLite database under /var/lib/fail2ban so they can be restored after a restart.

That design explains the uninstall steps. Stopping the service normally runs each action's actionstop, which flushes and deletes the chains it created. If the daemon crashed or was killed, those chains remain and keep blocking addresses after the package is gone, which is the main thing to verify.

Safe Removal Order

Safe Removal Order Stop serviceactionstop cleans Verify chains goneiptables / nft Purge packageapt purge Remove leftoversdirs, logs Verifydpkg, systemctl Stopping the service first lets fail2ban run each action's actionstop, which deletes its own chains and jump rules.
# 1. See what fail2ban currently manages
sudo fail2ban-client status
sudo iptables -S | grep -i f2b
sudo nft list ruleset 2>/dev/null | grep -i f2b

# 2. Stop cleanly (runs actionstop for every jail)
sudo systemctl stop fail2ban

# 3. If any f2b chains survived, remove ONLY those, not the whole ruleset
for c in $(sudo iptables -S | awk '/^-N f2b-/{print $2}'); do
  sudo iptables -D INPUT -j "$c" 2>/dev/null
  sudo iptables -F "$c"
  sudo iptables -X "$c"
done
sudo nft delete table inet f2b-table 2>/dev/null

# 4. Then purge and clean up as in the steps above
sudo apt purge -y fail2ban && sudo apt autoremove -y

This targeted cleanup is safer than iptables -F or deleting every DROP rule in INPUT, both of which also remove rules you or other tools added on purpose. If IPv6 is in use, repeat the checks with ip6tables.

When to Uninstall, and What to Use Instead

OptionWhat it doesNotes
Keep fail2ban, tune jailsadjust bantime, findtime, ignoreipoften enough when false positives were the problem
Disable one jailenabled = false in jail.localkeeps protection for other services
SSH keys only, no passwordsPasswordAuthentication noremoves the brute-force risk fail2ban mitigates
Static firewall allowlistiptables/nftables or ufw rulesstrongest for admin ports with known source IPs
CrowdSeclog-based detection with shared blocklistsdifferent architecture, separate bouncer component

Uninstalling is reasonable when moving to a different tool, when the server is behind a load balancer or cloud firewall that already filters traffic, or when debugging connectivity problems. Before removing it from an internet-facing server, make sure SSH and any login forms are protected another way.

Pitfalls