Turn an empty VPS into a server you can leave on the internet: your own user with key-only login, a firewall, updates with a nightly reboot, swap, a journal limit and Docker. The why is in the manuals, linked from every stage.
Start froman empty VPS from the provider’s panel: Ubuntu 24.04 or 26.04, root login with the password from the provider’s email or the key from the panel.
What you get
login example-user with a key; passwords and root over SSH are offoutside ufw: 22, 80 and 443 open, everything else droppedupdates security fixes every night, a reboot at 4:00 when neededmemory swap in /swapfile, 2 GBjournal 500 MB at most; the clock is synchronizedDocker from Docker’s repository, container logs capped at 30 MB
You’ll needThe server address 203.0.113.10 and root access: the password from the provider’s email or the key from the panel
You’ll needAn SSH key on your computer (SSH keys); a second one on another device, in case you lose the first
You’ll needA login to the provider’s panel: it has the server’s web console (Provider console)
Your values
Filled into the commands and files of this and other recipes on the path; stored in this browser only. Highlighted parts come from “Your values”.
Server address
203.0.113.10placeholder
User
example-userplaceholder
Server name
web1placeholder
Without the script the commands show placeholder values: replace them with yours.
Steps
The same recipe, short: commands from the stages, files as one line. For a second server and for those who already know what each command does.
Before changing anything, open the server’s web console in the provider’s panel and log in as root. It
bypasses the network and SSH: if you lock yourself out, it’s the only way back in (Provider console).
This stage and the start of the next run as root, so there’s no sudo.
adduser asks for a password — make it long and keep it in a password manager. You won’t log in over SSH
with it, but sudo and the provider’s console need it.
Check the login from a third window, and only then close the root window. Before locking root’s password,
log in to the provider’s console as example-user: after the lock, that’s the only way in there.
Root password
Command
What it does
sudo passwd -l root
locks root’s password, the provider’s console included
Run the first two checks from your computer, in a new window.
the web: Let’s Encrypt checks and the HTTPS redirect arrive on 80
sudo ufw --force enable
enables it; --force skips the question about SSH sessions, the SSH rule is already there
ufw doesn’t close ports that Docker publishes: they bypass its rules (Docker ports bypass the input chain). That’s
why services further along the path have no ports:.
Done when
Command
What you should see
sudo ufw status verbose | head -3
Status: active and Default: deny (incoming), allow (outgoing), deny (routed)
Ubuntu already installs security updates on its own, but a new kernel waits for a manual reboot. Your own
file turns the reboot on and cleans out old kernels; leave 50unattended-upgrades from the package alone.
/etc/apt/apt.conf.d/52unattended-upgrades-locala reboot at 4:00 when an update needs one, and old kernel cleanup
Not like this — see “If it doesn’t work”, stage cases.
If it doesn’t work
Stage 1
Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234 (unattended-upgr)
Cause
In the first minutes after boot the server installs security updates on its own.
Fix
Wait: while pgrep -x unattended-upgr >/dev/null; do sleep 5; done, then run the command again. Don’t delete the lock files.
Stage 1
During the upgrade a dialog says A new version of configuration file /etc/ssh/sshd_config is available
Cause
The provider or cloud-init changed the file, and the package doesn’t know whose version to keep.
Fix
Pick keep the local version currently installed: your settings go into a separate file in stage 3 anyway (sshd_config).
Stage 1
After a reboot the hostname is vm-4821 again
Cause
cloud-init sets the hostname from the provider’s settings on every boot.
Fix
echo 'preserve_hostname: true' > /etc/cloud/cloud.cfg.d/99-hostname.cfg, then hostnamectl set-hostname again.
Stage 2
example-user is not in the sudoers file
Cause
The user isn’t in the sudo group, or was added to it but the session is old: groups are read at login.
Fix
As root: usermod -aG sudo example-user, then log out and back in; check with groups example-user.
Stage 2
ssh example-user@… asks for the user’s password, not the key’s
Cause
The key isn’t in /home/example-user/.ssh/authorized_keys: root had none, so rsync copied nothing useful, or the client offered the wrong key.
Fix
From your computer, run ssh-copy-id from stage 2; ssh -v [email protected] 2>&1 | grep -E 'Offering|accepts' shows which key went out (SSH keys).
Stage 3
After restart ssh new sessions fail with Connection refused
Cause
The config has an error and sshd didn’t start. The open session lives on — it has a process of its own.
Fix
In the open session: sudo sshd -t names the line; fix it and run sudo systemctl restart ssh. No session left — use the provider’s console (Provider console).
Stage 3
Permission denied (publickey) for example-user, though the key worked before stage 3
Cause
AllowUsers has a different name: the user was created under a name other than the one in “Your values”, or there’s a typo.
Fix
In the open session fix the AllowUsers line in /etc/ssh/sshd_config.d/00-hardening.conf, run sudo sshd -t && sudo systemctl restart ssh, and log in from a new window (Keep the old session open).
Stage 4
After ufw enable new SSH sessions hang and time out
Cause
There was no SSH rule: the deny incoming policy closed port 22 as well.
Fix
In a session that’s still open, run sudo ufw allow OpenSSH. No session — the provider’s console, same command.
Stage 5
--dry-run installs nothing, /var/log/unattended-upgrades/ is empty
Cause
/etc/apt/apt.conf.d/20auto-upgrades says "0" or doesn’t exist — it happens with provider images.
Fix
sudo dpkg-reconfigure -plow unattended-upgrades; answering Yes recreates the file (unattended-upgrades).
Stage 5
The server rebooted at 4:00 UTC, not local time
Cause
Automatic-Reboot-Time follows the server’s time zone, and it’s still UTC.
Fix
sudo timedatectl set-timezone … from stage 1, or write the time in UTC.
Stage 6
swapon: /swapfile: swapon failed: Invalid argument or skipping - it appears to have holes
Cause
The file system is btrfs or an old XFS: fallocate makes a file with holes there, and the kernel rejects it.
Fix
sudo rm /swapfile, sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress, then chmod, mkswap, swapon from stage 6. On btrfs use btrfs filesystem mkswapfile.
Stage 6
timedatectl show -p NTPSynchronized --value says no
Cause
Outbound UDP 123 is blocked — by the provider or an outbound firewall; on 26.04 TCP 4460 too: chrony gets its NTS keys over it.
Fix
Look at timedatectl timesync-status (24.04) or chronyc sources -v (26.04) and open the outbound ports (Swap, journal and time).
Stage 7
docker-ce : Conflicts: docker.io during install
Cause
Docker from the Ubuntu repository is installed, and the stage’s first command was skipped.
Fix
Run it and install again. Images and volumes in /var/lib/docker survive remove.
Stage 7
permission denied while trying to connect to the Docker daemon socket
Cause
The command ran without sudo: the user isn’t in the docker group, by design.
Fix
sudo docker …. The docker group is root without a password (docker.sock).
How to remove it
The base isn’t taken apart piece by piece: recreating the server in the provider’s panel is faster, and
cloud-init gives you the same steps as a file at creation time (cloud-init). Only Docker comes off on
its own — along with every image, container and volume: