Protecting SSH with Fail2Ban

About fail2ban

If a server is exposed to the Internet, attackers will scan that server for as many vulnerabilities as possible. That’s just part of running a server - so how do we deal with these attacks? Fail2Ban is an essential tool for server administrators. Fail2Ban works by scanning logs and detecting patterns in them, blocking IP addresses who attempt malicious activities (continual failed logins, mass logins, etc). This guide will detail how to configure Fail2Ban on a server running CentOS 6 or Debian Wheezy.

Step One - Installing Packages

On Debian 7, run:


sudo apt-get update && sudo apt-get install fail2ban


On CentOS 6, run:


rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && yum install fail2ban

Step Two - Configuration

Fail2Ban’s configuration file is located in /etc/fail2ban/jail.conf. In the event something breaks with the configuration file when we edit it, we should have a backup of the original copy on hand.


cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.conf.bak


Open the configuration file:


nano /etc/fail2ban/jail.conf


This is the default configuration file:


[DEFAULT]

# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not

# ban a host which matches an address in this list. Several addresses can be

# defined using space separator.

ignoreip = 127.0.0.1

# "bantime" is the number of seconds that a host is banned.

bantime  = 3600

# A host is banned if it has generated "maxretry" during the last "findtime"

# seconds.

findtime  = 600

# "maxretry" is the number of failures before a host get banned.

maxretry = 3


Enter all IP addresses you wish to grant immunity from Fail2Ban. All desired IP addresses and ranges should be separated by a single space, as shown below.


ignoreip = 127.0.0.1 192.73.232.13 2604:0180::/48


Now, we decide on bantime, findtime, and maxretry values. bantime refers to the length of the ban against an IP or range initiated by Fail2Ban. findtime sets the grace period for a user, in other words, how long each failed login attempt counts as a strike against an IP. maxretry tells Fail2Ban how many failed login attempts a user is given before they are banned.


Step Three - Restart Fail2Ban

To restart Fail2Ban, run the following command:


sudo service fail2ban restart

  • 345 utilizatori au considerat informația utilă
Răspunsul a fost util?

Related Articles

How to install and configure OpenVPN

This howto will show you how to install OpenVPN inside an OpenVZ VPS on Ubuntu.OpenVZ supports...

How to change your VPS date/time?

Since we have a lot of people from all around the world almost anyone will need to have his VPS...

How to enable PPP (PPPD) on a OpenVZ VPS

In order to activate PPP (PPPD) on your OpenVZ VPS log into Hypanel, go to Machine...

How to enable swap inside your VPS?

A lot of people are asking about swap in an OpenVZ VPS. Usually swap is not good but in some...

How to enable TUN/TAP on OpenVZ VPS?

In order to activate TUN/TAP on your OpenVZ VPS log into Hypanel, go...