Skip to content

Alpha Book

Actu

Linux Security: Best Practices for Effectively Protecting Your System

A freshly installed Linux server already exposes network services, has an active root account, and uses default settings designed for compatibility, not…

Ingénieur informatique analysant une interface Linux en ligne de commande pour sécuriser un système, assis dans un bureau moderne

A freshly installed Linux server already exposes network services, an active root account, and default settings designed for compatibility rather than security. Every listening service, open port, and overly broad permission represents a potential entry point. Securing a Linux system starts with reducing this attack surface before even considering detection tools.

Check which Linux kernel is actually running after an update

Installing kernel security patches is not always enough. Until the system is rebooted, the old kernel version remains in memory. You may have just installed a critical patch, but the vulnerable version remains active until you reboot.

To check, compare the output of uname -r with the latest kernel installed by your package manager. If they differ, the patch is not yet in effect.

Live patching (offered by some distributions) can reduce downtime by applying patches on the fly. It does not cover every vulnerability and does not always eliminate the need for a full reboot. On a production server, scheduling a reboot after every major kernel update remains the most reliable approach. Several resources compile recent security advisories for popular distributions, such as the Hebdo Linux security website, which lists patches by distribution.

Local kernel vulnerabilities are often used to escalate privileges after an initial compromise. An attacker who gains limited access through a misconfigured web application can exploit an unpatched kernel to become root. Limiting local accounts reduces this risk of privilege escalation.

Cybersecurity analyst configuring a Linux firewall at a multi-monitor workstation in a security operations center

Harden SSH configuration: more than just changing the port

Changing the default SSH port only slows down automated scans. Real protection comes from configuring the daemon itself.

Have you ever looked at the authentication logs of a server exposed to the Internet? Hundreds of login attempts per hour target the root account using common passwords. Disabling direct root access over SSH (PermitRootLogin no in sshd_config) cuts off this attack vector.

Here are the settings to check first in your SSH configuration:

  • Public key authentication only: disable passwords (PasswordAuthentication no) once your keys are deployed to block brute-force attacks
  • Restrict authorized users with the AllowUsers directive instead of allowing all system accounts to attempt a login
  • Limit encryption algorithms to the latest options by excluding older ones (such as CBC) using Ciphers and MACs
  • Set an inactivity timeout with ClientAliveInterval and ClientAliveCountMax to close abandoned sessions

Every change to sshd_config requires reloading the service. Always test from a second open session to avoid locking yourself out.

Service management and reducing the Linux attack surface

A standard Linux system enables services by default that you probably don’t need. A web server has no reason to run a print server or a Bluetooth agent.

List active services with systemctl list-units --type=service --state=running. Each line represents a listening process, a set of dependencies, and a potential risk. Disable any service that your use case does not require.

The same logic applies to network ports. Running ss -tlnp displays listening ports and their associated processes. If a port does not correspond to a legitimate service, it is leftover installation clutter that should be removed.

Firewall and network filtering

A properly configured firewall allows only expected traffic. On recent distributions, nftables is gradually replacing iptables. The principle remains the same: deny everything by default, then open only the ports you need.

A typical web server needs only ports 80, 443, and your SSH port. Close everything else. This “deny all, allow specific” approach turns your network security policy from a sieve into an active filter.

Developer's hands configuring SSH and Linux security settings on a laptop in a welcoming workspace

Access rights and sudo: the principle of least privilege

Why follow this principle? Because if a user account with broad permissions is compromised, the attacker gets those same permissions. Carefully configuring sudo limits the damage in the event of an intrusion.

Instead of adding a user to the sudo group with full access, define the specific commands they are allowed to run in /etc/sudoers (using visudo). A database administrator does not need to restart the firewall.

  • Give each role only the sudo commands it needs, not ALL=(ALL) ALL access
  • Enable logging of sudo commands to keep a record of every privileged action
  • Regularly check the files in /etc/sudoers.d/: rules left behind after someone leaves can persist for months

An overly permissive sudoers file is equivalent to having no privilege separation. Audit it at least once a quarter.

Permissions for sensitive files and directories

System configuration files (/etc/shadow, /etc/ssh/sshd_config, private keys) require strict permissions. Running chmod 600 on private SSH keys and chmod 640 on /etc/shadow prevents unauthorized accounts from reading them.

Periodically search for files with the SUID bit enabled (find / -perm -4000). This bit allows a program to run with its owner’s permissions, often root. Every unnecessary SUID binary is a privilege escalation vector waiting to be exploited.

The ANSSI-BP-028 guide, published by ANSSI and dedicated to GNU/Linux system configuration recommendations, details these points with hardening levels suited to different contexts (minimal, intermediate, enhanced). Matching the hardening level to the server’s actual role helps avoid breaking applications by applying restrictions that are too strict for their intended use. Containers, for example, may depend on kernel features (such as unprivileged user namespaces) that an overly aggressive policy would disable.

Linux security is not a fixed state. Kernel patches, reviews of active services, sudoers file audits, and permission checks form a cycle that must be repeated. A server hardened in January with rules that have not been reviewed by June becomes a target again.

Linux Security: Best Practices for Effectively Protecting Your System