Guides · Linux · 4 min
First Hour on a New Ubuntu 24.04 VPS: Hardening Without Locking Yourself Out
The safe order for SSH keys, firewall, port changes and updates on Ubuntu 24.04, including the cloud-init and socket activation traps that catch people out.
Most hardening guides list the right settings. Fewer tell you the order, and the order is what decides whether you finish the hour with a secure server or a server you can no longer reach. This guide follows the order that keeps a way back in at every step, and covers two Ubuntu 24.04 details that trip people up.
Rule zero: keep a second session open
Before you change anything about SSH or the firewall, open a second terminal and log in. Keep it open until you have tested a fresh login. If a change locks you out, the open session is still connected and you can undo it.
Also find your provider's web console (often called "Console", "VNC" or "Recovery"). It works even when SSH does not.
1. Update and create your own user
apt update && apt full-upgrade -y
adduser deploy
usermod -aG sudo deploy
2. Put your SSH key in place, then test it
From your own computer:
ssh-copy-id [email protected]
ssh [email protected] # must work without a password
sudo -v # must accept deploy's password
If your provider already turned off password logins and you log in as root with a key, copy that key to the new user instead of using ssh-copy-id:
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy
Do not continue until both work. Everything after this assumes key login works.
3. Turn off password and root logins, the way that sticks
Ubuntu 24.04 cloud images often ship /etc/ssh/sshd_config.d/50-cloud-init.conf containing PasswordAuthentication yes. sshd uses the first value it reads for each setting, and files in sshd_config.d are read in name order before the rest of the main file. So editing the main file does nothing while that drop-in exists.
Write your own drop-in with a name that sorts first:
printf 'PasswordAuthentication no\nKbdInteractiveAuthentication no\nPermitRootLogin no\n' \
| sudo tee /etc/ssh/sshd_config.d/00-hardening.conf
sudo sshd -t && sudo systemctl reload ssh
sudo sshd -T | grep -Ei 'passwordauthentication|permitrootlogin'
sshd -T shows the settings sshd is really using. Now test a new login in a third terminal before closing anything.
4. Changing the SSH port on 24.04 (optional)
A different port does not make SSH safer against a targeted attacker, but it cuts log noise from bots. On Ubuntu 24.04, SSH uses systemd socket activation, so restarting ssh.service after changing the port has no effect. Do it in this order:
sudo ufw allow 2222/tcp # allow the new port first
echo 'Port 2222' | sudo tee /etc/ssh/sshd_config.d/01-port.conf
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket
ss -tlnp | grep 2222 # confirm it listens
Test ssh -p 2222 [email protected] before you remove port 22 from the firewall. Also open the new port in your provider's cloud firewall, if you use one.
5. Firewall: allow first, then enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp # or your new SSH port
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
If you run Docker, published container ports skip these rules. Read how to stop Docker from bypassing UFW before you publish anything.
The UFW and SSH generator writes these commands for your ports, in the safe order.
6. Automatic security updates and brute-force protection
sudo apt install -y unattended-upgrades fail2ban
sudo dpkg-reconfigure -plow unattended-upgrades
sudo systemctl enable --now fail2ban
fail2ban's default settings protect SSH. Check it with sudo fail2ban-client status sshd.
7. Check what is listening
sudo ss -tulpn
Everything listening on 0.0.0.0 or [::] is reachable from outside unless the firewall blocks it. Databases and caches should listen on 127.0.0.1 only. See how to check whether your database is open to the internet.
What is left after the first hour
The first hour covers access and the network. Next come your web server, app settings, backups and log retention. The security checklist picks up from here with steps for your stack, and saves your progress in your browser.
How PortGate helps
The Server & Infra Guard checks these settings on every report: password logins, root login, firewall state, open ports including Docker's, and SSH brute-force attempts. Each finding comes with a fix that uses your real SSH port, writes an sshd drop-in that wins over cloud-init's, and refuses to turn off password logins if the server has no SSH key yet.