{"id":11645,"date":"2024-03-15T17:53:27","date_gmt":"2024-03-15T12:23:27","guid":{"rendered":"https:\/\/www.skynats.com\/?p=11645"},"modified":"2025-01-08T20:36:55","modified_gmt":"2025-01-08T15:06:55","slug":"how-to-secure-ssh-with-fail2ban-on-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/www.skynats.com\/blog\/how-to-secure-ssh-with-fail2ban-on-ubuntu-22-04\/","title":{"rendered":"How To Secure SSH with Fail2Ban on Ubuntu 22.04"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"268\" height=\"188\" sizes=\"(max-width: 268px) 100vw, 268px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/download-fail.png\" alt=\"Fail2Ban\" class=\"wp-image-11683\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Fail2ban is a helpful software that enhances the security of Linux servers by preventing unauthorised logins and suspicious attacks. We monitor the logs of services like SSH and Apache to operate this service. The main goal is to prevent multiple login attempts, which improves security measures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To install and set up fail2ban,follow the below steps<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Install fail2ban package using commands<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>apt update\napt install fail2ban -y<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> 2. Start and enable Fail2ban service<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl enable fail2ban\nsystemctl start fail2ban<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Check Fail2ban service status using below command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl status fail2ban<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the service is functioning correctly, the output will display as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"905\" height=\"248\" sizes=\"(max-width: 905px) 100vw, 905px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/FAIL2BAN-1.png\" alt=\"fail2ban\" class=\"wp-image-11649\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/FAIL2BAN-1.png 905w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/FAIL2BAN-1-300x82.png 300w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/FAIL2BAN-1-768x210.png 768w\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">4. After successfull installation, Configure the Fail2ban<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fail2ban configuration files are located in \/etc\/fail2ban directory. Folowing are the important contents in the directory.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"189\" height=\"112\" sizes=\"(max-width: 189px) 100vw, 189px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-2.png\" alt=\"fail2ban \" class=\"wp-image-11651\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To begin Fail2ban configuration,copy the default jail configuration file jail.conf to jail.local using cp command and edit the jail.local file using vim editor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/etc\/fail2ban\/\ncp \/etc\/fail2ban\/jail.conf \/etc\/fail2ban\/jail.local\nvim jail.local<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, uncomment the &#8216;ignoreip&#8217; section line. Next, include the required IP addresses.This section helps you to prevent IPs from blocked by Fail2ban service.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"26\" sizes=\"(max-width: 693px) 100vw, 693px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-ignoreip.png\" alt=\"fail2ban ignoreip\" class=\"wp-image-11652\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-ignoreip.png 693w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-ignoreip-300x11.png 300w\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In the next step edit bantime,findtime and maxretry as you need. Screenshot below shows the default settings.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"881\" height=\"186\" sizes=\"(max-width: 881px) 100vw, 881px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-bantime.png\" alt=\"fail2ban bantime\" class=\"wp-image-11653\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-bantime.png 881w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-bantime-300x63.png 300w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-bantime-768x162.png 768w\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Bantime = The amount of time an IP address is blocked<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Findtime = the intervals of time during which an unsuccessful attempt is retried and deemed to be blocked.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Maxretry = the maximum amount of failed attempts that can pass before blocking an IP address.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enable email notifications for blocked IP addresses.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Change the action option and default sender and destination mail addresses as below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"170\" sizes=\"(max-width: 779px) 100vw, 779px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-email.png\" alt=\"fail2ban email\" class=\"wp-image-11654\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-email.png 779w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-email-300x65.png 300w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-email-768x168.png 768w\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Enable the jail for ssh service. For that go to sshd section and add the line enable as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"284\" height=\"87\" sizes=\"(max-width: 284px) 100vw, 284px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-ssh-enable.png\" alt=\"fail2ban ssh enabled\" class=\"wp-image-11655\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">After making the changes listed above, save the file and close it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">5. Restart the service using command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl restart fail2ban<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">6. To verify Fail2ban configurations, Run the below command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fail2ban-client ping<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If there is no configuration error, will get the output as &#8220;Server replied: pong&#8221;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">7. Test the Fail2ban set up by repeatedly logging into your server with an incorrect password. (Once the maximum number of retries has been reached, the IP address will be blocked.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">8. For checking  the status of sshd jail, Run the below command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fail2ban-client status sshd<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">you can see the detailed status and blocked IPs as shown below;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"172\" sizes=\"(max-width: 773px) 100vw, 773px\" src=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-status-ssh.png\" alt=\"fail2ban status ssh\" class=\"wp-image-11656\" srcset=\"https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-status-ssh.png 773w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-status-ssh-300x67.png 300w, https:\/\/www.skynats.com\/blog\/wp-content\/uploads\/2024\/03\/fail2ban-status-ssh-768x171.png 768w\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">9. You can block and unblock the IP by running the below commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fail2ban-client set sshd banip 82.148.19.36\nfail2ban-client set sshd unbanip 82.148.19.36\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now you have installed and configured Fail2ban on your server. As per the defined rules,it will perform against suspicious activities and your server will be secured using Fail2ban.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Our&nbsp;<a href=\"https:\/\/www.skynats.com\/server-management\/\">Technical Team<\/a>&nbsp;will be available to assist you with How To Secure<strong> <\/strong>SSH with Fail2Ban on Ubuntu 22.04 that can make your job a lot easier. Get in touch with&nbsp;<a href=\"https:\/\/www.skynats.com\/server-management\/\">Skynats<\/a>&nbsp;if you have any queries<a href=\"https:\/\/ubuntu.com\/\" target=\"_blank\" rel=\"noopener\">.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fail2ban is a helpful software that enhances the security of Linux servers by preventing unauthorised logins and suspicious attacks. We monitor the logs of services like SSH and Apache to operate this service. The main goal is to prevent multiple login attempts, which improves security measures. To install and set up fail2ban,follow the below steps [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[762,5],"tags":[826,825,828],"class_list":["post-11645","post","type-post","status-publish","format-standard","hentry","category-ubuntu-22-04","category-blog","tag-fail2ban","tag-ssh","tag-ubuntu-22-04"],"_links":{"self":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/11645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/comments?post=11645"}],"version-history":[{"count":0,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/posts\/11645\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/media?parent=11645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/categories?post=11645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skynats.com\/blog\/wp-json\/wp\/v2\/tags?post=11645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}