Remove Fail2Ban and all configuration files from Ubuntu Server for a clean system.
sudo systemctl stop fail2ban
Clear all current bans before uninstalling:
sudo fail2ban-client unban --all
sudo systemctl disable fail2ban
Use purge to remove the package and its configuration files:
sudo apt purge fail2ban -y
Remove packages that were installed as dependencies:
sudo apt purge python3-pyinotify python3-pyasyncore -y
sudo apt autoremove -y
Delete all remaining configuration files and directories:
sudo rm -rf /etc/fail2ban
sudo rm -f /var/log/fail2ban.log
sudo rm -f /var/log/fail2ban.log.*
sudo rm -rf /var/run/fail2ban
sudo rm -rf /var/lib/fail2ban
Remove any remaining iptables rules created by fail2ban:
sudo iptables -L -n --line-numbers
Warning: This removes ALL custom iptables rules, not just fail2ban rules.
sudo iptables -F
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
Remove all DROP rules from INPUT chain:
while sudo iptables -D INPUT -j DROP 2>/dev/null; do :; done
sudo rm -f /etc/systemd/system/fail2ban.service
sudo rm -f /etc/systemd/system/multi-user.target.wants/fail2ban.service
sudo systemctl daemon-reload
dpkg -l | grep fail2ban
Expected output: No results or status shows "rc" (removed, config files remain)
ls -la /etc/fail2ban
Expected output: ls: cannot access '/etc/fail2ban': No such file or directory
ls -la /var/log/fail2ban*
Expected output: ls: cannot access '/var/log/fail2ban*': No such file or directory
sudo systemctl status fail2ban
Expected output: Unit fail2ban.service could not be found.
sudo iptables -L INPUT -n
Expected output: No fail2ban related DROP rules
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
systemctl stop fail2banfail2ban-client unban --allsystemctl disable fail2banapt purge fail2banapt purge python3-pyinotify python3-pyasyncoreapt autoremoverm -rf /etc/fail2banrm -f /var/log/fail2ban.log*rm -rf /var/run/fail2ban /var/lib/fail2baniptables -Fsystemctl daemon-reload