These are the steps I take on a new Ubuntu 24.04 VM before putting anything on it. The accompanying script does all of them; this page explains the reasoning and the traps.
1. Key-only SSH, no root login
Create a key pair on your own machine and give the cloud provider the public key when creating the VM. Then forbid passwords and root login in a drop-in file, so the main sshd_config stays untouched:
# /etc/ssh/sshd_config.d/99-hardening.conf
Port 22
Port 2222
PasswordAuthentication no
PermitRootLogin no
Check the effective result with sudo sshd -T | grep -E 'port|passwordauthentication|permitrootlogin'.
2. A custom port is hygiene, not security
Moving SSH off port 22 does not stop a determined scanner. It does remove most of the automated noise from the logs and from fail2ban. The real protection is the key-only login.
The safe order matters, because it is easy to lock yourself out:
- Open the new port in the firewall first.
- Make sshd listen on both ports.
- From a second terminal, log in on the new port. Only then close port 22 and remove it from the config.
3. Ubuntu 24.04 uses socket activation for SSH
On 24.04 the ssh.socket unit decides which ports are listened on and ignores Port in sshd_config. If your custom port does not work, this is why. Disable the socket and use the plain service:
sudo systemctl disable --now ssh.socket
sudo systemctl enable ssh
sudo sshd -t && sudo systemctl restart ssh
ss -tlnp | grep sshd
4. fail2ban and automatic updates
sudo apt install fail2ban unattended-upgrades
sudo systemctl enable --now fail2ban
sudo dpkg-reconfigure -f noninteractive unattended-upgrades
The default sshd jail already bans addresses with repeated failed logins. Unattended upgrades install security updates by themselves; reboot occasionally when a new kernel was installed (/var/run/reboot-required tells you).
5. Two firewalls on Oracle Cloud
On Oracle Cloud there are two layers and a port must be opened in both:
- the Security List (or network security group) of the virtual network, in the cloud console
- the iptables rules inside the VM. The Oracle Ubuntu image ends the INPUT chain with a REJECT rule, so a new ACCEPT rule must be inserted before it:
sudo iptables -I INPUT 5 -p tcp --dport 443 -j ACCEPT
sudo netfilter-persistent save
I do not use ufw on this server: the rules are plain iptables, saved with netfilter-persistent, so there is one tool and one place to look.
6. Check what is really listening
sudo ss -tlnp # TCP listeners and their processes
sudo ss -ulnp # UDP listeners
sudo iptables -S INPUT # the live INPUT rules
On a server that only hosts a website, you should see only SSH, HTTP and HTTPS. I also disabled and masked rpcbind, which had opened port 111 for no reason I needed.
The script
The script below applies steps 1 to 5 and installs Docker and a 2 GB swap file as a safety net on small-memory VMs. Read it before you run it, set SSH_PORT, and keep a second SSH session open while it runs.
Downloads
- Download ubuntu-first-run-hardening.sh 2 KB · plain text, read it before you run it