{"id":2395,"date":"2015-11-05T15:07:09","date_gmt":"2015-11-05T13:07:09","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/install-fail2ban-debian\/"},"modified":"2015-11-05T15:07:09","modified_gmt":"2015-11-05T13:07:09","slug":"install-fail2ban-debian","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/install-fail2ban-debian\/","title":{"rendered":"How to install Fail2ban on Debian"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Fail2ban 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. You can install the software with the following.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo aptitude 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 editing with your favourite text editor, for example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo nano \/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 own computer has a fixed IP you can enter it here. <span style=\"margin: 0px;padding: 0px\">Next, set the\u00a0<em>bantime,\u00a0<\/em>which determines how long an offending host will remain blocked until automatically unblocked.<\/span> Lastly, check the\u00a0find time\u00a0and\u00a0max retry\u00a0counts, 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\nbantime  = 3600 \nfindtime = 600\nmaxretry = 3<\/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. SSH jail settings, which you can find at the top of the jails list, are enabled by default.<\/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 editing the <em>enabled<\/em> parameter to <em>true<\/em>.<\/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 service fail2ban restart<\/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. 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 the iptables and firewall access control in general; feel free to experiment with the configuration, and don\u2019t worry if you get your own 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-2395","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2395","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=2395"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2395\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2395"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}