Fail2Ban is an intrusion prevention framework written in Python (programming language). It checks log files and uses firewall such as iptables and TCP Wrapper to ban IP which makes too many login failure.
As mentioned, it uses
iptables (or other applicable firewall applications) should be installed beforehand.
Check if it is already installed. If it is not installed yet, install it first.
$ sudo apt-get install iptables
Fail2ban also uses
sendmail mail transfer agent (MTA) to send an email in order to report the failure of login. This can be optional, yet it is better to have the report thus installing
sendmail is recommended.
$ sudo apt-get install sendmail
$ sudo apt-get install fail2ban
Create Configuration File (
It has a default configuration file which is
jail.conf in the
/etc/fail2ban directory. However, using
/etc/fail2ban/jail.local file is encouraged. If there is no such a file, create one and copy the contents of the
jail.conf file. Or just simply copy
/etc/fail2ban/jail.conf file to
$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
[ssh] section and change like:
[ssh] enabled = true # port = ssh port = ssh,sftp filter = sshd logpath = /var/log/auth.log maxretry = 3 action = iptables[name=SSH, port=ssh, protocol=tcp] sendmail-whois[name=SSH, email@example.com]
port = ssh is comment out yet
sftp are set instead.
- It will email you when it detects the failure of the login attempt which happened more than
maxretry, 3 in this example, after banning the IP, from which the attempt of the access is.
- If you changed the port number used for sshd, then change
action = iptables[name=SSH, port=ssh, protocol=tcp]
action = iptables[name=SSH, port=<port number>, protocol=tcp]
e.g.) action = iptables[name=SSH, port=1234, protocol=tcp]
How long the IP is banned can be found from the
[DEFAULT] # "ignoreip" can be an IP address, a CIDR mask or a DNS host ignoreip = 127.0.0.1 bantime = 600 maxretry = 3
Change it as you wish (it is seconds).
$ sudo /etc/init.d/fail2ban restart
There is a known bug in fail2ban from Ubuntu (8.04) repository. The bug is that fail2ban is not started after rebooting. The bug was reported here.
This can be solved by adding the line below to
[ -d /var/run/fail2ban ] || mkdir -p /var/run/fail2ban
So it should be like this.
DAEMON_ARGS="$DAEMON_ARGS -x" fi # Assure that /var/run/fail2ban exists. This line is added. [ -d /var/run/fail2ban ] || mkdir -p /var/run/fail2ban start-stop-daemon --start --quiet --chuid root --exec $DAEMON -- \