Before you change anything
This guide assumes a fresh Ubuntu 24.04 LTS virtual server with SSH access and either a root account or an existing user with sudo privileges. Use the account and SSH port supplied by your provider. Commands below are examples, not a script to run blindly on a production server.
If your VPS already runs Docker, a hosting control panel, VPN software or custom firewall rules, review their network configuration first. Adding UFW rules without understanding those services can disrupt them.
Record the starting state
cat /etc/os-release
whoami
sudo ss -tulpn
sudo ufw status verbose
systemctl is-active sshNote which TCP port SSH is listening on. The examples in the firewall section use port 22; use your actual SSH port if it differs.
Apply system updates
Fresh server images can have outstanding security updates. Update the package index and install available updates before deploying applications.
sudo apt update
sudo apt upgrade
if [ -f /var/run/reboot-required ]; then echo "Reboot required"; fiReview the package changes before accepting them. If a reboot is required, arrange it at a safe time and verify that your console and SSH access work afterward. Do not automatically reboot a server hosting live services.
Create a separate sudo account
Use an ordinary account for administration instead of routinely signing in directly as root. Choose a username other than the example if you prefer.
sudo adduser vpsadmin
sudo usermod -aG sudo vpsadmin
id vpsadminThe first command prompts you to set a password. Retain it safely because sudo will normally ask for it, even when SSH login later uses a key.
Set up SSH keys and test the new account
On your own laptop, generate an Ed25519 key using PowerShell, macOS Terminal or a Linux shell. If you already have a key, you can use that one instead. Never overwrite an existing key unless you know what you are doing.
ssh-keygen -t ed25519 -C "my-ubuntu-vps"Select a file location when prompted and use a passphrase. Copy the contents of the new .pub file only. Do not upload or share your private key.
sudo install -d -m 700 -o vpsadmin -g vpsadmin /home/vpsadmin/.ssh
sudo nano /home/vpsadmin/.ssh/authorized_keysPaste your public key as a single line, save the file, and set ownership and permissions:
sudo chown vpsadmin:vpsadmin /home/vpsadmin/.ssh/authorized_keys
sudo chmod 600 /home/vpsadmin/.ssh/authorized_keysssh vpsadmin@YOUR_SERVER_IP
sudo -vIf your private key is stored under a non-default name, specify it with ssh -i. For an authentication error, inspect the VPS logs with sudo journalctl -u ssh -n 50 --no-pager.
Configure UFW without blocking SSH
Check the existing rules before enabling UFW. Allow the actual SSH port first; this example uses the standard TCP port 22. Do not assume that every provider uses port 22.
sudo ufw status verbose
sudo ufw allow 22/tcp
sudo ufw enable
sudo ufw status verboseOpen another new SSH connection immediately after enabling UFW. If you host a web service on this VPS, add only the ports it requires (commonly TCP 80 and 443). Do not open every port merely to make an application work.
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpThose last two rules are optional and intended only for a public web server. Some container and virtualization networking setups require special firewall consideration. Check your provider's rules as well as UFW.
Disable SSH passwords only after key-based login works
This is an optional, higher-risk step. First confirm that you can sign in as vpsadmin with your key from a fresh terminal and that sudo works. Keep an existing SSH session and the browser console available throughout.
Ubuntu commonly includes configuration snippets from /etc/ssh/sshd_config.d/. Review the current settings and any existing cloud-init configuration before writing a new file.
sudo ls -la /etc/ssh/sshd_config.d/
sudo sshd -T | grep -E "^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication) "If you choose to proceed, create an early-loading local snippet. The following lines allow public keys while disabling direct root SSH login and password authentication. Verify the effective settings afterward because other files or conditional SSH rules can affect the result.
sudo nano /etc/ssh/sshd_config.d/01-local-hardening.confPermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yesBefore applying the change, validate syntax and inspect the resulting configuration:
sudo sshd -t
sudo sshd -T | grep -E "^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication) "If the syntax check fails or the effective options differ from what you intend, correct the configuration before reloading SSH. Only when both checks are satisfactory should you apply it:
sudo systemctl reload sshOpen one more fresh SSH session from your laptop and verify the new account can log in and use sudo. Do not close the original session until all tests pass. If you depend on password-based automation, another account or a rescue workflow, adapt this change rather than copying it unchanged.
Official reference: Ubuntu OpenSSH server guidance.
Check automatic security updates and backups
Ubuntu Server typically includes unattended upgrades for security updates. Verify how your particular image is configured instead of assuming every provider image has identical defaults:
dpkg -l unattended-upgrades
apt-config dump | grep -E "APT::Periodic::(Update-Package-Lists|Unattended-Upgrade)"
systemctl is-enabled apt-daily-upgrade.timerIf unattended upgrades are absent or disabled, follow Canonical's documentation to configure them. Review restart and maintenance behavior for production services. Back up your application data and databases to a separate destination; disk redundancy and server snapshots are not substitutes for a verified recovery process.
Official reference: Ubuntu security-update documentation.
Check access and services before considering the VPS ready
sudo sshd -t
sudo systemctl status ssh --no-pager
sudo ufw status verbose
sudo ss -tulpn
sudo journalctl -u ssh -n 30 --no-pagerConfirm a fresh SSH login succeeds, sudo works, intended application ports are reachable and no unexpected services are exposed. Keep a tested recovery path and backup plan. Review firewall rules whenever you deploy a new service.
If you accidentally lose SSH access
Use your provider's authorized console or rescue access rather than repeatedly guessing credentials. Check your firewall rules, the SSH service status and the SSH configuration syntax. Revert the most recent configuration change through the console, then test a new session before disconnecting.
Further reading: Ubuntu UFW firewall guide and Ubuntu user management documentation.