Hardening a Linux server means installing security updates automatically, allowing only SSH key logins, putting a default-deny firewall in front of the server, and removing every service, user and permission you don’t need. After that, you keep checking that nothing drifts. The checklist below covers the ten areas that matter most, with commands for Debian, Ubuntu and the RHEL family.
Work through it on a fresh server before you deploy anything, then repeat it every few months. For a fuller baseline, the CIS Benchmarks publish detailed guides for each distribution.
1. Install updates, automatically
Most compromises exploit known vulnerabilities that already have a fix. Patch first.
Debian / Ubuntu:
sudo apt update && sudo apt full-upgrade
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
By default, unattended-upgrades installs security updates only. Check whether a reboot is pending (for example after a kernel update):
[ -f /var/run/reboot-required ] && cat /var/run/reboot-required
RHEL / Rocky / AlmaLinux 8 and 9:
sudo dnf upgrade --refresh
sudo dnf install dnf-automatic
sudo sed -i 's/^apply_updates = no/apply_updates = yes/' /etc/dnf/automatic.conf
sudo systemctl enable --now dnf-automatic.timer
sudo dnf needs-restarting -r # tells you if a reboot is needed
To limit automatic installs to security fixes, set upgrade_type = security in /etc/dnf/automatic.conf. The Debian wiki covers unattended-upgrades in more detail.
2. Lock down SSH
Allow keys only, and never allow a root password. Put the settings in a drop-in file that sorts before any cloud-init file:
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
MaxAuthTries 3
X11Forwarding no
EOF
sudo sshd -t && sudo sshd -T | grep -Ei '^(passwordauth|permitroot)'
sudo systemctl reload ssh # RHEL family: sshd
Test a new login before you close your current session. Then add fail2ban to ban addresses that keep guessing (see SSH brute force protection).
3. Turn on a default-deny firewall
Allow only what you serve.
Ubuntu / Debian with ufw:
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose
RHEL family with firewalld:
sudo firewall-cmd --list-all
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --permanent --remove-service=cockpit # if you don't use it
sudo firewall-cmd --reload
If you run Docker, be aware that ports published by containers skip ufw. They need rules in the DOCKER-USER chain (see Docker security).
4. Review users and sudo
Every account is a way in. Look for anything unexpected:
# Accounts with UID 0 (should be only root)
awk -F: '$3 == 0 {print $1}' /etc/passwd
# Accounts with an empty password field
sudo awk -F: '$2 == "" {print $1}' /etc/shadow
# Accounts with a login shell
getent passwd | awk -F: '$7 !~ /(nologin|false)$/ {print $1, $7}'
# sudo rules that need no password
sudo grep -rn NOPASSWD /etc/sudoers /etc/sudoers.d/
Give each person their own account in the sudo group (Debian/Ubuntu) or the wheel group (RHEL). Remove accounts that are no longer used with sudo userdel -r name. Check ~/.ssh/authorized_keys for every account and remove keys nobody can identify.
5. Close services you don’t need
List everything that listens on the network:
sudo ss -tulpn
Anything bound to 0.0.0.0 or [::] can be reached from outside unless the firewall blocks it. Databases (MySQL, PostgreSQL, Redis, MongoDB) and admin panels should normally listen on 127.0.0.1 or a private network. Disable what you don’t use:
systemctl list-unit-files --state=enabled --type=service
sudo systemctl disable --now cups.service # example
Also check for programs with the SUID bit that you don’t recognise:
sudo find / -xdev -type f -perm -4000 -exec ls -l {} \; 2>/dev/null
6. Set kernel security parameters (sysctl)
A few sysctl settings reduce the attack surface at no real cost. Create /etc/sysctl.d/99-hardening.conf:
sudo tee /etc/sysctl.d/99-hardening.conf >/dev/null <<'EOF'
# Network
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Kernel
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
kernel.randomize_va_space = 2
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
EOF
sudo sysctl --system
Don’t set net.ipv4.ip_forward = 0 on a Docker or Kubernetes host, because container networking depends on forwarding. The kernel documents each parameter in ip-sysctl.
7. Keep the clock in sync
Wrong time breaks TLS checks, two-factor codes and log correlation during an incident.
timedatectl status # look for "System clock synchronized: yes"
Debian and Ubuntu use systemd-timesyncd (install it with sudo apt install systemd-timesyncd if it is missing). The RHEL family uses chrony:
sudo dnf install chrony && sudo systemctl enable --now chronyd
chronyc tracking
8. Keep logs, and keep them readable
You can’t investigate what you didn’t record. Make sure the journal survives reboots, and that logs don’t fill the disk:
sudo mkdir -p /var/log/journal
sudo systemctl restart systemd-journald
journalctl --disk-usage
sudo journalctl _COMM=sshd --since today | tail
Set a size limit with SystemMaxUse= in /etc/systemd/journald.conf, and check that logrotate handles web server logs. If you can, send logs to another machine. An attacker with root can edit the logs on the server itself.
9. Back up, and test restores
Hardening reduces risk but never removes it. Keep automated, off-server backups of data and configuration, with at least one copy the server itself cannot delete or overwrite. A backup you have never restored is only a hope, so test a restore on a schedule.
10. Monitor continuously
A hardened server drifts. Someone opens a port “temporarily”, a package goes out of date, a new sudo rule appears. You need to watch for:
- CPU, memory, disk and inode usage, with alerts before the disk fills
- Failed logins, new users, new SSH keys and new listening ports
- Changes to
/etc/passwd,sudoers,sshd_configand scheduled tasks - Packages with known vulnerabilities (the OSV database is a good public source)
- Certificates close to expiry
How Avanguard helps
- Every hour, the agent runs a full scan of packages, pending updates, users, SSH keys, passwordless sudo rules, SSH and firewall configuration, kernel security parameters, SUID programs, scheduled tasks, time sync and automatic updates.
- It reports packages with known vulnerabilities (OSV database), missing security updates, pending reboots, SSH root or password login, a missing firewall, and databases listening on all interfaces. Each finding comes with an explanation and commands to verify and fix it.
- Every 15 seconds it collects metrics, and it alerts you to new users, new SSH keys, new open ports and changes to system files.
See the Linux vulnerability scanner and server monitoring pages.
Summary
- System fully updated, with automatic security updates on and pending reboots checked
- SSH key login only, no root password login, fail2ban active
- Default-deny firewall, and Docker ports handled through
DOCKER-USER - No unexpected UID 0 users, empty passwords,
NOPASSWDrules or unknown keys - Only the services you need are listening, and databases are on localhost
- sysctl hardening applied with
sysctl --system - Clock synchronized
- Persistent journal, size limits and, ideally, remote logs
- Off-server backups with a tested restore
- Continuous monitoring and alerts