<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Codearmo</title>
    <description>The latest articles on DEV Community by Codearmo (@codearmo).</description>
    <link>https://dev.to/codearmo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3054565%2Fd8a74b11-3807-4231-a67f-7e005eb3767e.png</url>
      <title>DEV Community: Codearmo</title>
      <link>https://dev.to/codearmo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codearmo"/>
    <language>en</language>
    <item>
      <title>Protect your Virtual Machine with Fail2ban</title>
      <dc:creator>Codearmo</dc:creator>
      <pubDate>Wed, 16 Apr 2025 08:40:24 +0000</pubDate>
      <link>https://dev.to/codearmo/protect-your-virtual-machine-with-fail2ban-7dk</link>
      <guid>https://dev.to/codearmo/protect-your-virtual-machine-with-fail2ban-7dk</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;OK so after a long time playing around with Linux servers, and now at the stage I have to spin them up pretty frequently with dozens done this year alone, I made a &lt;a href="https://www.codearmo.com/blog/linux-server-checklist" rel="noopener noreferrer"&gt;10 Things to do on New Linux Server Checklist&lt;/a&gt; to try reduce the pain process of setting them up. &lt;/p&gt;

&lt;p&gt;I had always known web security stuff was a real deep rabbit hole :( but my experience with automated login attempts to my servers really made me realize how scary a place the internet can be. &lt;/p&gt;

&lt;p&gt;On &lt;a href="https://www.codearmo.com/blog/linux-server-checklist#install-fail2ban" rel="noopener noreferrer"&gt;Step 4 of the checklist&lt;/a&gt; , inspired by watching videos at the excellent &lt;a href="https://www.youtube.com/@LearnLinuxTV" rel="noopener noreferrer"&gt;LearnLinuxTV&lt;/a&gt; I decided to install Fail2ban. &lt;/p&gt;

&lt;h2&gt;
  
  
  Internet is a Scary Place
&lt;/h2&gt;

&lt;p&gt;SO anyway, I install fail2ban on the test server and the results totally shocked me. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In just 3–4 hours since spinning up the test server for the checklist post, there were already 18 unauthorized login attempts to my virtual machine. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And now about a week later, the ssh jail is getting overcrowded as you can see below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl16fx84m41bgpggkda1k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl16fx84m41bgpggkda1k.png" alt="Fail2ban sshd Jailed IP addresses" width="685" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Check Login Attempts on your Virtual Machine
&lt;/h2&gt;

&lt;p&gt;Below I list some handy commands to check your machine for login attempts &lt;/p&gt;

&lt;p&gt;Check Failed Password Attempts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo grep "Failed password" /var/log/auth.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check Invalid User Attempts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo grep "Invalid user" /var/log/auth.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Count the Number of Attempts in Latest Log file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo grep "Failed password" /var/log/auth.log | wc -l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the logs rotate pretty quick, you might need to change that to log.1 etc for old attempts. &lt;/p&gt;

&lt;h2&gt;
  
  
  🛡️Protect your Virtual Machine with Fail2ban.
&lt;/h2&gt;

&lt;p&gt;So if you found some bots trying to brute force in to your server, you might want to install fail2ban&lt;/p&gt;

&lt;p&gt;Install ✅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt update &amp;amp;&amp;amp; sudo apt install fail2ban
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable &amp;amp; start it ✅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl enable fail2ban
sudo systemctl start fail2ban
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a Jail for the bots ✅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/fail2ban/jail.d/sshd.local
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure your jail file &lt;a href="https://www.codearmo.com/blog/securing-linux-servers-fail2ban" rel="noopener noreferrer"&gt;full guide here&lt;/a&gt; ✅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[sshd]
enabled = true
port    = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
maxretry = 5
bantime = 24h
findtime = 1h

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart the service ✅&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart fail2ban
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check How many IPs are Blocked&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo fail2ban-client status sshd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ubuntu</category>
      <category>linux</category>
      <category>security</category>
      <category>bash</category>
    </item>
  </channel>
</rss>
