How to secure a cloud server: a practical checklist

The first hour on a new Linux VPS: SSH keys, a firewall, automatic updates, TLS and the habits that keep it safe.

A terracotta padlock resting against a cream server box

A new cloud server is reachable from the whole internet within seconds of booting. Automated scanners will start trying passwords on SSH long before you have finished installing your app.

This checklist is for developers running their own VPS on Ubuntu or Debian. It covers the first hour of hardening: secure logins, a firewall, automatic updates, brute-force protection, TLS, and keeping databases off the public internet. Each step includes the exact commands.

At the end you will also see which of these jobs a managed platform takes off your hands, so you can decide how much of this you want to own.

Before you start

You need a fresh server, its public IP address (the examples use 203.0.113.10) and the ability to log in as root or a sudo user. Keep your first SSH session open throughout. If you lock yourself out, that open session is your way back in. Most providers also offer a web console for emergencies.

SSH keys, not passwords

Passwords can be guessed. An SSH key pair cannot realistically be brute-forced, and the private key never leaves your machine.

On your own computer, create a key if you do not have one:

Terminal
ssh-keygen -t ed25519 -C "laptop"

Protect it with a passphrase. Your SSH agent or keychain can remember it so you are not typing it constantly.

Create a non-root user

Working as root means every typo runs with full power, and root is the first account attackers try. Create a normal user with sudo rights, then copy your key to it.

On the server:

Terminal
sudo adduser deploy
sudo usermod -aG sudo deploy

From your computer:

Terminal
ssh-copy-id deploy@203.0.113.10

Open a new terminal and confirm ssh deploy@203.0.113.10 works and that sudo -v asks for and accepts the user's password. Do not continue until it does.

Lock down the SSH server

Now turn off password logins and root logins. On recent Ubuntu releases, files in /etc/ssh/sshd_config.d/ are read before the main config, and for most settings the first value wins. Some cloud images ship a file there that turns password login back on, so put your settings in a file that sorts first:

Terminal
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf

Add these lines:

Text
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes

Check the syntax and the effective values, then restart SSH:

Terminal
sudo sshd -t
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication'
sudo systemctl restart ssh

On Ubuntu and Debian the service is called ssh; on Red Hat-based systems it is sshd. Test a fresh login in a new terminal before you close the original session.

Turn on the firewall

A firewall means only the ports you choose are reachable. On Ubuntu and Debian, ufw is the simplest option. Allow SSH before enabling it, or you will cut off your own connection:

Terminal
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Ports 80 and 443 are for HTTP and HTTPS. Do not open anything else unless you know why. Many cloud providers also offer a network firewall in their dashboard; using both is fine.

Automatic security updates

Most compromises use known bugs that already have fixes. Automatic security updates close that gap without you remembering to log in.

Terminal
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades

Answer yes when asked. This writes /etc/apt/apt.conf.d/20auto-upgrades with:

Text
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

Kernel updates need a reboot to take effect. Either reboot during a quiet period each month, or set Unattended-Upgrade::Automatic-Reboot "true"; in /etc/apt/apt.conf.d/50unattended-upgrades and choose a time with Unattended-Upgrade::Automatic-Reboot-Time.

fail2ban for repeated failures

With passwords off, SSH brute force cannot succeed, but fail2ban still cuts log noise and slows scanners by banning IPs that fail repeatedly.

Terminal
sudo apt install fail2ban
sudo nano /etc/fail2ban/jail.local
Config
[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
Terminal
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

Put your changes in jail.local, not jail.conf, so package updates do not overwrite them.

Keep databases off the internet

A database should only accept connections from your app. If the app and database share a server, make the database listen on localhost only.

For PostgreSQL, in postgresql.conf:

Config
listen_addresses = 'localhost'

For MySQL, in /etc/mysql/mysql.conf.d/mysqld.cnf:

Config
bind-address = 127.0.0.1

Restart the database, then check what is listening on public addresses:

Terminal
sudo ss -tlnp

Anything bound to 0.0.0.0 or your public IP is reachable unless the firewall blocks it. If the database lives on another server, use the provider's private network and allow only the app server's private IP.

HTTPS with Let's Encrypt

Every site should use HTTPS. With nginx, certbot gets a free certificate from Let's Encrypt and configures renewal:

Terminal
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run

Your DNS must already point to the server for this to work. Certificates last 90 days, and the packaged timer renews them automatically; the dry run confirms renewal will succeed.

Least privilege and secrets

  • Run each app as its own unprivileged user, not root.
  • Give the app's database user access to its own database only.
  • Keep secrets in environment variables or a secrets manager, readable only by the app's user (chmod 600 on any env file).
  • Never commit .env files. Rotate any secret that was ever pushed to a repository.

The environment variables guide covers secrets in more depth.

Logging and auditing

Know who logs in and what changes. Useful starting points:

Terminal
sudo journalctl -u ssh --since today
last -n 20

Ship logs to a separate service if you can, so an attacker who gains access cannot simply erase them. For stricter needs, auditd records detailed system events.

Backups

Hardening reduces risk; it does not remove it. Back up your data off the server, encrypt the copies and test restores. Backups and disaster recovery walks through a full plan.

Step by step: the first-hour checklist

  1. Log in, then run sudo apt update && sudo apt upgrade.
  2. Create a sudo user and copy your SSH key to it.
  3. Disable root and password logins, test, then restart SSH.
  4. Enable ufw with SSH, 80 and 443 only.
  5. Turn on unattended-upgrades.
  6. Install fail2ban with an sshd jail.
  7. Bind databases to localhost and confirm with ss -tlnp.
  8. Issue a TLS certificate with certbot.
  9. Set up off-server backups and test a restore.

What a managed platform handles

Everything above is ongoing work: updates, firewall changes, certificate renewals and log review never really end. On a managed platform, much of it is done for you.

On Launchpad, apps run on firewalled servers that only accept web traffic, each app runs in its own isolated container with CPU and memory limits, environment variables are stored encrypted and never written to logs, and every app gets HTTPS automatically. There is no SSH to harden because there is no SSH access. The trade-off is less low-level control, which self-hosting vs managed cloud discusses in detail.

Common mistakes

  • Enabling ufw before allowing SSH. Fix: run ufw allow OpenSSH first; recover through the provider's web console if locked out.
  • Editing sshd_config while a drop-in file overrides it. Fix: check effective values with sshd -T.
  • Exposing Docker ports publicly. Fix: publish on 127.0.0.1 and put a reverse proxy in front.
  • Never rebooting after kernel updates. Fix: schedule reboots or enable automatic reboots.
  • Leaving the database on 0.0.0.0. Fix: bind it to localhost or a private network address.

Security considerations

  • Keep your private key on your own devices, protected with a passphrase.
  • Remove users and keys when people leave; review ~/.ssh/authorized_keys periodically.
  • Turn on two-factor authentication for your cloud provider account. Whoever controls that account controls the server.
  • Install only the software you need. Every extra service is another thing to patch.

Troubleshooting

SymptomCauseFix
Permission denied (publickey)Key not in authorized_keys, or wrong permissionsCheck ~/.ssh is 700 and authorized_keys is 600, owned by the user
Password login still worksA drop-in file sets PasswordAuthentication yes firstCheck sshd -T, use a 00- file or edit the other file
Locked out after ufw enableSSH was not allowedUse the provider's web console, run ufw allow OpenSSH
fail2ban will not start on Debian 12No /var/log/auth.log; logs are in the journalAdd backend = systemd to the [sshd] jail
certbot challenge failsDNS not pointing here, or port 80 closedFix the A record and allow port 80

Frequently asked questions

Should I change the SSH port?

It reduces log noise from simple scanners but adds no real security. Keys only, no root login and fail2ban matter far more. If you change it, update the firewall rule first.

Is ufw enough, or do I need my provider's firewall too?

ufw on its own is fine for a single server. A provider firewall adds a second layer that works even if the server's own rules are misconfigured, so using both is good practice.

How often should I check the server?

Automatic updates handle most patching. Review logins, open ports and disk space monthly, and read your distribution's security announcements.

Do I need antivirus on a Linux server?

Generally not for a web server. Focus on updates, minimal software, strong logins and keeping your application and its dependencies patched.

Next steps

These steps take about an hour and stop the most common attacks. The harder part is keeping them up month after month.

Share this articleLinkedIn (opens in a new tab)X (opens in a new tab)Facebook (opens in a new tab)
Was this article helpful?

Get new guides in your inbox.

One email a month. Unsubscribe anytime.