{"id":2392,"date":"2015-11-05T15:08:24","date_gmt":"2015-11-05T13:08:24","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/install-fail2ban-centos\/"},"modified":"2015-11-05T15:08:24","modified_gmt":"2015-11-05T13:08:24","slug":"install-fail2ban-centos","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/install-fail2ban-centos\/","title":{"rendered":"How to install Fail2ban on CentOS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/upcloud.com\/global\/resources\/tutorials\/install-fail2ban-ubuntu\/\">Fail2ban<\/a> is an intrusion prevention framework which works together with a packet-control system or firewall installed on your server and is commonly used to block connection attempts after several failed tries.<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Installing Fail2ban<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It operates by monitoring log files for certain types of entries and runs predetermined actions based on its findings. Since Fail2ban is not available directly from CentOS, you need to install EPEL first before you can install Fail2ban. EPEL contains additional packages for Enterprise Linux, including CentOS.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install epel-release<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo yum install fail2ban<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once installed, copy the default jail.conf file to make a local configuration with this command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo cp \/etc\/fail2ban\/jail.conf \/etc\/fail2ban\/jail.local<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then open the new local configuration file for edit with your favourite text editor, for example<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo vi \/etc\/fail2ban\/jail.local<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Scroll down to go through some of the settings available in the configuration file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First up are the basic defaults for <em>ignoreip<\/em>, which allows you to exclude certain IP addresses from being banned, for example, if your computer has a fixed IP you can enter it here. Next set the <em>bantime <\/em>which determines how long an offending host will remain blocked until automatically unblocked. Lastly check the <em>findtime<\/em> and <em>maxretry<\/em> counts, of which the find time sets the time window for the max retry attempts before the host IP attempting to connect is blocked.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[DEFAULT]\nignoreip = 127.0.0.1\/8 ::1\nbantime  = 10m\nfindtime = 10m\nmaxretry = 5<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you have a sendmail service configured on your cloud server, you can enable the email notifications from Fail2ban by entering your email address into the parameter <em>destemail<\/em> and changing the <em>action = %(action_)s<\/em> to <em>action = %(action_mw)s.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019ve done the basic configurations, check the different jails available in the configuration options. Jails are the rules which fail2ban applies to any given application or log file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SSH jail settings, which you can find at the top of the jails list, are disabled by default. You should turn them on&nbsp;by adding <em>enabled = true <\/em>in the SSH jail section.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[sshd]\nenabled = true<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can enable any other jail modules in the same fashion by adding the&nbsp;same line just below any jail header like <em>[sshd]<\/em> in this example above.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you\u2019ve enabled all the jails you wish, save the configuration file and exit the editor. Then you\u2019ll need to restart the monitor with the following command<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo systemctl restart fail2ban<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With that done, you should now check your iptable rules for the newly added jail sections on each of the application modules you enabled.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo iptables -L<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Any banned IP addresses will appear in the specific chains where the failed login attempts occurred at. You can also manually ban and unban IP addresses from the services you defined jails for with the following commands.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo fail2ban-client set &lt;jail&gt; banip\/unbanip &lt;ip address&gt;\n# For example\nsudo fail2ban-client set sshd unbanip 83.136.253.43<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Fail2ban is a handy addition to a firewall access control in general, feel free to experiment with the configuration and don\u2019t worry if you get your IP address banned, you can always log in through the web Console at your <a href=\"https:\/\/my.upcloud.com\/server\/list\" target=\"_blank\" rel=\"noopener\">UpCloud Control Panel<\/a> to unban yourself afterwards.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\/\">Test hosting on UpCloud!<\/a><\/div>\n<\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","community-category":[253],"class_list":["post-2392","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2392"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2392\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2392"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}