Before You Start
Do the steps in this order; each depends on the one before. Several change how you log in, so two rules apply throughout: keep your current SSH session open and test every login change from a second terminal, and find the server console in your provider's control panel first. It does not use SSH and is your way back in.
1. Update the System
# Ubuntu, Debian
sudo apt update && sudo apt upgrade
# AlmaLinux, Rocky Linux
sudo dnf upgrade
An image can be weeks old. If the kernel was updated, reboot now, while nothing depends on the server yet. The apt commands are explained in sudo apt update.
2. Create a Sudo User
Daily work belongs in a normal account that uses sudo, not in root. Replace user with the name you want:
# Ubuntu, Debian
sudo adduser user
sudo usermod -aG sudo user
# AlmaLinux, Rocky Linux
sudo useradd -m user
sudo passwd user
sudo usermod -aG wheel user
Details and checks are in how to create a sudo user.
3. Log In with an SSH Key
On your own computer, create a key pair if you have none, and copy the public key to the new account:
ssh-keygen -t ed25519
ssh-copy-id user@your_server_ip
From a second terminal, log in as that user and run sudo -v. Both must work before you continue. See how to generate SSH keys and ssh-copy-id.
4. Turn Off Root and Password Logins
With key login working for the sudo user, create a drop-in file such as /etc/ssh/sshd_config.d/00-hardening.conf with these lines:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
Check with sudo sshd -t, reload SSH (ssh on Ubuntu and Debian, sshd on AlmaLinux and Rocky Linux), and test a new login from a second terminal. Other drop-ins can override these values; see how to disable root login over SSH and how to disable SSH password authentication.
5. Turn On the Firewall
Allow SSH before you turn the firewall on, on the port SSH really uses. On Ubuntu and Debian with ufw (on Debian, install it first with sudo apt install ufw):
sudo ufw allow OpenSSH
sudo ufw enable
The OpenSSH profile is port 22; if you moved SSH, allow that port instead, such as 2222/tcp. On AlmaLinux and Rocky Linux, firewalld is usually active and allows ssh; check with sudo firewall-cmd --list-all. Use one firewall tool only, and note that ports published by Docker bypass both; see Docker port mapping. See how to set up a firewall with UFW and how to use firewalld.
6. Turn On Automatic Security Updates
Ubuntu Server ships unattended-upgrades and usually has it enabled. On Debian, install and enable it:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
On AlmaLinux and Rocky Linux the tool is dnf-automatic. Both, and how to handle reboots, are in how to enable automatic security updates.
7. Time, Hostname, and Swap
timedatectl
sudo timedatectl set-timezone UTC
sudo hostnamectl set-hostname server1.example.com
swapon --show
- Time:
timedatectlshould report that the system clock is synchronized. Logs, certificates, and two-factor codes depend on it; if it is not, runsudo timedatectl set-ntp true(Ubuntu 25.10 and later use chrony for this). See how to change the timezone. - Hostname: on Ubuntu and Debian, put the same name on the
127.0.1.1line in/etc/hostsso it resolves locally. See how to change the hostname. - Swap:
swapon --showprints nothing when there is no swap. On a server with little memory, a small swap file softens memory spikes; see how to add a swap file.
8. Install Fail2ban
Fail2ban reads the logs and blocks addresses that fail again and again. Put your settings in /etc/fail2ban/jail.local, add your own address to ignoreip, and on Debian 12 point the SSH jail at the journal, which the newer packages already do. The steps are in how to install and configure Fail2ban.
9. Take a Snapshot
When everything above works, take a snapshot in your provider's control panel: a clean starting point to return to if the next change goes wrong. A snapshot is not a backup; see snapshot vs backup. Then check what listens on the network with how to check open ports in Linux, and on AlmaLinux and Rocky Linux keep SELinux enforcing.