Linux › Section J · Network applications and services › Topic J.4
Network security and hardening
Linux runs a large share of the world's internet-facing servers, so a new machine is probed by automated attacks within minutes of going online. A few habits, applied consistently, defeat most of them: run little, block the rest, patch quickly, and confine whatever must run.
What attackers aim for
The Networking topic "Networks under attack" surveys threats in general; on a single Linux host three outcomes matter most. A full compromise gives an intruder root, either directly through a flawed network service or by first taking over an ordinary account and then exploiting a local weakness. A denial-of-service attack stops the machine doing its job, by flooding it with requests or by triggering a crash in a server program. Malware, such as a trojaned binary or a cryptocurrency miner, arrives through untrusted software or a compromised account. Hardening means making each of these harder to achieve and easier to notice.
Typical weaknesses
Most real break-ins exploit something ordinary. Unpatched software with a published vulnerability is the commonest: once a fix is announced, attackers study it and scan for machines that have not applied it. Weak or reused passwords are next, because lists of credentials leaked from other breaches are tried automatically against SSH and web logins. Services exposed by accident, such as a database or admin panel listening on every interface with a default password, are a third. Older protocols that send passwords in clear text, such as telnet, rlogin and plain FTP, let anyone on the path read them. Memory-safety bugs such as buffer overflows remain a risk, although defences like address space layout randomisation (ASLR) make them harder to exploit.
Run as little as possible
A service that is not running cannot be attacked. Start with sudo ss -tulpn to list everything listening, as in the topic "Diagnostic tools and remote procedure calls". For each entry, ask whether the machine needs it. Stop and disable what it does not with sudo systemctl disable --now name.service, and remove the package with apt remove or dnf remove so it cannot come back after an update. A service needed only by programs on the same machine should listen on 127.0.0.1 or a Unix domain socket, never on every interface. Replace telnet with SSH and FTP with SFTP or rsync over SSH. If an old program cannot encrypt, a wrapper such as stunnel can add TLS in front of it.
Patch promptly and automatically
Security teams concentrate on supported, stable releases, so servers should run long-term support versions, such as Ubuntu LTS, Debian stable or RHEL, rather than development branches. Install updates quickly: sudo apt update && sudo apt upgrade on Debian and Ubuntu, sudo dnf upgrade on Fedora and RHEL. Automate security updates with unattended-upgrades on Debian and Ubuntu or dnf-automatic on the Fedora family. An updated library protects nothing until the programs using it restart, and a new kernel needs a reboot unless a live-patching service applies it; needrestart (Debian family) and dnf needs-restarting (Fedora family) show what still runs old code. Follow your distribution's security advisories and the CVE identifiers they cite.
Firewalls and least privilege
A host firewall should deny incoming connections by default and allow only what the machine serves; the topic "Firewalls" shows nftables, ufw and firewalld, and the Networking topic "Operational security: firewalls and intrusion detection" covers the wider design. Block internal services, such as rpcbind on port 111, at both the host and the network edge. Then give everything the least privilege it needs. Do not create accounts for people who do not need them, because local access makes privilege escalation far easier. Run each service as its own unprivileged user, use SSH keys rather than passwords, disable root logins over SSH, and grant administration through sudo, which logs who did what. Beyond the network, encrypt disks with LUKS, and let an integrity checker such as AIDE report changed system files.
Mandatory access control: SELinux and AppArmor
Ordinary file permissions are discretionary: an owner, or root, can change them. Mandatory access control (MAC) adds a system-wide policy that confines programs even when they run as root. SELinux, default on Fedora, RHEL and their relatives, labels every process and file with a security context (shown by ls -Z and ps -Z) and allows only the interactions the policy permits. AppArmor, default on Ubuntu and Debian (openSUSE switched to SELinux in 2025), confines programs with path-based profiles in /etc/apparmor.d. Both have an enforcing mode and a mode that only logs: permissive for SELinux, complain for AppArmor. Check them with getenforce or sestatus, and with sudo aa-status. If a service hits a denial, read the logs and fix the policy rather than turning MAC off.
Worked example: hardening a new web server
A fresh Ubuntu LTS server will host one HTTPS site. 1. Patch first: sudo apt update && sudo apt upgrade, then enable unattended-upgrades. 2. Survey: sudo ss -tulpn shows sshd on port 22, nginx on 80 and 443, and an FTP server left over from the image on 21. 3. Remove what is not needed: sudo systemctl disable --now vsftpd, then sudo apt remove vsftpd. 4. Firewall: sudo ufw default deny incoming, sudo ufw allow OpenSSH, sudo ufw allow 443/tcp (and 80/tcp if it redirects to HTTPS), then sudo ufw enable. 5. Lock down SSH: keys only, PasswordAuthentication no and PermitRootLogin no, with fail2ban. 6. Check confinement: sudo aa-status lists the profiles in enforce mode.
Sandboxing services with systemd
systemd can confine a service without any extra software. In the [Service] section of a unit, User= and Group= run it unprivileged; NoNewPrivileges=yes stops it gaining privileges through setuid programs; ProtectSystem=strict makes the whole filesystem read-only to it except paths listed in ReadWritePaths=; ProtectHome=yes hides users' home directories; and PrivateTmp=yes gives it a private /tmp. systemd-analyze security lists every service with an exposure score, and systemd-analyze security nginx.service explains, setting by setting, what could be tightened. Apply options one at a time and test, because a service that needs a hidden path will fail at start-up.
Worked example: verifying a download with GPG
You download tool-2.4.tar.gz and tool-2.4.tar.gz.asc from a project site. A checksum, compared with sha256sum as in the topic "Archiving and compressing files", shows the file was not corrupted, but an attacker who altered the file could alter the checksum too. A signature also proves who released it; the Networking topics "Principles of cryptography" and "Message integrity and digital signatures" explain public keys and signatures. 1. Obtain the project's public key from a source you trust, such as its documented key server entry, and gpg --import it. 2. Check the key's fingerprint against one published separately by the project. 3. Run gpg --verify tool-2.4.tar.gz.asc tool-2.4.tar.gz. 4. Accept the file only if gpg reports a Good signature from that key. Package managers perform this check automatically for every repository package.
Common mistakes to avoid
First, installing a service "in case it is needed later". Second, applying updates but never restarting services or rebooting, so the old vulnerable code keeps running. Third, running setenforce 0 or disabling AppArmor to make an error go away; it removes protection from everything. Fourth, trusting a checksum downloaded from the same server as the file. Fifth, leaving a database or admin interface listening on 0.0.0.0. Sixth, installing binaries from unknown sites, or piping a downloaded script straight into a root shell without reading it.
Key takeaways
Attacks aim at full compromise, denial of service or planting malware, mostly through unpatched software, weak or leaked passwords, accidentally exposed services and clear-text protocols. Run as little as possible, found with ss -tulpn and removed with systemctl and the package manager; bind local services to 127.0.0.1. Use LTS releases, automatic security updates and restarts. Deny incoming traffic by default and apply least privilege. Keep SELinux or AppArmor enforcing, sandbox services with systemd options, and verify downloads with GPG signatures as well as checksums.
Flashcards
Flashcard
An intruder gains root on the machine
Test your understanding
Question 1 of 5
A database used only by an application on the same server listens on 0.0.0.0:3306. What is the simplest good fix?
Question 2 of 5
A server installed security updates to OpenSSL last week but has not restarted anything. What is the risk?
Question 3 of 5
A web service is blocked by SELinux after a change. What is the right approach?
Question 4 of 5
Why is a GPG signature stronger evidence than a SHA-256 checksum from the same website?
Question 5 of 5
Which systemd setting stops a service from gaining extra privileges through setuid programs?
