Writing

A practical Linux OS hardening checklist

/9 min read/Reliability/#linux/#security/#hardening/#ansible/#aws/#sre

Most hardening guides are long lists of settings with no order and no cost attached. Applied all at once, they break things, and the team learns to treat “security baseline” as a source of outages. This post is a shorter checklist for a fleet of Linux hosts: what to put in the baseline, why, how to check it, and where it fights with latency tuning.

It assumes the hosts are managed with Ansible, as in Making Linux host tuning repeatable with Ansible, with inventory grouped by prd, stg and dev, and a scheduled pipeline that applies and checks the configuration.

1. Start with less

Every package and service on a host is something to patch, configure and explain. The cheapest hardening is not installing it.

  • Start from a minimal image and add what the host role needs.
  • List what is listening: ss -tulpn. Every open port should have an owner and a reason.
  • List what is enabled: systemctl list-unit-files --state=enabled. Disable and mask what the role does not use (print servers, avahi, rpcbind on hosts without NFS).
  • Remove compilers and debug tools from production images if your process allows it, or at least know which hosts have them.

Keep the package list in code. “Which hosts have tcpdump installed?” should be a query, not an investigation.

2. SSH

SSH is the front door, so it gets the most attention.

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowGroups ssh-users
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no

On older OpenSSH versions, KbdInteractiveAuthentication is ChallengeResponseAuthentication. Check the effective config with sshd -T, not by reading the file, because Include and Match blocks can override what you expect.

Always validate before reloading. A broken sshd_config on a host with no console access is a long afternoon. In Ansible, the template module’s validate: /usr/sbin/sshd -t -f %s does this for you.

On AWS, ask whether port 22 needs to be reachable at all. AWS Systems Manager Session Manager gives shell access without an inbound port, and it can log sessions centrally. If you keep SSH, allow it only from a bastion or a management network in the security group, and never leave it open to the whole internet.

3. Patching and reboots

Unpatched packages are the most common way a “hardened” host stops being hardened. Pick a cadence and make it boring:

  • Security updates on a regular schedule, with a faster path for critical CVEs.
  • Roll through dev, then stg, then prd, a slice at a time, as in Making changes you can take back.
  • Track which hosts need a reboot. On Debian and Ubuntu, /var/run/reboot-required exists. On RHEL-family systems, needs-restarting -r reports it.

A kernel update does nothing until the host boots into it. Fleets that patch but never reboot run old kernels for months. Compare uname -r against the newest installed kernel and alert when the gap gets old.

For trading hosts, reboots have to fit outside market hours, and some hosts cannot drop out without a replacement ready. Plan the reboot window as part of the patch process, not as an afterthought. Live kernel patching can cover some fixes between reboots, but it does not replace them.

4. Users and sudo

  • No shared accounts. Each person logs in as themselves, so the logs mean something.
  • Manage local users and SSH keys from code, and remove them from code. Leavers are the case that matters.
  • Grant sudo through groups, in files under /etc/sudoers.d/, validated with visudo -cf.
  • Avoid NOPASSWD: ALL for people. If automation needs passwordless sudo, give it a dedicated account with its own key and limit where it can log in from.
  • Lock or remove service accounts’ shells if they never log in interactively.

5. Firewalls at two layers

On AWS, security groups are the first filter. They are stateful and attached to the network interface. Network ACLs sit at the subnet and are stateless, so return traffic needs its own rules.

A host firewall (nftables, or firewalld on top of it) is still worth having. It protects against a security group that someone widened “for a minute”, and it applies to traffic inside the same group. Default deny inbound, allow what the role needs, and log drops at a rate limit so a scan does not fill the disk.

Keep both in code. A rule that only exists in the console will be gone, or wrong, the next time someone rebuilds the host.

6. Audit logs, shipped somewhere else

auditd records security-relevant events. A useful starting set watches changes to identity and privilege files and to the audit config itself:

-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/group -p wa -k identity
-w /etc/sudoers -p wa -k privilege
-w /etc/sudoers.d/ -p wa -k privilege
-w /etc/ssh/sshd_config -p wa -k sshd
-w /etc/audit/ -p wa -k audit-config

Logging every execve gives a full command history, but it is noisy and has a cost on busy hosts. Decide per host role, and see section 9.

Logs that stay on the host can be edited by whoever compromised it. Ship auth logs, journald and audit logs to a central store that the host cannot delete from, and keep enough retention to cover the time it takes to notice a problem. Alert on the log shipper going quiet, not only on what the logs say.

7. File permissions

  • Sensitive files should match the distribution defaults: /etc/shadow is not world-readable, /etc/ssh/ssh_host_*_key is root-only.
  • Find world-writable files outside the expected places: find / -xdev -type f -perm -0002.
  • Keep a list of SUID and SGID binaries and review changes: find / -xdev -type f \( -perm -4000 -o -perm -2000 \).
  • Set a sensible default umask so new files are not group or world writable by accident.

A file integrity tool (AIDE or similar) can catch changes to system binaries, at the cost of a scan that reads a lot of disk. Schedule it.

8. Kernel sysctls and mount options

A common set of sysctls for hosts that are not routers:

net.ipv4.ip_forward = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.tcp_syncookies = 1
kernel.randomize_va_space = 2
kernel.kptr_restrict = 1
kernel.dmesg_restrict = 1
kernel.unprivileged_bpf_disabled = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1

Set the matching net.ipv4.conf.default.* keys too, so interfaces added later get the same values, and cover IPv6 if it is enabled.

Some need care. Kubernetes nodes and container hosts need ip_forward = 1. Strict reverse path filtering (rp_filter = 1) can drop legitimate traffic on hosts with several network interfaces and asymmetric routes, which is common on hosts that separate market data from order traffic. Loose mode (rp_filter = 2) is a common compromise there. kernel.yama.ptrace_scope = 1 stops non-root users attaching debuggers to other processes, which may surprise developers. Test each one per host role.

For mount options, the usual targets are /tmp, /var/tmp and /dev/shm with nodev,nosuid,noexec, and /home with nodev. noexec on /tmp breaks some installers and tools that unpack and run from there. Before changing mount options on /dev/shm, check whether any application uses it for shared memory, and test it. Putting /var/log on its own filesystem stops a log flood from filling root.

9. Where hardening fights latency tuning

The low-latency tuning checklist removes work from the CPUs that matter. Hardening adds work. Some conflicts to expect:

  • Audit rules. Syscall audit rules add work on the system call path, and broad rules like logging every execve produce a steady stream of events. Measure the tail latency of the application with and without them, using the same load, before deciding.
  • CPU vulnerability mitigations. Booting with mitigations=off can reduce syscall and context switch overhead, and it also turns off protection against a class of side-channel attacks. That is a security decision, not a tuning flag. If it is considered at all, it belongs on a small, documented set of hosts, with sign-off, and with measurements showing it matters for this workload.
  • Agents on isolated cores. Security agents, file integrity scans and log shippers should run on housekeeping cores, not on the cores you isolated for the application. Check their CPU affinity after they start, and after they update.
  • Scans and patching in market hours. Schedule integrity scans, vulnerability scans and package updates outside trading hours.

The rule is the same as for tuning: change one thing, measure, keep the result. “Hardening made us slower” and “hardening is free” are both guesses until someone runs the test.

10. CIS Benchmarks as a reference

The CIS Benchmarks are a good list to work through. They explain each control and split them into levels by how much they restrict. Tools like OpenSCAP can scan a host against a profile, and there are community Ansible roles that implement them.

Treat them as a reference, not something to apply wholesale. Some controls will not fit your hosts, and a few will break them. Go through the list, decide per control, and record the decision. A scan score of 100% is not the goal. A baseline you understand and can defend is.

11. Enforce it with Ansible and check for drift

Put the baseline in one role applied to every host, with values that differ by role or environment in group_vars:

inventory/
  prd/  stg/  dev/
group_vars/
  all/baseline.yml
  prd/baseline.yml
roles/
  baseline/
    tasks/ssh.yml
    tasks/sysctl.yml
    tasks/audit.yml
    tasks/users.yml

Run it from the scheduled CI pipeline, dev first. Validate config files before they replace the live ones (sshd -t, visudo -cf) and assert the live state afterwards, for example by checking sshd -T output. Roll out with serial so a bad change stops after the first batch.

Between runs, people change hosts by hand. A scheduled ansible-playbook --check --diff shows which hosts have drifted from the baseline, as described in Detecting configuration drift with Ansible check mode. Get the check to zero changes first, so a new change stands out.

12. Write the exceptions down

Every fleet has exceptions: a host that needs IP forwarding, a team that needs a debugger, a latency-sensitive role with a lighter audit set. That is fine. An exception nobody can explain is not.

Keep each exception in code, next to the variable it overrides, with a reason, an owner and a review date:

# Exception: lighter audit rules on order-path hosts.
# Reason: execve logging cost on these hosts; link the measurement here.
# Owner: platform team. Review: 2027-05-01.
audit_log_execve: false

Review them on a schedule. Exceptions tend to outlive the reason they were added.

Checklist

  • Minimal image; every listening port and enabled service has a reason.
  • SSH is key-only, no root login, limited to a group, validated before reload.
  • Security updates on a schedule, rolled through dev, stg, prd; reboots tracked and planned.
  • Named accounts only; sudo through groups in validated files.
  • Security groups and host firewalls default deny, both in code.
  • auditd watches identity, privilege and audit config; logs shipped off the host.
  • Permissions, SUID binaries and world-writable files reviewed.
  • sysctls and mount options set per role and tested.
  • Latency-sensitive hosts measured with and without each costly control.
  • Baseline enforced with Ansible and checked for drift on a schedule.
  • Every exception has a reason, an owner and a review date.

References

  1. CIS, CIS Benchmarks.
  2. OpenSSH, sshd_config(5).
  3. Linux kernel documentation, IP sysctl and /proc/sys/kernel.
  4. Linux kernel documentation, Hardware vulnerabilities.
  5. AWS, Security group rules and Session Manager.
  6. OpenSCAP, openscap.org.