{"id":1559,"date":"2021-05-20T08:46:43","date_gmt":"2021-05-20T05:46:43","guid":{"rendered":"https:\/\/www.scalahosting.com\/kb\/?p=1559"},"modified":"2022-05-19T18:40:45","modified_gmt":"2022-05-19T15:40:45","slug":"how-to-harden-a-server-with-fail2ban","status":"publish","type":"post","link":"https:\/\/www.scalahosting.com\/kb\/how-to-harden-a-server-with-fail2ban\/","title":{"rendered":"How to Harden a Server With Fail2ban"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">When you\u2019re in the business of managing a dedicated server for your company,<\/span><b> you can never be too careful or secure.\u00a0<\/b><\/p>\n<p><span style=\"font-weight: 400;\">A server\u2019s primary function is to store files and act as a medium of sending and receiving information upon request. Hence, the need to protect it from password hackers or brute-force attacks.<\/span><\/p>\n<p><b>Fail2ban monitors <\/b><span style=\"font-weight: 400;\">repeated connection requests that fail to authenticate on a server.<\/span><\/p>\n<h2><b>What is Fail2ban?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Fail2ban is a software framework that<\/span><b> prevents intrusion from external brute-force attacks. <\/b><span style=\"font-weight: 400;\">The basic concept behind fail2ban is identifying suspicious patterns in authentication failures that may affect your computer server.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After a predetermined number of authentication errors from a server, fail2ban blocks the host&#8217;s IP address for a specific time or permanently.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The fail2ban application is written in Python and runs on Portable Operating System Interface (POSIX) systems such as <\/span><a href=\"https:\/\/www.scalahosting.com\/blog\/the-best-server-administration-tools-for-linux\/\"><b>Linux<\/b><\/a><b> and UNIX.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">It integrates with the system\u2019s firewall and enforces bans on suspicious IP addresses by adding a new rule to existing iptables<\/span><b> to block the attacking <\/b><a href=\"https:\/\/www.scalahosting.com\/blog\/what-is-my-ip\/\"><b>IP address<\/b><\/a><b>.<\/b><span style=\"font-weight: 400;\"> This keeps your server safe from botnets or scripted attacks.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We\u2019ll discuss how to install and enable fail2ban on your server in this article. Let\u2019s get started.<\/span><\/p>\n<h2><b>How to Install and Secure Fail2ban on Your Server?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Installing Fail2ban requires root access. If you don&#8217;t log into the server with the root user, the commands you see below will need to be accompanied by the <em>sudo<\/em> prefix.<br \/>\n<\/span><\/p>\n<p>Here are the exact steps:<\/p>\n<h3><b>For CentOS<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To install the fail2ban package on CentOS, do the following steps.<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> Ensure you update your system and install the EPEL repository for the system using this command:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>yum update &amp;&amp; yum install epel-release<\/b><\/li>\n<\/ul>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> Then, type in this command to install fail2ban:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>yum install fail2ban<\/b><\/li>\n<\/ul>\n<ol start=\"3\">\n<li><span style=\"font-weight: 400;\"> You can install Sendmail if you like extra email support, although you won\u2019t need it to use fail2ban. Use this command:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>yum install sendmail<\/b><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">You might encounter this error: <\/span><b>no directory<\/b> <b>\/var\/run\/fail2ban to contain the socket file \/var\/run\/fail2ban\/fail2ban.sock, <\/b><span style=\"font-weight: 400;\">while trying to install fail2ban application on CentOS.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If this happens, manually create the directory with this command:<\/span><b> mkdir \/var\/run\/fail2ban<\/b><\/p>\n<h3><b>For Fedora<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To install the fail2ban package on Fedora, do the following steps.<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> Update your system with this command:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>dnf update<\/b><\/li>\n<\/ul>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> Then install fail2ban using this command:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>dnf install fail2ban<\/b><\/li>\n<\/ul>\n<ol start=\"3\">\n<li><span style=\"font-weight: 400;\"> Optionally, install sendmail for extra email support using this code:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>dnf install sendmail<\/b><\/li>\n<\/ul>\n<h3><b>For Debian and Ubuntu<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To install the fail2ban package on Debian and Ubuntu, do the following steps.<\/span><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> Update your system using this command:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>apt-get update &amp;&amp; apt-get upgrade -y<\/b><\/li>\n<\/ul>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> Next, install fail2ban with this command:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>apt-get install fail2ban<\/b><\/li>\n<\/ul>\n<ol start=\"3\">\n<li><span style=\"font-weight: 400;\"> For extra email support using Sendmail, use this command:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>apt-get install sendmail-bin sendmail<\/b><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Now you\u2019ve installed fail2ban, proceed to configuring and enabling it on your server.<\/span><\/p>\n<h2><b>How to Configure and Enable Fail2ban?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Two configuration files come with the default Fail2ban installation that you should use as a starting point.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">They are: <\/span><b>\/etc\/fail2ban\/jail.conf<\/b><span style=\"font-weight: 400;\"> and <\/span><b>\/etc\/fail2ban\/jail.d\/defaults-debian.conf.\u00a0<\/b><\/p>\n<p><span style=\"font-weight: 400;\">You should avoid modifying these files since they could be overwritten during updates. <\/span><b>The fail2ban package<\/b><span style=\"font-weight: 400;\"> reads the configuration files by overriding the .conf file settings with the .local files.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The most common way to set up Fail2ban is to copy the jail.conf file to jail.local and make changes to the .local file.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If you\u2019re an advanced user, you could build your .local configuration file from the ground up. You can skip all unnecessary settings from the .conf file and keep the ones you want to override in the .local file.<\/span><\/p>\n<p><b>Here\u2019s how to go about it.<\/b><\/p>\n<ol>\n<li><span style=\"font-weight: 400;\"> Login into your server through SSH, and type the following command prompt:<\/span><\/li>\n<\/ol>\n<ul>\n<li aria-level=\"1\"><b>cp \/etc\/fail2ban\/jail.conf \/etc\/fail2ban\/jail.local<\/b><\/li>\n<\/ul>\n<ol start=\"2\">\n<li><span style=\"font-weight: 400;\"> Use your preferred text editor to open the <\/span><b>jail.local<\/b><span style=\"font-weight: 400;\"> file.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"> Look for the [DEFAULT] section, which contains the specific options mentioned below:<\/span><\/li>\n<\/ol>\n<p><b>ignoreip:<\/b><span style=\"font-weight: 400;\"> With this option, you can tell fail2ban which IP addresses or hostnames to ignore.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You might, for example, add IP addresses or host names that you often use, such as the ones from your home or office. This prevents fail2ban from locking you away from your server.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Use space to distinguish different addresses just as in this example: i<\/span><b>gnoreip = 127.0.0.1\/8 96.174.216.35.<\/b><\/p>\n<p><b>bantime:<\/b><span style=\"font-weight: 400;\"> This option specifies the duration an IP address or host is banned in seconds. Without a suffix, the value defaults to 600 seconds (10 minutes). You may change this value to your liking and use a negative number to ban an IP address permanently.<\/span><\/p>\n<p><b>findtime: <\/b><span style=\"font-weight: 400;\">This option is used in conjunction with <\/span><b>maxretry<\/b><span style=\"font-weight: 400;\">. It specifies the duration before a ban is set after a predetermined number of failures.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">If it reaches the maximum retry amount within a specific bantime, the fail2ban application bars the host or IP address from the server.<\/span><\/p>\n<p><b>maxretry<\/b><span style=\"font-weight: 400;\">: This value specifies how many failures a host may have before being barred. The default value is five times.<\/span><\/p>\n<ol start=\"4\">\n<li><span style=\"font-weight: 400;\"> After configuring these fail2ban\u2019s options above, you can enable and disable jails for the services and protocols you want to secure.\u00a0<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">SSH login attempts are monitored by default by fail2ban (you can search for the [ssh-iptables] section in the <\/span><b>jail.local<\/b><span style=\"font-weight: 400;\"> file to view the specific settings for the SSH jail).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Fail2ban monitors SSH login attempts by default. To see the specific settings for the SSH jail, check for the [ssh-iptables] section in the<\/span><b> jail.local <\/b><span style=\"font-weight: 400;\">file.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For many protocols, the <\/span><b>jail.local <\/b><span style=\"font-weight: 400;\">file contains default jail settings.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Changing the <\/span><b>enabled = false line to enabled = true<\/b><span style=\"font-weight: 400;\"> and restarting fail2ban is often all you need to do to allow a jail. For added versatility, you can build custom jails and filters.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">When you\u2019re done, save your changes to the <\/span><b>jail.local <\/b><span style=\"font-weight: 400;\">file, and use this command: <\/span><b>service fail2ban restart <\/b><span style=\"font-weight: 400;\">to restart the fail2ban package and load the updated configuration.<\/span><\/p>\n<h3><b>Email Notifications<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">When an IP address is banned, Fail2ban can send email alerts. To receive emails, you&#8217;ll need an SMTP server installed and the default action set to<\/span><b> %(action mw)s.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Using the <\/span><b>% (action mw)s<\/b><span style=\"font-weight: 400;\"> bans the malicious IP and sends a whois report via email. Set the action to <\/span><b>%(action mwl)s <\/b><span style=\"font-weight: 400;\">if you want related logs added in the email.<\/span><\/p>\n<h3><b>Stopping the Fail2ban Service<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">If you want to avoid using your fail2ban service at any time, type the following into the command prompt:<\/span><\/p>\n<p><b>Fail2ban-client stop<\/b><\/p>\n<p><span style=\"font-weight: 400;\">You must add two additional commands:\u00a0<\/span><\/p>\n<ul>\n<li aria-level=\"1\"><b><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;systemctl&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:636,&quot;5&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;6&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;7&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;8&quot;:{&quot;1&quot;:[{&quot;1&quot;:2,&quot;2&quot;:0,&quot;5&quot;:{&quot;1&quot;:2,&quot;2&quot;:0}},{&quot;1&quot;:0,&quot;2&quot;:0,&quot;3&quot;:3},{&quot;1&quot;:1,&quot;2&quot;:0,&quot;4&quot;:1}]},&quot;9&quot;:0,&quot;12&quot;:0}\">Systemctl<\/span> stop fail2ban, and<\/b><\/li>\n<\/ul>\n<ul>\n<li aria-level=\"1\"><b>Systemctl disable fail2ban\u00a0<\/b><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">to CentOS 7 and Fedora to fully stop and disable the fail2ban package.<\/span><\/p>\n<h2><b>Final Thoughts<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Fail2ban is a simple and effective solution to a difficult problem. It requires<\/span><b> minimal setup and has minimal operating<\/b><span style=\"font-weight: 400;\"> overhead costs or workload to you or your computer.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you\u2019re in the business of managing a dedicated server for your company, you can never be too careful or secure.\u00a0 A server\u2019s primary function is to store files and act as a medium of sending and receiving information upon request. Hence, the need to protect it from password hackers or brute-force attacks. Fail2ban monitors [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[34],"tags":[],"class_list":["post-1559","post","type-post","status-publish","format-standard","hentry","category-web-hosting"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/comments?post=1559"}],"version-history":[{"count":6,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1559\/revisions"}],"predecessor-version":[{"id":4598,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/1559\/revisions\/4598"}],"wp:attachment":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/media?parent=1559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/categories?post=1559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/tags?post=1559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}