Linux Server Security Auditing and Hardening with Lynis and CIS Benchmark

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

Problem Statement: The 2 AM Security Wake-Up Call

At 2:14 AM, your phone lights up with an alert: unusual outbound traffic spikes on a production web node. You SSH in, check htop and ss -tulnp, and spot the culprit—a runaway cron job, not a breach. Even so, your pulse is racing. That server was spun up six months ago, and nobody has audited its security since.

Guessing which kernel flags, SSH directives, or file permissions are wide open during an outage is a losing game. You need a fast, repeatable way to inspect the OS, catch misconfigurations early, and measure the box against a trusted baseline like the CIS Benchmark. Otherwise, you are flying blind.

Over three years of managing a dozen Linux VPS instances, I learned that lesson the hard way. Early on, I pasted a hardening script onto a live server at 2 AM and locked myself out of SSH—I had disabled password auth before checking my key agent. That is why I stick to Lynis. It audits first, explains what is wrong, and leaves every system change in your hands.

Core Concepts: How Lynis and the CIS Benchmark Work Together

Lynis operates very differently from external tools like Nmap or cloud vulnerability scanners. Instead of probing ports over the network, it runs directly inside your Linux host. From there, it inspects local configuration files, running services, kernel parameters, installed packages, and filesystem permissions.

Nothing on your system gets modified automatically. Lynis runs more than 300 modular checks and compares your setup against industry standards, including the Center for Internet Security (CIS) Benchmark. When it finishes, you get a Hardening Index from 0 to 100. A fresh Ubuntu 24.04 cloud image typically scores around 60 to 65, while a well-tuned production host sits between 75 and 85.

Understanding Warnings vs. Suggestions

Every finding in a Lynis report lands in one of two actionable buckets:

  • Warnings: High-priority gaps or broken configurations that need immediate attention, such as unpatched repositories, missing firewall rules, or world-writable system directories.
  • Suggestions: Hardening tweaks aligned with the CIS Benchmark that shrink your attack surface. Common examples include tightening SSH idle timeouts, enabling full ASLR, or disabling unused filesystems.

Why Staging Tests Matter Before Hardening

Chasing a higher Hardening Index means nothing if it breaks your app. Take noexec on /tmp: it satisfies a classic CIS check, yet it will immediately break deployment pipelines or database installers that execute temporary binaries there. Audit first, weigh the impact of each change, and test everything on a staging clone before touching production.

Hands-On Practice: Scanning, Reading Reports, and Hardening

Let’s walk through a full audit-and-fix cycle on a Debian/Ubuntu or RHEL-based server. Every command below requires root or sudo so Lynis can read protected system files.

Step 1: Installing Lynis

Default distro repositories often lag several releases behind upstream, leaving you with outdated test definitions. Pulling the latest stable release from the official CISOfy repo—or cloning it into /usr/local/lynis—keeps your checks aligned with current CIS guidance.

# Option A: Install on Ubuntu/Debian via apt
sudo apt update && sudo apt install -y lynis

# Option B: Get the latest release via Git (works on any distro)
sudo git clone https://github.com/CISOfy/lynis /usr/local/lynis
cd /usr/local/lynis
sudo ./lynis show version

Cloning via Git leaves your system package database untouched. That minimal footprint is especially handy when you are investigating a live incident.

Step 2: Running Your First System Audit

Pass the --quick flag so Lynis runs non-interactively instead of pausing for Enter between sections:

sudo lynis audit system --quick

Most scans finish in 30 to 60 seconds. As checks run, Lynis prints green [ OK ], yellow [ WARNING ], or white [ SUGGESTION ] markers. Behind the scenes, it writes two key files to disk:

  • /var/log/lynis.log — The human-readable execution log showing every test ID and what Lynis observed.
  • /var/log/lynis-report.dat — A structured key-value file holding your Hardening Index, warnings, and suggestions.

Step 3: Filtering Warnings and Suggestions

Scrolling through hundreds of lines of terminal output at 2 AM is a waste of time. Query the structured report file directly to pull out only what needs action:

# Check your baseline Hardening Index
sudo grep "hardening_index" /var/log/lynis-report.dat

# List all warnings
sudo grep "warning\[\]" /var/log/lynis-report.dat

# List all suggestions
sudo grep "suggestion\[\]" /var/log/lynis-report.dat

Each output line includes a test ID such as SSH-7408 or KRNL-6000. To see why Lynis flagged a setting and how it evaluates that check, pull up the test details:

sudo lynis show details SSH-7408

Step 4: Applying Targeted CIS Benchmark Fixes

Never try to resolve 40 suggestions at once. Tackle the highest-risk areas first—usually SSH access and kernel network settings—and place your overrides in drop-in files so they are trivial to roll back.

Start by locking down the SSH daemon under /etc/ssh/sshd_config.d/ rather than editing the main config file:

sudo tee /etc/ssh/sshd_config.d/99-cis-hardening.conf > /dev/null <<'EOF'
PermitRootLogin no
PasswordAuthentication no
MaxAuthTries 3
AllowAgentForwarding no
AllowTcpForwarding no
ClientAliveInterval 300
ClientAliveCountMax 2
LogLevel VERBOSE
EOF

# Validate syntax BEFORE reloading (use 'sshd' instead of 'ssh' on RHEL/Rocky)
sudo sshd -t && sudo systemctl reload ssh

That sshd -t check is your safety net. If there is a syntax typo, sshd refuses to reload instead of crashing and locking you out. Keep your active SSH session open and verify a fresh login in a second terminal before closing anything.

With SSH locked down, move on to common KRNL-6000 findings. A dedicated sysctl drop-in file hardens network stack behavior and memory protections without touching kernel defaults permanently:

sudo tee /etc/sysctl.d/99-cis-hardening.conf > /dev/null <<'EOF'
# Enable reverse path filtering to prevent IP spoofing
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# Ignore ICMP redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0

# Restrict core dumps for SUID binaries
fs.suid_dumpable = 0

# Full ASLR protection
kernel.randomize_va_space = 2
EOF

# Apply and verify without rebooting
sudo sysctl --system

Step 5: Re-Scanning and Customizing Your Baseline

After your fixes pass staging checks and roll out, run Lynis again to verify the score bump:

sudo lynis audit system --quick
sudo grep "hardening_index" /var/log/lynis-report.dat

Expect your Hardening Index to jump roughly 8 to 12 points from those two drop-in files alone. Of course, some CIS checks simply do not fit every workload—a single-purpose container host, for example, might intentionally skip separate partition rules. Instead of tuning out the same noise every month, add a custom profile at /etc/lynis/custom.prf to suppress accepted exceptions:

# Skip a specific test ID with a documented reason
skip-test=FILE-6310

Adding a short comment beside each skipped test tells future teammates why that exception exists, saving them from re-investigating the same finding months later.

Conclusion: Keeping Your Hardening Baseline Honest

Good server security is not about chasing a 100/100 score in one marathon session. What matters is having a repeatable baseline you can verify in under a minute after OS upgrades, new server builds, or late-night alerts.

Run lynis audit system --quick on a staging host today. Pull your top five warnings and suggestions from /var/log/lynis-report.dat, and fix them with clean drop-in files. Once your services pass testing, roll those configs to production and schedule a monthly scan—so configuration drift never surprises you at 2 AM again.

Share: