Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
DevOpsJune 20, 20264 min read

Linux server hardening: Automate audits with Lynis and fail2ban

Linux server hardening doesn't have to be manual. Learn to use Lynis for automated security audits and fail2ban to block brute-force attacks effectively.

LinuxSecurityDevOpsSysAdminLynisFail2banHardeningDockerCI/CD
Close-up of blue ethernet cables hanging in a data center, highlighting technology connections.

I remember the first time I saw my /var/log/auth.log file flooded with thousands of failed SSH login attempts. It’s a rite of passage for every sysadmin, but it’s also the moment you realize that "default" is never enough. Securing production boxes isn't about being a black-belt security researcher; it's about closing the low-hanging fruit before someone else does it for you.

If you’re running Linux in production, Linux server hardening is a continuous process, not a one-time script. I’ve found that combining proactive auditing with reactive defense—specifically using Lynis and fail2ban—is the most reliable way to keep a small-to-medium infrastructure sane.

Getting started with Lynis for automated security audits

Think of Lynis as a checklist that actually checks itself. It’s a battle-tested tool that scans your system for misconfigurations, missing security patches, and potential vulnerabilities. Unlike some heavy-duty scanners, it doesn't need to be installed as a daemon; it’s a lightweight shell script.

I usually run it directly from the source on Debian or Ubuntu systems. Here is how I set it up:

Bash
# Clone the latest version
git clone https://github.com/CISOfy/lynis
cd lynis
# Run an audit
sudo ./lynis audit system

When you run this, you’ll get a wall of output. Don’t panic. The "Suggestions" section is where the magic happens. Lynis will point out things like insecure permissions on /etc/shadow, lack of a firewall, or outdated kernels.

The beauty of Lynis is that it provides a "Security Index" number. Aiming for 70+ is a decent start for a hardened production server. If you’re managing clusters, you might also want to look at how we handle Kubernetes security auditing to keep your containerized workloads consistent with your host-level security.

Stopping brute-force attacks with fail2ban

Once you've audited your system, the next step is stopping the automated bots that never sleep. If you have SSH exposed to the public internet, you will be scanned. Fail2ban is the industry standard for dropping these connections at the firewall level.

I install it with sudo apt install fail2ban. Once installed, don't just leave it at the defaults. I create a jail.local file to override the base configuration:

INI
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 1h

With this configuration, anyone who fails to log in three times gets banned for an hour. It’s simple, effective, and it keeps your logs from exploding. I’ve seen this reduce log noise by roughly 90% on servers that were previously getting hammered by dictionary attacks.

Why you need both

Wooden letters spelling 'WHY' on a brown cardboard background. Ideal for concepts of questioning and curiosity.

You might wonder why you need an automated security audit if you already have a firewall. The reality is that fail2ban only protects against brute force. It doesn't tell you if you’ve left an unnecessary service like telnet running, or if your SSH configuration allows root login.

Server security is layered. I once spent two days debugging an issue where a developer had enabled password authentication on an SSH key-only server. My daily Lynis run flagged the PermitRootLogin and PasswordAuthentication settings in /etc/ssh/sshd_config immediately. Without that audit, I wouldn't have noticed until the next manual review.

Integrating security into your workflow

If you're already deep into the cloud-native ecosystem, you’re likely familiar with tools like Falco for threat detection. While Lynis handles the host-level hardening, Falco manages the runtime behavior of your containers. They serve different purposes, but they both follow the same philosophy: assume the environment is messy and automate the verification.

If you’re managing sensitive data, remember that hardening the OS is only half the battle. You should also be looking at managing secrets with HashiCorp Vault to ensure your application code never touches sensitive credentials directly.

Lessons learned and what's next

Sneakers on pavement with a chalk question mark, symbolizing curiosity or decisions.

The biggest mistake I made early on was being too aggressive with fail2ban. I once banned my own office IP because I forgot my password twice. Always whitelist your office or VPN IP addresses in the ignoreip setting in jail.local.

Also, don't treat the Lynis suggestions as a "must-fix-all" list. Some suggestions might conflict with your specific application requirements. Read the output, understand what the tool is flagging, and decide if it makes sense for your threat model.

I’m currently still experimenting with shipping these Lynis results to a centralized dashboard. Parsing the raw logs is okay for a single server, but when you have five or ten, it becomes a chore. Automating the collection of these audits is likely my next weekend project. Until then, keep your ports closed, your keys rotated, and your logs monitored.

Back to Blog

Similar Posts

A classic MS-DOS terminal screen displayed on a laptop keyboard with vivid illumination.
DevOpsJune 20, 20264 min read

Nginx as a reverse proxy: The config explained line by line

Nginx as a reverse proxy is the backbone of reliable web traffic routing. Learn how to configure your setup line-by-line for production-grade performance.

Read more
Close-up of colorful programming code on a computer screen, showcasing digital technology.
DevOps
June 20, 2026
4 min read

Zero-downtime deploy with GitHub Actions: A Practical Guide

Achieve a zero-downtime deploy with GitHub Actions using blue-green strategies. Learn how to keep your services running seamlessly during every release.

Read more
Shipping containers and cranes at Hamburg port showcasing global trade.
DevOpsJune 20, 20265 min read

Docker for app developers: A mental model that sticks

Master Docker for app developers by shifting your mental model. Learn how containers actually work, why they aren't VMs, and how to build efficient images.

Read more