DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · SECURITY · 23 OF 187

Linux Server Hardening, SELinux and AppArmor

How to make a Linux server hard to break into, as a checklist you can apply in an afternoon: patching and unattended upgrades, fewer listening ports (ss -tlnp showed ShopRung on 0.0.0.0:8080), a host firewall, SSH hardening, least privilege, auditing setuid programs (13 on our machine), fail2ban, central logs, systemd sandboxing, and SELinux and AppArmor, plus how to debug a denial instead of switching it off.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 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
1 / 12

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

Key takeaways

  • Patch
  • fewer ports
  • firewall
  • keys, no root
  • least privilege
  • SELinux/AppArmor enforcing
  • logs off the box

Notes

Defence in depth

The idea is defence in depth. Reduce the ways in: open ports and accounts. Keep software patched. Use strong authentication. Run every service with the least privilege it needs. Add mandatory access control, so even a hijacked service is boxed in. And log enough to detect and investigate. Each layer assumes the one before it has failed, which is the right assumption.

The layers
  • fewer ways in: ports, users
  • patched software
  • strong authentication
  • least privilege for services
  • mandatory access control
  • logging and audit to detect and investigate
  • each layer assumes the one before it failed

Patch

Most successful attacks use bugs that were fixed long ago, on servers nobody updated. So patching comes first. Unattended upgrades installs security updates automatically every day on Ubuntu. Kernel fixes need a reboot, flagged by the reboot required file, or a live patching service. In container and cloud setups the habit changes: rebuild the image with updates and redeploy, rather than patching servers by hand.

Keep current
$ sudo apt update
$ apt list --upgradable
$ sudo apt install
    unattended-upgrades
  security updates, daily
/var/run/reboot-required
  kernel updates need a reboot
  (or live patching)
in images: rebuild, redeploy

Fewer ports

Every listening port is a door. ss minus t l n p shows them all. On our machine ShopRung listened on zero zero zero zero, every network address, so it would be reachable from anywhere a packet can arrive. Other services listened only on one two seven zero zero one, the local machine, which is safe. Bind internal services to localhost or a private address, and use a host firewall, ufw or nftables, plus cloud security groups, to allow only the ports you mean, often just SSH and HTTPS.

ss -tlnp (our machine)
LISTEN 0.0.0.0:8080  node
  ← ShopRung on ALL addresses
LISTEN 127.0.0.1:46879 ...
  ← local only: not reachable

bind internal services to
  127.0.0.1 or a private IP
firewall: allow 22, 443 only
  (ufw, nftables, cloud SGs)

Least privilege

Least privilege means nothing runs with more power than it needs. Setuid programs run as root whoever starts them, so they deserve a list. Our machine had thirteen, all expected: sudo, su, passwd, mount, an SSH helper and a few others. A new setuid file that you did not install, especially a shell, is a classic backdoor. Services run as their own user, with no sudo rights and no login shell.

setuid programs (our machine)
$ find /usr -perm -4000 -type f
13 found, e.g.
  /usr/bin/sudo  /usr/bin/su
  /usr/bin/passwd /usr/bin/mount
  /usr/lib/openssh/ssh-keysign
audit new ones: a setuid shell
  is a classic backdoor
services: own user, no sudo,
  no login shell

Authentication

For logins: SSH with keys only and no root login, as the last episode set up. fail2ban watches the auth log and bans addresses after repeated failures, which cuts the noise of guessing bots. Humans should use multi-factor authentication through a bastion or single sign-on. Remove accounts nobody uses, and give service accounts no login shell, so a stolen password for them opens nothing.

Login hardening
  • SSH: keys only, no root login (last episode)
  • fail2ban: ban IPs after repeated failures
  • MFA for humans (bastion, SSO)
  • remove unused accounts
  • lock service accounts: usermod -s /usr/sbin/nologin

SELinux and AppArmor

Normal permissions are discretionary: a file's owner decides. Mandatory access control adds a policy that every process must obey, even root's. So if attackers hijack nginx, the policy still stops it reading files outside its web folders. SELinux, the default on Red Hat systems, puts a label on every file and process and allows only labelled pairs. AppArmor, the default on Ubuntu, gives each program a profile listing the paths it may use. Our sandbox ran neither, so try them in your lab VM.

Mandatory access control
  • normal permissions: the owner decides (discretionary)
  • MAC: a policy decides; even a hacked root process is boxed in SELinux: labels on every file and process (RHEL)
  • AppArmor: per-program path profiles (Ubuntu, Debian)

Debug, do not disable

The common reaction to a denial is to switch SELinux off. Do not. A denial is information. On SELinux, getenforce shows the mode, ausearch shows the recent denials, and ls minus capital Z shows labels; very often files copied into a new folder simply have the wrong label, and restorecon fixes them. On AppArmor, aa status lists profiles and the kernel log shows each denied path. Fix the label or the profile, and keep the protection.

When something is denied
SELinux:
$ getenforce   Enforcing
$ ausearch -m avc -ts recent
$ ls -Z /srv/app  labels
$ restorecon -Rv /srv/app
AppArmor:
$ sudo aa-status
$ journalctl -k | grep DENIED
never: setenforce 0 for good

Log and audit

Finally, keep evidence. Authentication logs show who logged in. auditd can record access to sensitive files and commands. Ship logs to another system, because attackers delete local logs first, and keep clocks in sync so events line up across machines. And remember the systemd sandboxing options from episode nine, protect system and no new privileges: a few lines that remove whole classes of damage.

Keep evidence
  • auth logs: /var/log/auth.log or journalctl -u ssh
  • auditd: watch sensitive files and commands
  • ship logs off the server (an attacker deletes local ones)
  • time in sync (episode 19)
  • systemd sandboxing: ProtectSystem, NoNewPrivileges

Common mistakes

Mistakes. Switching off SELinux or AppArmor to make a problem go away. Services bound to every address by default. No automatic security updates. One wide-open security group shared by every server. Logs kept only on the machine an attacker controls. And hardening once at setup but never checking again; tools like Lynis or CIS benchmarks let you re-scan regularly.

Avoid these
  • setenforce 0 / disabling AppArmor to fix a denial
  • services bound to 0.0.0.0 by default
  • no automatic security updates
  • one open security group for every server
  • logs only on the server
  • hardening once, never checking again

Say it like this

In the interview: I patch automatically and rebuild images regularly, close unused ports and bind internal services to localhost, and firewall the rest. SSH with keys only and no root login, multi-factor for humans. Each service runs as its own unprivileged user, with systemd sandboxing. SELinux or AppArmor stays enforcing, and I debug denials instead of disabling it. I audit setuid files and ship logs to a central system.

In the interview
  • "Patch automatically, close
  • unused ports and bind internal
  • services locally, firewall the rest, SSH keys only, no root
  • login, least privilege for services, keep SELinux or AppArmor enforcing, audit setuid, and ship logs off the box."

Interview questions

Q1How do you harden a new Linux server?

Automatic security updates, minimal packages and listening ports, a host firewall plus cloud security groups, SSH keys only with no root login, least-privilege service users, SELinux/AppArmor enforcing, audit of setuid files, fail2ban, and central logging.

Q2What is the difference between SELinux and AppArmor?

Both are mandatory access control. SELinux labels every file and process and allows labelled interactions (RHEL default); AppArmor confines programs with path-based profiles (Ubuntu default).

Q3An app works with SELinux permissive but fails enforcing. What do you do?

Read the AVC denials (ausearch -m avc), usually fix file labels with restorecon or semanage fcontext, or adjust a boolean; do not disable SELinux.

Q4How do you list listening ports and their processes?

ss -tlnp for TCP and ss -ulnp for UDP: each listening address and port with the process that owns it.

Q5Why bind internal services to 127.0.0.1?

So they are not reachable from the network at all, regardless of firewall mistakes.

Q6Why audit setuid binaries?

They run with their owner's (often root's) privileges; unexpected ones are a common backdoor.

Q7What does fail2ban do?

Watches logs for repeated failures (for example SSH logins) and temporarily blocks offending IPs with firewall rules.

Q8Why ship logs off the server?

Attackers often delete local logs; a central store keeps evidence and allows correlation across machines.