What you end up with
before after
root logs in with a password root can't log in over SSH
example-user logs in with a key, sudo asks example-user's password
packages as of the image all updates installed, server rebooted into the new kernel
name vm-4821, UTC name web1, your time zone
All steps are on one server: address 203.0.113.10, user example-user. Until you reach step 9 password login is
still open, so don’t leave this for tomorrow.
Setup
Check the fallback access
In the provider’s panel, open the server’s web console (VNC, “Console”, “Emergency access” — every provider names it differently) and log in as root. If the console won’t let you in or there’s no root password, sort it out now while SSH is open: later it’s the only way onto the server if you lock yourself out (Provider console).
Log in as root
ssh [email protected]On the first login
sshasks about the server key. Compare the fingerprint through the console from the previous step:ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub(Server fingerprint).Install updates
apt update apt full-upgrade -yfull-upgradealso removes packages when an upgrade can’t go in otherwise; on a fresh server that’s safe. IfaptsaysCould not get lock, the server is already updating itself — wait (see “If it doesn’t work”).If the kernel was updated, reboot right away, while nothing runs on the server yet:
cat /var/run/reboot-required 2>/dev/null && reboot # *** System restart required ***Log in again after a minute.
uname -rshows the new kernel:6.8.0-…on 24.04,7.0.0-…on 26.04.Set the hostname
The name shows in the shell prompt, in logs and in mail from the server. It’s how you tell servers apart with several windows open.
hostnamectl set-hostname web1 sed -i 's/^127\.0\.1\.1.*/127.0.1.1 web1/' /etc/hosts grep -q '^127.0.1.1' /etc/hosts || echo '127.0.1.1 web1' >> /etc/hostsWithout the
/etc/hostsline everysudoprintsunable to resolve host. If the provider manages the name through cloud-init, it comes back after a reboot — usepreserve_hostname: true, see “If it doesn’t work”.Set the time zone
timedatectl set-timezone Europe/Berlin # your own zone; list: timedatectl list-timezones timedatectl # Local time: Sun 2026-10-18 21:40:11 CEST # Universal time: Sun 2026-10-18 19:40:11 UTC # Time zone: Europe/Berlin (CEST, +0200) # System clock synchronized: yes # NTP service: activeThe time zone decides when “4:00” comes for the automatic reboot (unattended-upgrades) and for cron. If you have servers in several countries and compare their logs, keep them all on UTC. The zone list:
timedatectl list-timezones.System clock synchronized: yesmeans the clock syncs. If it saysno, deal with it now (Swap, journal and time): without accurate time, certificates aren’t issued and logs lie.Create your own user
adduser example-user usermod -aG sudo example-useradduserasks for a password — make it long and store it in a password manager. You won’t log in over SSH with it, butsudoand the provider console need it.The
sudogroup grantssudothrough the%sudo ALL=(ALL:ALL) ALLrule in/etc/sudoers. No separate file insudoers.dis needed.On 26.04,
sudo-rsreplaces classicsudo. Rules and the group are the same; the differences are small: the password shows as asterisks, andsudo -Edoesn’t pass environment variables — list the ones you need withsudo --preserve-env=HTTP_PROXY ….Give the user a key
If root already has the key (the provider’s panel put it there), copy it with the right ownership:
rsync --archive --chown=example-user:example-user /root/.ssh /home/example-user/If there’s no key on the server, run this from your computer while passwords are still open:
ssh-copy-id [email protected](on Windows, the command from SSH keys, step 3). Add a second key from another device right away.Log in as the new user — in a second window
Don’t close the root window (Keep the old session open). In a new window:
ssh [email protected] sudo -v && sudo whoami # [sudo] password for example-user: # rootA login without the user’s password (only the key’s passphrase was asked) and
rootin the reply — all set.Close password and root logins
In the second window, now as
example-user:sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF' # Local sshd settings. Named 00-: sshd uses the first value it finds PasswordAuthentication no KbdInteractiveAuthentication no PermitRootLogin no AllowUsers example-user EOF sudo sshd -t && sudo systemctl restart sshThe file is called
00-…so it’s read before50-cloud-init.conf: that one often hasPasswordAuthentication yes, andsshduses the first value. The rest of the file —MaxAuthTries, forwarding, timeouts — is covered in sshd_config.Test from a third window (see “How to check”), and only then close the root window.
Lock the root password
sudo passwd -l root sudo passwd -S root # root L 2026-10-18 0 99999 7 -1Lin the second field means the password is locked. Root can no longer log in with a password anywhere, including the provider console — there you log in asexample-userand usesudo. So do this step only afterexample-userhas logged in through the console with its password at least once.
Next: the firewall (The ufw firewall).
If it doesn’t work
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 for the process to finish:
while pgrep -x unattended-upgr >/dev/null; do sleep 5; done. Don’t delete the lock files.
During the upgrade — a dialog 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 own settings live insshd_config.d/anyway (sshd_config).
sudo: unable to resolve host web1: Name or service not known
- Cause
- The hostname was changed, but
/etc/hostsdoesn’t have it. - Fix
- Add the line
127.0.1.1 web1to/etc/hosts.
After a reboot the hostname is vm-4821 again
- Cause
- cloud-init sets the name from the provider’s settings on every boot.
- Fix
echo 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-hostname.cfg, thenhostnamectl set-hostnameagain.
example-user is not in the sudoers file
- Cause
- The user isn’t in the
sudogroup, or was added 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 withgroups example-user.
The new user logs in but gets Could not chdir to home directory /home/example-user
- Cause
- The user was created with
useraddwithout-m— there’s no home directory. - Fix
- Create users with
adduser: it makes the directory, copies/etc/skeland asks for a password.
How to check
From your computer, in a new window. No one’s password is accepted, and root can’t log in even with a key:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password [email protected]
# [email protected]: Permission denied (publickey).
ssh [email protected]
# [email protected]: Permission denied (publickey).
Your own login works, and so does sudo:
ssh -t [email protected] 'sudo whoami'
# [sudo] password for example-user:
# root
On the server, what’s in effect with all files applied:
sudo sshd -T | grep -Ei '^(passwordauthentication|permitrootlogin|allowusers) '
# permitrootlogin no
# passwordauthentication no
# allowusers example-user