A freshly imaged Ubuntu host arrives in one of two states: with a root password emailed to you, or with a temporary SSH key. Either way, the host is not ready to carry traffic. The first 30 minutes are about turning a blank cloud image into something that can be safely exposed to the network. This article walks the sequence we run on every Ubuntu 22.04 and 24.04 host before any application stack touches it.
How to verify
Confirm what the image actually shipped with before changing anything.
lsb_release -a
uname -r
who am i
sudo -l 2>/dev/null || echo "no sudo yet"
ss -tlnp | awk 'NR==1 || /sshd/'
systemctl is-active ssh
timedatectl status | head -4
If who am i returns root you are logged in as root over SSH, which the cloud provider’s default image typically allows. The goal of this article is to make that no longer true.
What’s happening
A stock Ubuntu cloud image has a few defaults that are convenient for provisioning but unsafe for running: root or a generic ubuntu user with passwordless sudo, SSH listening on port 22 with password authentication still allowed, no firewall enabled (or all-allow), and unattended-upgrades present but conservative. Each of those is a known weak point that gets probed within minutes of the host being reachable.
The initial setup sequence turns that image into a host with a single named operator account, key-only SSH, a default-deny firewall, time and update services aligned with your fleet, and centralized journald logs. None of it is exotic — but skipping any step leaves an opening that bots find before lunch.
The procedure
-
Log in as root (or as the default user with sudo), update the package index, and apply current security patches. On a brand-new image the patches will already include several CVEs disclosed since the image was built.
apt update apt full-upgrade -y apt autoremove --purge -yIf
full-upgradepulls a new kernel, schedule a reboot at the end of the setup, not now. -
Create a named operator user with sudo. Replace
opswith your team’s convention. See create-sudo-user-linux for the deeper rationale.adduser --gecos "" ops usermod -aG sudo ops install -d -m 0700 -o ops -g ops /home/ops/.ssh -
Install your SSH public key for the new user. Copy the key from your workstation into
/home/ops/.ssh/authorized_keys, then lock down ownership.nano /home/ops/.ssh/authorized_keys chmod 0600 /home/ops/.ssh/authorized_keys chown ops:ops /home/ops/.ssh/authorized_keysTest the new login in a second SSH session before continuing. If it fails, you still have the original root session to debug. Never close the first session until the second works.
-
Disable root SSH and password authentication. Edit
/etc/ssh/sshd_config(or drop a file in/etc/ssh/sshd_config.d/10-hardening.confon 22.04+):PermitRootLogin no PasswordAuthentication no KbdInteractiveAuthentication no PubkeyAuthentication yesValidate the syntax, then reload. The
reload(notrestart) keeps existing sessions alive.sudo sshd -t sudo systemctl reload sshCross-reference ssh-disable-password-auth for the full rationale.
-
Enable UFW with a default-deny inbound policy. Allow SSH first, then enable. Enabling before allowing SSH will lock you out.
sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow OpenSSH sudo ufw enable sudo ufw status verboseSee ufw-firewall-basics for layered rules and IPv6.
-
Set timezone and confirm NTP. Mismatched clocks ruin log correlation and TLS handshakes.
sudo timedatectl set-timezone America/Toronto sudo timedatectl set-ntp true timedatectl show-timesync --all | headFor fleets where chrony is the standard, swap
systemd-timesyncdper linux-timezone-ntp-chrony. -
Set a real FQDN.
localhostis fine for a developer laptop; a server needs a name the rest of the fleet can resolve.sudo hostnamectl set-hostname web-01.prod.example.net echo "127.0.1.1 web-01.prod.example.net web-01" | sudo tee -a /etc/hosts -
Enable unattended security upgrades. Ubuntu installs
unattended-upgradesby default; verify the configuration and let it auto-reboot during a maintenance window.sudo apt install -y unattended-upgrades apt-listchanges sudo dpkg-reconfigure -plow unattended-upgrades sudo unattended-upgrade --dry-run --debug | tail -20See unattended-upgrades-debian for the Debian-flavoured equivalent and
needrestartgotchas. -
Add a swap file if the image has none and you cannot guarantee memory headroom. Cloud images often ship with no swap; for sub-4 GB hosts running anything memory-bursty, a 1–2 GB file file prevents the OOM killer from harvesting your application processes. See linux-add-swap-file.
-
Reboot if the kernel was upgraded, then re-verify. From your workstation, not the existing session:
ssh ops@web-01.prod.example.net 'uname -r; uptime; ss -tlnp | grep ssh; sudo ufw status'If that returns the new kernel, a fresh uptime, sshd listening, and ufw active, the host is ready for the application stack.
Common pitfalls
- Disabling root login before testing the new user’s SSH key from a fresh session — first lockout of every junior operator.
- Enabling UFW before allowing SSH — second lockout.
- Forgetting
chmod 0700 ~/.sshand0600 authorized_keys— sshd silently refuses the key with no obvious error. - Setting
PasswordAuthentication noin the wrong include file when a vendor drop-in overrides it; always runsshd -T | grep -i passwordto read the effective config. - Leaving the default
ubuntuuser in place with passwordless sudo and an attacker-known username — delete or disable it once your operator user is verified.
In the engagements we run, this 30-minute sequence is the floor — every host in Managed Operations starts here, then layers the application stack and observability on top. If you inherit a fleet where steps were skipped, the audit and retro-fit is exactly what we run on day one.