Fail2Ban is a simple, yet useful tool that can monitor your server from malicious attack and block them before they can wreak havoc.
Installation
Fail2Ban is available in the Ubuntu repository, so you can easily install with the command: For Red Hat or Centos users, you can install Fail2Ban via the EPEL repository. Once you have installed Fail2Ban, the next step is to move the configuration file to a local folder so you won’t change the main configuration file accidentally.
Configuration
Once installed, Fail2Ban will automatically start to monitor your server for malicious attack. In most cases, the default settings are sufficient to protect your site, but if you have some custom server configuration (like different port for SSH access) or want to enable other services, here is how you can do so: The configuration file is divided into different sections. The first one that you will come across is [DEFAULT]. The few parameters that you need to take note here is ignoreip, bantime and maxretry.
ignoreip – this is the place where you whitelist the IP that you don’t want to block. The default is the localhost (127.0.0.1/8). You can add additional IPs to the field, separating each IP with a space.bantime – this is the amount of time in seconds to block the IP from accessing your server. The default is 10 minutes (600 seconds)maxretry -this is the number of failed login attempt before the IP is blocked.
There is also the destemail field where you can specify an email address for it to notify where a malicious attack is detected. One thing to note is that this will work only if you have a mail server installed. The next section is the “Actions”. Most of the settings here can be left default unless you want to change the banaction and protocol. The default “banaction” is via the IPTable. You can get it to use the multi-port setting or create a new IpTable for this. The default value for the “protocol” is tcp, but you can change it to udp , depending on which connection you are using. The last part of the “Jail” section where you can configure Fail2Ban to monitor your Apache server, FTP server, mail server and DNS server. In most cases, you just have to change the “enabled = false” setting to “enabled = true” and it will be activated for that service. If you are not using the usual port 20 for SSH, you can also change the port number in the setting above. Once you are done with the configuration, press “Ctrl + o” to save and “ctrl + x” to exit. Lastly, restart the Fail2Ban service with the command: What other ways do you use to protect your SSH server?