Skip to content

Linux hardening: essential practices for production servers

Servers installed with default settings carry avoidable risk. See a hardening baseline you can apply and audit.

Reading time
9 min
Published

A freshly installed Linux server comes with running services you do not use, broad permissions, and settings designed for convenience, with security in the background. Hardening is the process of shrinking that surface: turning off what is not needed, tightening access, and making the system produce evidence of what happens on it. Done once and standardized, it raises the security floor of the whole fleet.

The baseline below is practical and defensive, designed for production servers, and organized around the points that reduce risk the most: SSH access, privileges, firewall, updates, services, and auditing. Use the CIS Benchmarks as a reference and adapt each item to your environment, because applying the list blindly can break specific applications.

Access, SSH, and privileges

SSH is the main way into the server and deserves attention. Disable direct root login, require key-based authentication instead of passwords, and restrict who can connect. Key authentication prevents password brute force, which is the most common attack against exposed SSH. In the service configuration file, the settings look like this:

Excerpts from /etc/ssh/sshd_config to harden access
PermitRootLogin noPasswordAuthentication noX11Forwarding no

Privileges, keys, and service accounts

On the privilege side, apply least privilege: each person has their own user, and elevation happens through sudo, with logging. Avoid shared accounts, because with them you lose track of who did what.

Treat SSH keys as credentials with a lifecycle. Keep a record of which keys have access, remove the keys of people who left the company, and prefer passphrase-protected keys. Service accounts that do not need an interactive shell should use a shell that refuses login, so a credential for that account does not become an entry point. Also restrict which users can authenticate over SSH, instead of leaving access open to any account on the system.

Firewall, services, and updates

Enable a host firewall and allow only the necessary ports, with everything else denied by default. Next, list the running services and disable anything without a clear function, since every service you remove is one less potential vulnerability. Security updates need a defined process, with regular patching and priority for critical fixes, ideally tested in staging before they reach production.

  • Deny by default on the firewall and allow only what is necessary
  • Disable services and packages with no clear function
  • Apply security updates on a defined cadence
  • Keep staging separate from production to validate changes
  • Synchronize time with NTP for reliable log correlation

Kernel and mandatory access control

A good part of the surface of a server lies in the kernel and in network behavior. A few sysctl parameters close common vectors: ICMP redirects are ignored, reverse path filtering is turned on against source spoofing, and memory address randomization stays on, which makes memory bugs harder to exploit. Since the settings are versioned in a file, they come with the server from the start and are easy to audit, as in this example:

Example of /etc/sysctl.d/60-hardening.conf
net.ipv4.conf.all.rp_filter = 1net.ipv4.conf.all.accept_redirects = 0net.ipv4.conf.all.send_redirects = 0kernel.randomize_va_space = 2

Keep mandatory access control active. SELinux in enforcing mode, or AppArmor with its profiles loaded, confines each process to what it is supposed to do, and a compromised service cannot reach files and resources outside its scope. Many people turn this layer off at the first conflict and, by doing so, lose the very containment that would limit the damage. The better path is to adjust the profile or the context and stay in enforcing mode.

Logs, auditing, and abuse protection

Centralize logs in a remote destination, so that whoever compromises the host cannot erase the trail of what they did. Enable system auditing to record sensitive events, such as sudo use and changes to critical files. Tools like fail2ban complement key authentication: they block sources with many failed attempts, which cuts down brute force.

Review the permissions of sensitive files and remove unnecessary write permissions. Many incidents escalate because a configuration file or a binary had permissions that were too broad.

File integrity and a verifiable baseline

Hardening only holds up if you notice when the system changes. A file integrity tool such as AIDE records a baseline of critical files and later points out what was changed, which helps detect a swapped binary or a tampered configuration. Package managers themselves verify the integrity of installed files by comparing each one with what the package delivered. That is useful when you are investigating a suspicious host.

System auditing can also watch sensitive paths and record who changed them. With rules on the accounts, the SSH configuration, and the sudoers file, a change that would otherwise go by silently becomes a traceable event. Combine these rules with shipping the logs off the host, so the evidence does not depend on the very server that may have been compromised. The rules live in their own files, as in:

Audit rules for sensitive files (/etc/audit/rules.d/)
-w /etc/passwd -p wa -k identity-w /etc/ssh/sshd_config -p wa -k sshd-w /etc/sudoers -p wa -k sudoers

The baseline also depends on how the server is provisioned. When hardening is described as code in an automation tool, every host starts out in the same state, and you can compare what is running with what should be running. Without that, the configuration drifts from one host to another, and the drift only shows up during an incident.

Practical checklist

  • Disable direct root login over SSH
  • Require key authentication and turn off password login in SSH
  • Give each person their own user and elevate through sudo with logging
  • Enable a host firewall that denies by default
  • Disable services and packages with no clear function
  • Define a security update process with a fixed cadence
  • Centralize logs in a remote destination
  • Enable auditing of sensitive events (sudo, critical files)
  • Use fail2ban or an equivalent against brute force
  • Review file permissions and remove unnecessary write access
  • Synchronize time through NTP
  • Standardize the baseline for new servers
  • Keep SELinux or AppArmor in enforcing mode
  • Apply network and memory sysctl parameters (rp_filter, ASLR, redirects)
  • Enable file integrity checking with AIDE or an equivalent
  • Add audit rules for sensitive files (passwd, sshd_config, sudoers)
  • Restrict which users can authenticate over SSH

Best practices

  • Use the CIS Benchmarks as a reference, adapted to your environment
  • Standardize the baseline so new hosts are hardened from day one
  • Centralize logs so the evidence survives even a compromise
  • Apply hardening changes incrementally and validate each step
  • Document exceptions with a justification and a review date
  • Monitor drift from the baseline over time
  • Keep mandatory access control on instead of turning it off at the first conflict
  • Describe hardening as code so every host starts out in the same state

Common mistakes

SSH with password and root login enabled
The server becomes a direct brute force target, and whoever gets in arrives with maximum privilege.
Default services running with no use
They widen the attack surface and add unnecessary vulnerabilities.
Local-only logs
Whoever compromises the host can erase the evidence trail.
Updates without a process
Known vulnerabilities stay open for long periods.
Shared accounts
Traceability is lost: nobody can say who carried out each action.
SELinux or AppArmor disabled to fix an error quickly
It removes a containment layer that would limit a compromised process.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

Do I need to follow the CIS Benchmarks to the letter?
The CIS Benchmarks are a great reference, but they should be adapted to your environment. Some items can break specific applications or simply make no sense in your context. The recommended approach is to apply what reduces risk without making operations unworkable and to justify the exceptions, instead of following the list blindly.
Is SSH key authentication really safer than a password?
Yes, by a wide margin. Keys eliminate password brute force, which is the most common attack against exposed SSH, and they are much harder to guess. In exchange, two precautions come up: protect the private key with a passphrase, and control which keys have access so that the ones no longer needed get removed.
Why centralize logs off the server?
Because an attacker who compromises the host usually tries to erase their tracks to hinder the investigation, and if the logs only live on the server, they can. With the logs sent to a remote, restricted destination, you preserve the evidence even when the source server has been compromised.
Does hardening remove the need for monitoring?
No. Hardening reduces the surface and the likelihood of compromise, but it does not stop every attack. You still need monitoring and detection to identify suspicious activity and respond quickly. The two work together: hardening reduces what can go wrong, and detection warns you when something happens.
Should I disable SELinux when it gets in the way of an application?
The short answer is no. Disabling SELinux is often the shortcut to fixing a permission error, but it removes a layer that confines each process to its scope. When a service is blocked, the best move is to analyze the denial log and adjust the context or the profile without leaving enforcing mode. Permissive mode can be used for a while to diagnose, but not to run production.
What is file integrity checking, and do I need it?
It is a mechanism that records a baseline of critical files and later points out what changed. It makes it easier to detect a swapped binary or a tampered configuration. Tools like AIDE do this, and package managers also verify the integrity of what they installed. On servers that are exposed or that hold sensitive data, this control speeds up the investigation of a suspicious host considerably.
How do I maintain the hardening baseline over time?
Describe hardening as code in an automation tool. That way every server starts out in the same state, and you can compare what is running with what should be running. Day-to-day manual changes make the configuration drift from one host to another, and that drift usually only appears during an incident. Automation, combined with periodic checks against the baseline, keeps the fleet consistent.

When to bring in specialist help

The GUARDIASEC hardening service works on exactly this: defining a hardening baseline that reduces risk without breaking operations, and standardizing it across the fleet. The CIS Benchmarks are adapted to your environment, and changes are validated incrementally.