Loading...
Loading...
Install and configure fail2ban on VPS servers to automatically ban IP addresses that show malicious signs like too many password failures, seeking exploits, or brute-force attacks.
npx skill4agent add mikr13/secure-server-setup-skills fail2ban-setupsudo apt update
sudo apt install fail2ban -ysudo yum install epel-release -y
sudo yum install fail2ban -ysudo systemctl status fail2banjail.confsudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.localsudo nano /etc/fail2ban/jail.localjail.local[DEFAULT]
# Ban time in seconds (1 hour)
bantime = 3600
# Find time window (10 minutes)
findtime = 600
# Number of failures before ban
maxretry = 3
# Destination email for notifications (optional)
destemail = admin@example.com
# Sender email
sendername = Fail2Ban
# Email action
action = %(action_)s
# Or with email: %(action_mwl)s[sshd][sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3sudo systemctl enable fail2ban
sudo systemctl start fail2bansudo systemctl status fail2ban/etc/fail2ban/jail.local[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 3
[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache*/*error.log
maxretry = 3[wordpress-auth]
enabled = true
port = http,https
filter = wordpress-auth
logpath = /var/log/auth.log
maxretry = 3[proftpd]
enabled = true
port = ftp,ftp-data,ftps,ftps-data
filter = proftpd
logpath = /var/log/proftpd/proftpd.log
maxretry = 3[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600 # 1 hour
findtime = 600 # 10 minutes
[sshd-aggressive]
enabled = true
port = ssh
filter = sshd-aggressive
logpath = /var/log/auth.log
maxretry = 1
bantime = 86400 # 24 hours
findtime = 3600 # 1 hour[recidive]
enabled = true
filter = recidive
logpath = /var/log/fail2ban.log
bantime = 604800 # 1 week
findtime = 86400 # 1 day
maxretry = 3[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.10 192.168.1.0/24[DEFAULT]
destemail = admin@example.com
sendername = Fail2Ban
mta = sendmail
# Action with email
action = %(action_mwl)s# Overall status
sudo fail2ban-client status
# Specific jail status
sudo fail2ban-client status sshd# List banned IPs for SSH
sudo fail2ban-client status sshd
# List all banned IPs
sudo fail2ban-client banned# Unban specific IP from specific jail
sudo fail2ban-client set sshd unbanip 203.0.113.100
# Unban from all jails
sudo fail2ban-client unban 203.0.113.100sudo fail2ban-client set sshd banip 203.0.113.100fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf# View fail2ban log
sudo tail -f /var/log/fail2ban.log
# View recent bans
sudo grep "Ban" /var/log/fail2ban.log
# View unbans
sudo grep "Unban" /var/log/fail2ban.log# View iptables rules
sudo iptables -L -n
# View fail2ban chains
sudo iptables -L fail2ban-sshd -n
# View UFW status
sudo ufw status numbered# Count bans by jail
sudo fail2ban-client status | grep "Jail list"
# Count current bans
sudo fail2ban-client status sshd | grep "Currently banned"
# Total bans
sudo fail2ban-client status sshd | grep "Total banned"/etc/fail2ban/fail2ban.conf # Main fail2ban configuration
/etc/fail2ban/fail2ban.local # Local fail2ban config (create if needed)
/etc/fail2ban/jail.conf # Default jail configurations (don't edit!)
/etc/fail2ban/jail.local # Local jail overrides (edit this!)
/etc/fail2ban/jail.d/ # Additional jail configs/etc/fail2ban/filter.d/ # Log file filters
/etc/fail2ban/action.d/ # Ban actions (iptables, ufw, etc.)
/var/log/fail2ban.log # Fail2ban log file/etc/fail2ban/filter.d/myapp.conf[Definition]
failregex = ^.*Failed login attempt from <HOST>.*$
^.*Invalid user .* from <HOST>.*$
ignoreregex =/etc/fail2ban/jail.local[myapp]
enabled = true
port = 8080
filter = myapp
logpath = /var/log/myapp/access.log
maxretry = 5
bantime = 3600fail2ban-regex /var/log/myapp/access.log /etc/fail2ban/filter.d/myapp.confsudo systemctl reload fail2ban# Restart fail2ban
sudo systemctl restart fail2ban
# Check status
sudo systemctl status fail2ban# Reload without restarting (keeps existing bans)
sudo fail2ban-client reload
# Reload specific jail
sudo fail2ban-client reload sshd/var/log/fail2ban.log# Check for syntax errors
sudo fail2ban-client -t
# View error logs
sudo journalctl -u fail2ban -n 50
# Check configuration
sudo fail2ban-client -d# Check jail status
sudo fail2ban-client status
# View jail configuration
sudo fail2ban-client get sshd maxretry
sudo fail2ban-client get sshd bantime
# Test filter against log
fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf# Ubuntu/Debian SSH logs
/var/log/auth.log
# CentOS/RHEL SSH logs
/var/log/secure
# Nginx logs
/var/log/nginx/error.log
/var/log/nginx/access.log# Unban your IP
sudo fail2ban-client set sshd unbanip YOUR.IP.ADDRESS
# Or stop fail2ban temporarily
sudo systemctl stop fail2banjail.confjail.localssh-hardeningfirewall-configurationauto-updates