How to harden a Linux server: SSH, updates, firewall and logging


SaveAdd to Google
Illustration: a server with a terminal prompt

At a glance

Audience
Developers, DevOps & IT
IT & security teams
Difficulty
Intermediate
Time needed (minutes)
60 min
Last reviewed
Nov 22, 2026

What you need

  • A Debian or Ubuntu server with sudo access
  • Console access through your hosting provider, in case of a lockout
  • An SSH client on your computer

A fresh Linux server on the internet starts receiving login attempts within minutes. Most are automated bots trying common passwords and known flaws, and most of them fail against a server that has had one careful hour of hardening. This guide walks through that hour on Debian or Ubuntu; the same ideas apply to any distribution.

🔑

Keys, not passwords

SSH with keys only, no root login.

🧱

Only what is needed

A firewall that denies by default, and no extra services.

📜

Patched and watched

Automatic security updates, logs sent somewhere safe.

▲ Before you change SSH

Keep your current SSH session open while you work, and test every change from a second terminal. Make sure you have console access through your hosting provider in case you lock yourself out.

1. A named user, and keys instead of passwords

# On your own computer: create a key (protect it with a passphrase)
ssh-keygen -t ed25519 -C "[email protected]"
ssh-copy-id [email protected]

# On the server: a personal admin account with sudo, instead of root
sudo adduser admin
sudo usermod -aG sudo admin

Then put your SSH settings in their own file, so package updates never overwrite them:

# /etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers admin
MaxAuthTries 3
X11Forwarding no
sudo sshd -t && sudo systemctl reload ssh   # check the syntax first, then reload

✅ Test before you log out

From a second terminal, confirm that ssh admin@server works with your key and that ssh root@server is refused. Only then close the first session.

2. Automatic security updates

sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades   # answer "Yes"

This installs security updates every day. Decide how reboots happen (a maintenance window, or a tool such as needrestart to restart services), and check /var/log/unattended-upgrades/ now and then. Kernel and OpenSSL fixes only protect you after a restart.

3. A firewall that denies by default

sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH          # or: sudo ufw allow from 203.0.113.0/24 to any port 22
sudo ufw allow 443/tcp          # only the services this server offers
sudo ufw enable
sudo ufw status verbose

Better still, allow SSH only from your office address, a VPN or a bastion host, and use your cloud provider's security groups as a second layer.

4. Slow down password guessing

sudo apt install fail2ban
# /etc/fail2ban/jail.local
[sshd]
enabled = true
maxretry = 5
bantime = 1h

With password logins off, brute force cannot succeed, but fail2ban keeps the logs readable and blocks noisy scanners. Add jails for your web server's login pages too.

5. Remove what you do not need

sudo ss -tulpn                     # what is listening, and on which address?
systemctl list-unit-files --state=enabled
sudo apt purge <package> && sudo apt autoremove

Databases, caches and admin panels should listen on 127.0.0.1 or a private network, never on a public address. Every service you remove is one you never have to patch.

6. Logs you can trust

  • Send logs to another system (a log server, Wazuh, your cloud provider's logging), so an intruder cannot erase their tracks.
  • Install auditd to record changes to sensitive files such as /etc/passwd, /etc/sudoers and SSH keys.
  • Alert on new sudo users, new SSH keys in authorized_keys and logins from unexpected places.

7. Check your work

sudo apt install lynis
sudo lynis audit system

Lynis lists warnings and suggestions with a hardening score. For a full standard, compare the server with the free CIS Benchmark for your distribution.

Harden it once, then write it down as code, so every new server starts hardened.

Server checklist

  • Personal sudo accounts; root login and password login disabled
  • Automatic security updates and a reboot plan
  • Firewall denies by default; SSH limited to known addresses
  • Only needed services; internal services on private addresses
  • Logs shipped off the server; alerts on new admins and keys
  • The setup captured in Ansible, cloud-init or a golden image

Keep learning

Next: lock down your cloud account, and set up logging and detection.

All guides for developers, DevOps and IT →

  • Linux
  • SSH
  • Server hardening
  • DevOps

Sources

Editor's Picks

The best of cybersecuritynews.ca, delivered to you weekly.

On Display
Linux

Linux KVM Flaw Lets Guest VMs Reach Host Memory on AMD Servers With Nested Virtualization

Sonic6

SonicWall Tells Customers to Pull Gen 6 Firewalls Off the Internet

Staples Canada says attackers accessed customer records on its Staples Print website between November 1 and 5, 2026.

Staples Canada Confirms Breach of Print Website, Customer Contact Details and Encrypted Passwords Exposed