A Practical Baseline for Hardening a Fresh Linux Server
Published On
Oct 13, 2026
Category
- Security
- Linux
A fresh server is never as safe as it looks. The defaults are tuned for a smooth first boot, not for sitting on a public network for the next three years. The good news is that a sensible baseline takes maybe twenty minutes and removes most of the low-effort attacks you will ever see. None of this is exotic. It is the boring, repeatable groundwork we apply to every host before anything else lands on it.
This is a baseline, not a compliance audit. If you need formal coverage, map it against the CIS Benchmarks for your distribution later. Start here.
Day one: the first ten minutes
Before you change anything, get a clean baseline and make sure you can get back in.
# Confirm you have a non-root user with sudo, and that you can log in as them
# in a second terminal BEFORE you touch SSH config.
sudo -v && echo "sudo works"
# Patch everything immediately.
# Debian/Ubuntu:
sudo apt update && sudo apt full-upgrade -y
# RHEL/Rocky:
sudo dnf upgrade -y
Reboot if the kernel was updated. An unpatched kernel is the single most common reason a “new” server is already exploitable.
SSH: key-only, no root, watched
SSH is the front door. Lock it before doing anything else. Generate a key on your workstation (ssh-keygen -t ed25519) and copy the public key up with ssh-copy-id. Confirm key login works in a separate session, then edit /etc/ssh/sshd_config.d/10-hardening.conf (a drop-in file is cleaner than editing the main file):
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding no
AllowUsers deploy ops
Then reload and verify:
sudo sshd -t && sudo systemctl reload ssh # 'sshd' on RHEL/Rocky
Keep the existing session open until you have confirmed a fresh login still works. Locking yourself out of a remote box is a rite of passage you can skip.
On changing the port: moving SSH off 22 cuts log noise but is not security. Do it if you like quiet logs; do not count it as a control. What does help is rate-limiting repeated failures with Fail2ban:
# Debian/Ubuntu
sudo apt install -y fail2ban
# RHEL/Rocky
sudo dnf install -y fail2ban
Create /etc/fail2ban/jail.d/sshd.local:
[sshd]
enabled = true
maxretry = 4
findtime = 10m
bantime = 1h
Restart with sudo systemctl enable --now fail2ban. With key-only auth already in place, this is mostly about trimming brute-force noise rather than stopping a real intrusion, but it costs nothing.
Shrink the attack surface
Every listening service is a thing that can go wrong. Audit what is open:
sudo ss -tulpn
Remove anything you did not intend to run. A common offender on cloud images is an exposed mail or RPC service. Uninstall packages you do not need rather than just stopping them — a stopped service has a habit of starting again after an update.
Automatic security updates
Unattended patching for security fixes is one of the highest-leverage things you can enable. The risk of an automatic update breaking something is real but small; the risk of an unpatched CVE sitting for months is larger.
# Debian/Ubuntu
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
# RHEL/Rocky
sudo dnf install -y dnf-automatic
# Edit /etc/dnf/dnf-automatic.conf -> apply_updates = yes, upgrade_type = security
sudo systemctl enable --now dnf-automatic.timer
Schedule reboots for kernel updates in a maintenance window rather than letting them happen at random. unattended-upgrades supports Unattended-Upgrade::Automatic-Reboot-Time.
A host firewall, briefly
A host firewall is your second line of defence behind a cloud security group, not a replacement for it. Default-deny inbound, allow only what you serve. The short version with the distribution front-ends:
# Debian/Ubuntu (ufw)
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw enable
# RHEL/Rocky (firewalld)
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
For a hand-written, auditable ruleset we prefer nftables directly — that deserves its own walkthrough.
Kernel and sysctl hardening
A handful of sysctls close off common nuisances. Put them in /etc/sysctl.d/99-hardening.conf:
# Ignore ICMP redirects and source routing
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
# Log martians, enable reverse-path filtering
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.all.log_martians = 1
# SYN flood resistance
net.ipv4.tcp_syncookies = 1
# Restrict kernel pointer and dmesg exposure
kernel.kptr_restrict = 2
kernel.dmesg_restrict = 1
# Restrict ptrace to direct children
kernel.yama.ptrace_scope = 1
Apply with sudo sysctl --system. These are conservative and rarely break anything, but test in staging if you run network appliances or containers with unusual networking.
auditd: know what happened
When something does go wrong, you want a record. auditd logs security-relevant events at the kernel level.
# Debian/Ubuntu
sudo apt install -y auditd audispd-plugins
# RHEL/Rocky (often preinstalled)
sudo dnf install -y audit
sudo systemctl enable --now auditd
A minimal rule set in /etc/audit/rules.d/baseline.rules covers identity changes, sudo use, and tampering with the audit config itself:
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers
-w /etc/sudoers.d/ -p wa -k sudoers
-w /etc/audit/ -p wa -k auditconfig
-w /var/log/audit/ -p wa -k auditlog
-e 2
Load with sudo augenrules --load. The last line, -e 2, locks the rules: nobody can change them, not even root, until the next reboot. Leave it out while you test, and add it once the rules are final. Ship these logs off the host; an attacker’s first move is often to clean up locally.
Where to go from here
This baseline stops the opportunistic majority. The next layer — and where the CIS Benchmarks earn their keep — is per-service hardening, mandatory access control with SELinux or AppArmor, centralised log aggregation, and file-integrity monitoring. Get the baseline right first. A hardened host with no patches is still a soft target, and a fully patched host with an open root SSH login is still a gift. Boring, consistent fundamentals beat clever one-offs every time.