To secure an Ubuntu VPS, create a sudo user, log in with an SSH key, then put PermitRootLogin no and PasswordAuthentication no in a file that sorts first in /etc/ssh/sshd_config.d/, and let unattended-upgrades (or a daily cron job) install updates and reboot when Ubuntu asks for it. The one trap most guides miss: a 50-cloud-init.conf file in that folder can quietly keep password login on, because sshd uses the first value it reads.
Every command below ran on the night of 20 to 21 November 2026 in fresh Ubuntu 24.04.5 LTS and 26.04.1 LTS containers in our lab, and the outputs are copied from those runs. The same steps work on any of our Linux VPS and VDS plans in Amsterdam, Dublin and Prishtina, which come with full root access and a dedicated IPv4 and IPv6 address. Running Debian instead? Use our Debian 13 VPS security guide.
Quick fix: lock down SSH on Ubuntu in five commands (only after a key login works, see step 2)
- Create the drop-in file:
sudo nano /etc/ssh/sshd_config.d/00-hardening.confwith three lines:PermitRootLogin no,PasswordAuthentication no,KbdInteractiveAuthentication no.- Check the syntax:
sudo sshd -t(no output means OK).- Apply it:
sudo systemctl reload ssh(the service is calledsshon Ubuntu, notsshd).- Confirm what sshd really uses:
sudo sshd -T | grep -E "permitrootlogin|passwordauthentication|kbdinteractive"must shownothree times.- Turn on daily updates:
sudo apt install unattended-upgrades, then check/etc/apt/apt.conf.d/20auto-upgradeshas both values set to"1".
Key facts
- Ubuntu 24.04 ships OpenSSH 9.6p1 (our lab:
OpenSSH_9.6p1 Ubuntu-3ubuntu13.20); Ubuntu 26.04 ships OpenSSH 10.2p1 (1:10.2p1-2ubuntu3.8). - Both releases start SSH through
ssh.socket(socket activation, the default since Ubuntu 22.10 according to Ubuntu's announcement). Auth settings apply withsystemctl reload ssh; only port changes need the socket handling. - Out of the box on both releases
sshd -Treportedpasswordauthentication yes, and root may log in with a key only (without-passwordon 24.04,prohibit-passwordon 26.04, which is the same setting; the sshd_config manual lists prohibit-password as the default). - "For each keyword, the first obtained value will be used" (sshd_config(5)). Files in
sshd_config.dare read in lexical order, so00-beats50-cloud-init.conf, and99-loses to it. - Ubuntu enables automatic security updates by default once
unattended-upgradesis installed, and it runs once a day (Ubuntu Server docs, automatic updates). Automatic reboot is off by default. - Ubuntu 26.04 uses
sudo-rsas the defaultsudo(Ubuntu Foundations, 28 April 2026); our 26.04 container reportedsudo-rs 0.2.13. The commands in this guide are the same.
Why bother: what a public SSH port gets
A server with port 22 open is found by scanners fast. Palo Alto Networks' Unit 42 ran 320 honeypots with SSH, Samba, Postgres and RDP in public clouds and found that "80% of the 320 honeypots were compromised within 24 hours" (Unit 42, 22 November 2021). Those bots try lists of common and leaked username and password pairs. That is credential stuffing: if your root password is weak, or reused from a site that leaked, it is only a matter of time.
A key changes the maths. An Ed25519 key is designed to operate "at around the 128-bit security level" (RFC 8032), which nobody guesses over a network. Once password login is off, the bots still knock, but every attempt ends at Permission denied (publickey) before a password is even asked.
This is what failed password attempts look like in the log. We made three wrong root logins on purpose before hardening:
$ journalctl -u ssh --no-pager | grep "Failed password" | tail -3
Nov 20 23:03:23 lab-ubsec sshd[3146]: Failed password for root from 127.0.0.1 port 49660 ssh2
Nov 20 23:03:27 lab-ubsec sshd[3155]: Failed password for root from 127.0.0.1 port 46334 ssh2
Nov 20 23:03:29 lab-ubsec sshd[3161]: Failed password for root from 127.0.0.1 port 46350 ssh2
$ grep -c "Failed password" /var/log/auth.log
3
$ sudo lastb -n 3
root ssh:notty 127.0.0.1 Fri Nov 20 23:03 - 23:03 (00:00)
root ssh:notty 127.0.0.1 Fri Nov 20 23:03 - 23:03 (00:00)
...
On a real VPS that has been online for a day, run the same grep -c and you will usually see far more lines, from addresses you have never heard of. Ubuntu logs SSH both to the journal (journalctl -u ssh) and, through rsyslog, to /var/log/auth.log. lastb reads failed logins from /var/log/btmp.
Step 1: stop working as root, create a sudo user
Log in as root with the details from your welcome email, then create your own account and add it to the sudo group. Replace deploy with any name you like:
adduser deploy
usermod -aG sudo deploy
id deploy
adduser asks for a password twice (you will need it for sudo, not for SSH) and some optional details you can skip with Enter. The check should show the sudo group:
uid=1001(deploy) gid=1001(deploy) groups=1001(deploy),27(sudo),100(users)
Keep this root session open until the end of step 3. It is your way back in if something goes wrong.
Step 2: create an SSH key on your PC and copy it to the server
On Windows 10 or 11 (PowerShell)
Windows has OpenSSH built in. Open PowerShell (not as administrator) and run:
ssh-keygen -t ed25519
Press Enter to accept the file C:\Users\you\.ssh\id_ed25519 and type a passphrase. The passphrase protects the private key if your PC is stolen. Microsoft's OpenSSH key management page explains the two files: the one ending in .pub goes to servers, the other never leaves your PC.
Windows has no ssh-copy-id. Copy the key with scp, which copies the file byte for byte. Replace the IP with yours; each command asks for the deploy password:
ssh deploy@YOUR_SERVER_IP "mkdir -p ~/.ssh && chmod 700 ~/.ssh"
scp $env:USERPROFILE\.ssh\id_ed25519.pub deploy@YOUR_SERVER_IP:.ssh/authorized_keys
ssh deploy@YOUR_SERVER_IP "chmod 600 ~/.ssh/authorized_keys"
This writes a new authorized_keys file, which is right for a fresh account. A warning from our lab: the popular one-liner that pipes the key through PowerShell (type ...pub | ssh ... "cat >> ~/.ssh/authorized_keys") wrote an invisible byte-order mark and a Windows line ending into the file on our Windows PowerShell 5.1 test machine, and the server refused the key with Permission denied (publickey,password). The same key copied with scp logged in at once.
On macOS or Linux
ssh-keygen -t ed25519
ssh-copy-id deploy@YOUR_SERVER_IP
Real output from the lab (the client was a second account acting as the reader's laptop):
Generating public/private ed25519 key pair.
Your identification has been saved in /home/laptop/.ssh/id_ed25519
Your public key has been saved in /home/laptop/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:1uNmyck+2qathweTWO2YNI2mgwq/pEQPskndbthUda8 laptop-lab
...
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Test the key in a SECOND terminal
Open a new terminal window and log in with the key only. The two options make sure SSH cannot fall back to the password and hide a broken key:
ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no deploy@YOUR_SERVER_IP
sudo -v
A question for your key passphrase is fine. A password prompt for the account, or Permission denied, means the key is not working yet: fix that before step 3. In the lab the key login worked and the server log recorded it:
sshd[3094]: Accepted publickey for deploy from 127.0.0.1 port 51212 ssh2: ED25519 SHA256:1uNmyck+2qathweTWO2YNI2mgwq/pEQPskndbthUda8
Step 3: disable root login and password authentication on Ubuntu
Look at what is already in sshd_config.d
ls -la /etc/ssh/sshd_config.d/
sudo grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
Our container images had an empty folder. Many VPS and cloud images do not: on Ubuntu Server 24.04, /etc/ssh/sshd_config.d/50-cloud-init.conf can contain PasswordAuthentication yes, which overrides a no in the main file (Launchpad bug 2088207). Cloud-init's ssh_pwauth option controls that value (cloud-init modules reference).
We recreated that file in the lab to show why the name of your own file matters. With our settings in 99-hardening.conf, password login stayed on:
=== 99-hardening.conf + 50-cloud-init.conf
permitrootlogin no
passwordauthentication yes
kbdinteractiveauthentication no
Renamed to 00-hardening.conf, it wins:
=== 00-hardening.conf + 50-cloud-init.conf
permitrootlogin no
passwordauthentication no
kbdinteractiveauthentication no
Create the drop-in file
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
Paste these three lines, save with Ctrl+O and Enter, exit with Ctrl+X:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
KbdInteractiveAuthentication is the other way a password can be asked (through PAM); Ubuntu already sets it to no, and the line keeps it that way. If you prefer, you can also change yes to no inside 50-cloud-init.conf; the 00- file works either way.
Check, reload, verify
sudo sshd -t && echo syntax-ok
sudo systemctl reload ssh
sudo sshd -T | grep -E "^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication) "
syntax-ok
permitrootlogin no
passwordauthentication no
kbdinteractiveauthentication no
On Ubuntu the unit is ssh.service, started on demand by ssh.socket. systemctl reload ssh returned 0 on both 24.04 and 26.04 and the new settings applied to the next login. If sshd -t says Missing privilege separation directory: /run/sshd, the SSH service has not started yet since boot (the socket is waiting for the first connection); it disappeared in our lab after the first login. Changing the SSH port is different under socket activation; follow Ubuntu's socket activation notes for that.
Prove it from the client
From your second terminal, the key still works, and forcing a password is refused for both your user and root (lab output, so the address shows as 127.0.0.1):
$ ssh deploy@YOUR_SERVER_IP echo key-login-ok
key-login-ok
$ ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@YOUR_SERVER_IP
deploy@127.0.0.1: Permission denied (publickey).
$ ssh -o PubkeyAuthentication=no root@YOUR_SERVER_IP
root@127.0.0.1: Permission denied (publickey).
Note the server no longer even offers password in the list of methods. Only now close the root session from step 1.
Step 4: automatic daily updates with unattended-upgrades
This is the Ubuntu-native way. Install it (most VPS images already have it) and look at the two files that control it:
sudo apt install unattended-upgrades
cat /etc/apt/apt.conf.d/20auto-upgrades
systemctl list-timers apt-daily* --no-pager
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
NEXT LEFT LAST PASSED UNIT ACTIVATES
Sat 2026-11-21 06:58:59 UTC 7h - - apt-daily-upgrade.timer apt-daily-upgrade.service
Sat 2026-11-21 15:24:59 UTC 16h - - apt-daily.timer apt-daily.service
"1" means every day. The upgrade timer is set to OnCalendar=*-*-* 6:00 with RandomizedDelaySec=60m, so installs happen between 06:00 and 07:00 server time.
Security only, or all updates?
By default Ubuntu installs from the release and -security pockets (plus ESM if you have Ubuntu Pro). Bug-fix updates in -updates are not installed automatically. To install them too, and to reboot by itself when a new kernel needs it, create your own file instead of editing the package's 50unattended-upgrades:
sudo nano /etc/apt/apt.conf.d/52unattended-upgrades-local
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-updates";
};
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
Leave out the Allowed-Origins block if you want security updates only. Then do a dry run:
$ sudo unattended-upgrade --dry-run --debug 2>&1 | grep -E "Allowed origins|Packages that will be upgraded"
Allowed origins are: o=Ubuntu,a=noble, o=Ubuntu,a=noble-security, o=UbuntuESMApps,a=noble-apps-security, o=UbuntuESM,a=noble-infra-security, o=Ubuntu,a=noble-updates
Packages that will be upgraded: libnss-systemd libpam-systemd libsystemd-shared libsystemd0 libudev1 systemd systemd-dev systemd-resolved systemd-sysv systemd-timesyncd udev
A real run writes its log to /var/log/unattended-upgrades/unattended-upgrades.log:
2026-11-20 23:04:09,645 INFO Packages that will be upgraded: libnss-systemd libpam-systemd ...
2026-11-20 23:04:09,646 INFO Writing dpkg log to /var/log/unattended-upgrades/unattended-upgrades-dpkg.log
2026-11-20 23:04:14,182 INFO All upgrades installed
Since Ubuntu 24.04, needrestart restarts the affected services automatically after an upgrade (Ubuntu docs). A new kernel still needs a reboot, which is why a recent flaw like the NoisyTCP Linux kernel flaw is not fixed until the server restarts.
Step 5 (alternative): a daily update and upgrade cron job
Some admins prefer one plain script they can read, which runs a full apt-get full-upgrade every day and reboots only when Ubuntu creates /var/run/reboot-required. Use this instead of step 4, not on top of it. Create the script:
sudo nano /usr/local/sbin/auto-upgrade
#!/bin/bash
# Daily update + upgrade, log to a file, reboot only when Ubuntu asks for it
export DEBIAN_FRONTEND=noninteractive
LOG=/var/log/auto-upgrade.log
{
echo "=== $(date '+%F %T') start"
apt-get update -q
apt-get -y -q -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" full-upgrade
apt-get -y -q autoremove --purge
if [ -f /var/run/reboot-required ]; then
echo "reboot required by: $(cat /var/run/reboot-required.pkgs 2>/dev/null | tr '\n' ' ')"
echo "=== $(date '+%F %T') rebooting"
/sbin/shutdown -r +1 "auto-upgrade reboot"
else
echo "=== $(date '+%F %T') done, no reboot needed"
fi
} >> "$LOG" 2>&1
The two Dpkg::Options keep your edited config files when a package ships a new one, so the job never stops at a question. Make it executable, run it once by hand, and read the log:
sudo chmod 700 /usr/local/sbin/auto-upgrade
sudo bash -n /usr/local/sbin/auto-upgrade && echo syntax-ok
sudo /usr/local/sbin/auto-upgrade
sudo cat /var/log/auto-upgrade.log
=== 2026-11-20 23:04:28 start
...
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
...
=== 2026-11-20 23:04:31 done, no reboot needed
We also faked a pending kernel reboot to test the other branch. The log showed the reason and the scheduled restart, which we then cancelled with sudo shutdown -c:
reboot required by: linux-image-test
=== 2026-11-20 23:04:38 rebooting
Reboot scheduled for Fri 2026-11-20 23:05:38 UTC, use 'shutdown -c' to cancel.
Now schedule it daily at 04:30 in root's crontab:
sudo crontab -e
30 4 * * * /usr/local/sbin/auto-upgrade
sudo crontab -l should print that line back. Cron is installed and active on both our 24.04 and 26.04 images.
The honest risks of blind upgrades
- A full upgrade can bring a new version of something your app depends on. Freeze such packages with
sudo apt-mark hold PACKAGE; both methods skip held packages.apt-mark showholdlists them. - An automatic reboot drops every connection for a minute or two. Pick a quiet hour.
- Have a backup you can restore. Every RS Computers server gets free weekly backups (the last two kept) that you restore with one click; our VPS backup and restore guide shows how, and how to add your own daily copy of databases.
Step 6 (bonus): ufw firewall and fail2ban
Allow SSH before you switch the firewall on, or you lock yourself out:
sudo ufw allow OpenSSH
sudo ufw --force enable
sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
...
22/tcp (OpenSSH) ALLOW IN Anywhere
22/tcp (OpenSSH (v6)) ALLOW IN Anywhere (v6)
Add sudo ufw allow 80,443/tcp if you run a website, for example after you install PHP on Ubuntu. With password login off, fail2ban is optional, but it keeps logs quiet. Ubuntu's package already enables the sshd jail in /etc/fail2ban/jail.d/defaults-debian.conf:
sudo apt install fail2ban
sudo fail2ban-client status sshd
We let a second container try six unknown usernames. After five failures it was banned:
Status for the jail: sshd
|- Filter
| |- Currently failed: 1
| |- Total failed: 6
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 10.141.188.104
The next connection from that address got Connection refused. Unban yourself with sudo fail2ban-client set sshd unbanip YOUR_IP.
Ubuntu 24.04 vs 26.04: what changed for this guide
| Item | Ubuntu 24.04 LTS | Ubuntu 26.04 LTS |
|---|---|---|
| OpenSSH server | 9.6p1 (3ubuntu13.20) | 10.2p1 (2ubuntu3.8) |
| How SSH starts | ssh.socket, reload with systemctl reload ssh | Same |
| Default password login | yes | yes |
| Default root login | key only (without-password) | key only (prohibit-password) |
| sudo | sudo.ws | sudo-rs 0.2.13 |
| unattended-upgrades | 2.9.1, security on by default | 2.12, security on by default |
| fail2ban | 1.0.2, sshd jail on | 1.1.0, sshd jail on, journal match set to ssh.service |
The 00-hardening.conf file gave the same sshd -T result on both releases.
Checklist: secure Ubuntu server in order
| Step | Command or file | Proof it worked |
|---|---|---|
| Sudo user | adduser deploy, usermod -aG sudo deploy | id deploy shows 27(sudo) |
| SSH key | ssh-keygen -t ed25519, ssh-copy-id or scp | Key-only login works in a second terminal |
| No root, no passwords | /etc/ssh/sshd_config.d/00-hardening.conf | sshd -T shows no x3, password attempt refused |
| Daily updates | 20auto-upgrades + 52unattended-upgrades-local, or the cron script | Dry run lists origins, log shows "All upgrades installed" |
| Firewall | ufw allow OpenSSH, then ufw enable | ufw status verbose, new SSH login still works |
| Backups | Free weekly backups + your own copy | A test restore you have actually done |
Frequently asked questions
How do I disable root login on Ubuntu?
Put PermitRootLogin no in /etc/ssh/sshd_config.d/00-hardening.conf, run sudo sshd -t, then sudo systemctl reload ssh. Check with sudo sshd -T | grep permitrootlogin, which must print permitrootlogin no. Make sure your sudo user can log in first.
Why is PasswordAuthentication still yes after I set it to no?
Another file read earlier sets it to yes, usually /etc/ssh/sshd_config.d/50-cloud-init.conf. sshd keeps the first value it reads, and files in that folder load before the rest of sshd_config in lexical order. Name your file 00-hardening.conf or change the cloud-init file, then check with sshd -T.
Is it ssh or sshd on Ubuntu?
The service is ssh (ssh.service and ssh.socket). The program is sshd, so you test with sshd -t and reload with systemctl reload ssh.
Does Ubuntu install security updates automatically?
Yes, when the unattended-upgrades package is installed, which it is on most server images. It installs security updates once a day but does not reboot unless you set Unattended-Upgrade::Automatic-Reboot "true". Check /var/log/unattended-upgrades/unattended-upgrades.log to see what it did.
Should I use cron or unattended-upgrades for automatic updates?
Use unattended-upgrades unless you have a reason not to: it can limit itself to security updates, respects held packages and writes a clear log. A cron script with apt-get full-upgrade is simpler to read but upgrades everything; keep backups and hold packages you cannot afford to change.
What if I lock myself out of SSH?
Use the VNC or serial console in your provider's control panel to log in locally, then fix or delete 00-hardening.conf and reload ssh. This is why you keep the first session open and test in a second terminal.
Do I still need fail2ban if passwords are off?
Not for safety, since a key cannot be guessed, but it bans noisy scanners and keeps your logs readable. On Ubuntu it works for SSH right after apt install fail2ban.
Need a hand?
If you are starting a new server, our VPS and VDS servers with NVMe in Amsterdam, Dublin or Prishtina deploy Ubuntu automatically in under 60 seconds after payment; VPS plans use shared vCPUs and VDS plans use dedicated vCPUs for heavier workloads. If you would rather have someone build it for you, we do custom offers, custom setups and hardened systems built for you, quoted per job: message us on Telegram or email info@rscomputers-ks.com.