SECURITY ENGINEERING · PART 1 · LINUX AND WINDOWS, FROM THE INSIDE · LINUX · 13 OF 229

Harden a Linux Server: CIS Benchmarks and Lynis

Turning a default Linux install into a hardened one you can defend: what a baseline is, the CIS Benchmarks as a published checklist, scanning with Lynis and OpenSCAP to get a score and a to-do list, the highest-value hardening steps, applying a baseline at scale with Ansible and golden images, and verifying it stays applied.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Baseline from CIS
  • scan with Lynis/OpenSCAP
  • apply the high-value steps as code
  • golden images
  • verify, fight drift

Notes

Baselines

The core idea is a baseline: a documented, known-good configuration that every server should match. A default install has services on that you will never use, loose settings and extra packages, each one more attack surface. A baseline says exactly how a hardened host should look, so you can measure any machine against it. When a host drifts away from the baseline, you catch and fix it. Consistency itself is a security property, because surprises are where attackers live.

Why a baseline
  • default = many services on, loose settings, extra packages
  • a baseline is a documented secure config for your OS
  • measure every host against it
  • drift = a host that no longer matches; catch and fix it
  • consistency is a security property

CIS Benchmarks

You do not write a baseline from scratch. The Center for Internet Security publishes the CIS Benchmarks: detailed, free checklists for almost every operating system and major application, from Ubuntu and Red Hat to Docker, Kubernetes, the clouds and databases. Each benchmark has two levels, a practical level one and a stricter level two, and each item explains what to set, why it matters, how to apply it and how to check it. It is the standard starting point for hardening.

The CIS Benchmarks
  • published by the Center for Internet Security
  • per-OS and per-app checklists (Ubuntu, RHEL, Docker, K8s, cloud, databases)
  • two levels: L1 practical, L2 stricter
  • each item: what, why, how to fix, how to audit

Scan it

To find your gaps, scan. Lynis is a free, quick tool: run it and it gives a hardening index score and a list of warnings and suggestions, grouped by area. OpenSCAP is more formal, evaluating a machine against official SCAP content, including CIS and government profiles, and producing a pass-or-fail report per rule. Run one on a fresh server to see the starting point, then after each change to watch the score climb. Measuring beats guessing.

Measure, do not guess
Lynis (free, quick):
  lynis audit system
  → hardening index + warnings
OpenSCAP (SCAP content):
  oscap xccdf eval --profile
  cis ... → pass/fail report
run on a fresh box to see the
  starting point

High-value steps

Not every benchmark item matters equally, so start where the risk is. Remove packages and services you do not use, and close the ports they opened. Harden SSH, as the last episode showed. Tighten sudoers and SUID for least privilege. Turn on the firewall and auditd. Enable automatic security patching. Set a sensible login and password policy and lock unused accounts. Disable ancient protocols and set secure mount options like nosuid on scratch space. These few areas remove most of the practical risk.

Where to start
  • remove unused packages and services; close unused ports
  • SSH hardening (last episode)
  • least privilege: sudoers, SUID
  • enable the firewall + auditd
  • patch automatically
  • password/login policy, lock unused accounts
  • disable old protocols, set secure mount options

At scale

Hardening one server by hand is easy and does not last. Across a fleet you apply the baseline as code: an Ansible role, or ready-made hardening content, that enforces every setting the same way everywhere. Better still, bake the baseline into a golden image with a tool like Packer, so every new server starts already hardened rather than being fixed afterwards. In the cloud that means hardened base images. Now the baseline is version-controlled and reviewed, which Part nine covers.

Baseline as code
  • Ansible roles, or the CIS- provided hardening content
  • bake it into a golden image (Packer) so new servers start hardened
  • cloud: hardened base AMIs
  • infra as code so the baseline is version-controlled and reviewed (Part 9)

Keep it applied

Hardening is not a one-time event; it decays as people make changes and emergencies get patched by hand. So verify continuously. Re-scan on a schedule and alert when the score drops or a specific control fails. Let configuration management re-apply the baseline on every run, so unauthorised changes are reverted automatically. And treat significant drift as something to investigate, because a control that someone quietly turned off might be an attacker clearing the way, not just a tired engineer.

Fight drift
  • re-scan on a schedule, not once
  • alert when the hardening score drops or a control fails
  • config management re-applies the baseline every run
  • treat drift as an incident to investigate: who changed it, and why

A caution

One caution: harden with judgement. Some benchmark items, especially level two, can break real applications, so test changes in staging before production. When you cannot apply an item, document the exception with a reason and an owner rather than silently skipping it. The goal is a system that is both secure and operable; a server so locked down that the app fails is not a win. Use the benchmark as a strong starting point, and decide each item on its merits.

Do not break production
  • L2 and some L1 items can break apps: test in staging first
  • document exceptions with a reason and an owner
  • balance security with operability
  • a benchmark is a starting point, not a law
  • measure the score, but use judgement on each item

Common mistakes

Mistakes. Hardening by hand, once, so it is inconsistent and decays. Having no baseline, so every host is a little different and none can be reasoned about. Applying strict items blindly and breaking applications, which teaches people to distrust hardening. Ignoring drift. Chasing a high scan score while never actually patching. Having no process for documented exceptions. And, most common, leaving new servers to be hardened later, which usually means never.

Avoid these
  • hardening by hand, once
  • no baseline, every host unique
  • applying L2 blindly, breaking apps
  • ignoring drift
  • a great score, no patching
  • no exceptions process
  • new servers start unhardened

Say it like this

In the interview: I harden Linux against a CIS Benchmark as the baseline, scanning with Lynis or OpenSCAP to get a hardening score and the specific gaps. I apply the baseline as code with Ansible and bake it into golden images with Packer, so every new server starts hardened rather than being fixed later. Then I re-scan on a schedule and alert on drift, testing stricter items in staging first and documenting any exceptions with an owner.

In the interview
  • "Against a CIS Benchmark as the baseline, scanned with Lynis or OpenSCAP for a score and gaps.
  • I apply it as code with Ansible
  • and bake hardened golden images, so new servers start secure.
  • Then I re-scan on a schedule and alert on drift, testing strict
  • items in staging first."

Interview questions

Q1What is a security baseline?

A documented, known-good secure configuration that every host should match. It lets you measure machines against a standard, detect drift when a host no longer matches, and reason about your fleet instead of dealing with unique, unpredictable servers.

Q2What are the CIS Benchmarks?

Free, detailed hardening checklists from the Center for Internet Security for most operating systems and major applications. Each item states what to configure, why, how to apply it and how to audit it, with a practical level 1 and a stricter level 2.

Q3What do Lynis and OpenSCAP do?

They audit a system against hardening rules. Lynis is a quick free scanner giving a hardening index and prioritised suggestions; OpenSCAP evaluates a host against formal SCAP content such as CIS or government profiles and produces a pass/fail report per rule.

Q4Which hardening steps give the most value?

Removing unused packages, services and open ports; SSH hardening; least-privilege sudoers and SUID; enabling the firewall and auditd; automatic patching; a sensible login/password policy with unused accounts locked; and disabling old protocols with secure mount options.

Q5How do you apply a baseline across many servers?

As code: an Ansible role or ready-made hardening content that enforces every setting consistently, and ideally baked into golden images (for example with Packer) so new servers boot already hardened rather than being fixed afterwards.

Q6Why is hardening not a one-time task?

Configurations drift as people make changes and emergency fixes, so controls silently turn off. You must re-scan on a schedule, let configuration management re-apply the baseline, and alert when the score drops or a control fails, treating significant drift as worth investigating.

Q7Why apply CIS benchmarks with judgement rather than blindly?

Some items, especially level 2, can break applications. Test changes in staging, document exceptions with a reason and owner instead of skipping silently, and balance security with operability; the benchmark is a strong starting point, not an absolute rule.

Q8What is configuration drift and why does it matter for security?

Drift is a host no longer matching its baseline because of manual changes. It matters because an unexpected change could be an attacker weakening a control, and because inconsistent systems are hard to defend, so drift should be detected and reverted automatically.