Skip to main content

Linux Hardening

Linux is everywhere - web servers, cloud infrastructure, containers, embedded devices, developer workstations. And because it’s so prevalent, it’s also a top target. The good news is that Linux gives you a lot of control over your security posture. The bad news is that control only helps if you actually use it. This page covers the practical hardening steps you should apply to any Linux system, whether it’s a production server, a cloud VM, or a dev box handling sensitive work.

Where to Start - The CIS Benchmark

Before doing anything manual, grab the CIS Benchmark for your distribution. CIS maintains specific benchmarks for:
  • Ubuntu (LTS versions)
  • Red Hat Enterprise Linux / CentOS / AlmaLinux
  • Debian
  • SUSE Linux Enterprise
  • Amazon Linux
These are free to download at cisecurity.org/cis-benchmarks. They tell you exactly what to configure and why. Use them as your checklist. For a quick automated audit, run Lynis:
It’ll give you a hardening index and a list of suggestions. A good starting target is 70+.

User and Authentication Hardening

Disable the Root Login (or Restrict It Heavily)

If root is accessible directly, you’re one credential leak away from a full system compromise.

Enforce Strong Password Policies

Edit /etc/security/pwquality.conf:
And configure password aging in /etc/login.defs:

Audit Existing Users and Groups

Remove or lock any accounts that aren’t needed:

Configure sudo Properly

Never give blanket ALL=(ALL) NOPASSWD: ALL to regular accounts. Edit via visudo:

SSH Hardening

SSH is the most common remote access method - and one of the most targeted services. Lock it down. Edit /etc/ssh/sshd_config:
After editing:
Always test the new config before closing your current session:

Firewall Configuration

UFW (Ubuntu / Debian)

firewalld (RHEL / CentOS / Fedora)

File System and Permissions

Set noexec, nodev, nosuid on Appropriate Partitions

Edit /etc/fstab to add mount options:

Find World-Writable Files and SUID/SGID Binaries

Review the results - remove SUID bits from binaries that don’t need them:

Restrict /etc/cron.* Access

Kernel Hardening (sysctl)

Kernel parameters significantly impact security posture. Edit /etc/sysctl.conf or drop files in /etc/sysctl.d/:
Apply changes:

Audit Logging (auditd)

auditd is the Linux kernel’s audit framework - essential for monitoring and forensics.
Add audit rules to /etc/audit/audit.rules:

Services - Disable What You Don’t Need

Common services to consider disabling if not in use:
  • avahi-daemon (mDNS/zeroconf - usually not needed on servers)
  • cups (printing - almost never needed on servers)
  • bluetooth
  • rpcbind (only needed for NFS)
  • nfs (unless you’re using NFS)
  • telnet / rsh / rlogin (never use these)

Package Management Security

AppArmor and SELinux

These are Mandatory Access Control (MAC) systems that restrict what programs can do, even if they’re compromised.
  • AppArmor - Default on Ubuntu/Debian. Usually easier to work with.
  • SELinux - Default on RHEL/CentOS/Fedora. More powerful, steeper learning curve.

Useful Tools for Linux Hardening

Quick Wins - Things to Do First

If you’re hardening a Linux server and don’t know where to start, do these first:
  1. Update all packages
  2. Disable root SSH login
  3. Switch to key-based SSH authentication
  4. Enable and configure UFW or firewalld
  5. Disable unused services
  6. Enable auditd
  7. Run Lynis and address the top findings
These alone will dramatically improve your posture relative to a default install.

Reference


There’s no such thing as a fully hardened system - only systems that are harder to attack than others. The goal is to make exploitation as difficult and noisy as possible.