What Linux server hardening is and how it works
Server hardening reduces the attack surface: you close unnecessary entry points and limit both process privileges and the damage a break-in can do. You start by answering three questions: what you are protecting, from whom, and at what cost. Then you build several layers of defence, from the firewall to kernel restrictions.
Ubuntu and Debian already enable much of the protection: a minimal installation has no open ports, AppArmor is running, and address space layout randomisation (ASLR) and automatic security updates are on. On top of that, you configure SSH, the firewall, user accounts and monitoring.
Policy: what you protect and from whom
Hardening starts with questions, not tools. Which data and services are you protecting? Where do you expect the threat to come from: a mistake by one of your own users or a determined group? How much convenience and performance are you willing to give up? The answers form a risk model, and the risk model produces a security policy: a list of concrete measures. Policy comes first and tools second; otherwise it is easy to secure the wrong side.
The answers depend on the system. A home VPS holding a site backup needs only a basic set of measures; a server with a database of personal data needs stricter ones. The more valuable the asset, the more layers are justified.
Defence in depth
No single measure is 100% reliable, so protection is built in layers: a mistake in, or a bypass of, one layer does not hand the attacker the whole server. From the network to the process:
- the firewall lets through only the ports you need;
- SSH logins use keys, not passwords;
- people work under their own accounts and raise privileges with sudo;
- the server runs only the services it needs;
- services run in AppArmor profiles with minimal privileges;
- the kernel is restricted with sysctl parameters.
Two continuous tasks run through all the layers: updates close known vulnerabilities, and auditing and monitoring show what is happening on the server. Each layer is covered in detail in the sections “SSH”, “Firewall”, “Accounts and sudo”, “Minimal services and open ports”, “AppArmor and SELinux” and “Kernel and sysctl”.
What is already enabled by default
A minimal Ubuntu or Debian installation is better protected than most people assume. It has no open network ports: services do not listen on the network until you install and enable them. Passwords are stored with strong hashing, and Ubuntu installs security updates automatically.
The kernel and the way packages are compiled add layers you cannot see from the console: process address space is randomised (ASLR), memory is marked non-executable, and the stack is protected by canaries. Mandatory access control confines processes: Ubuntu and Debian run AppArmor by default, which keeps each program in a profile listing the operations it may perform. To check the state of the profiles, run sudo aa-status.
How hardening differs from basic setup
| Aspect | Basic setup | Hardening | CIS compliance |
|---|---|---|---|
| Goal | the server works and can be maintained | keeps working under attack | works and passes an audit |
| Source of the measures | distribution guides | the server’s risk model | a third-party benchmark checklist |
| Key question | how to enable it | whether to enable it | what the benchmark requires |
| Who assesses | the administrator | the administrator | an auditor, against the checklist |
Basic setup is covered in the Linux reference: SSH access, user accounts, packages. Hardening answers the question “what should be strengthened and what restricted”, while the CIS Benchmark is an independent checklist from the Center for Internet Security, used to audit servers against compliance requirements. For your own server the first two are usually enough; you bring in CIS when a regulator or a customer requires compliance.
What you need to know first
- SSH login and keys, working on the command line: Linux reference, section “SSH access”;
- file permissions, user accounts, sudo: Linux reference, section “Users and permissions”;
- installing and updating packages with
apt: Linux reference, section “Packages”; - systemd and units, at the level of concepts:
systemctlmanages a service and its configuration lives in unit files; for details, see the systemd reference.
Section sources
the section is written from these pages- Debian Administrator's Handbook — Chapter 14: Securitydebian.org/doc/manuals/debian-handbook/security.en.html(opens in a new tab)
- Ubuntu security documentation — Overview of security featuresdocumentation.ubuntu.com/security/security-features/security-features-overview/(opens in a new tab)
- Ubuntu Server documentation — Introduction to securityubuntu.com/server/docs/explanation/intro-to/security/(opens in a new tab)
- Debian Wiki — AppArmor: How to usewiki.debian.org/AppArmor/HowToUse(opens in a new tab)