How to set up a new VPS from scratch
The order of operations for a fresh VPS, from your first SSH login through to a locked-down server ready for a website.
A freshly provisioned VPS is a bare operating system and nothing else — no firewall configured, usually only a root account, and whatever packages the base image shipped with. None of that is dangerous by itself, but it is not something you want facing the internet for longer than it takes to secure it. This guide is the order to do things in, the first time you touch a new server.
Every command below assumes Ubuntu or Debian using apt, with the RHEL-family equivalent (Rocky Linux, AlmaLinux) using dnf noted alongside it. The two families differ in package manager, some file paths, and a handful of default service names.
1. Connect and update the system
Log in over SSH with the details your host provided — see connecting to a VPS over SSH if this is your first time. Then bring the system fully up to date before installing anything else:
sudo apt update && sudo apt upgrade -y # Ubuntu / Debian
sudo dnf check-update; sudo dnf upgrade -y # RHEL / Rocky / AlmaLinux
A VPS image can sit on a shelf for weeks between being built and being handed to you, so this step matters even on a server that has never been touched. Reboot afterwards if a kernel update was applied — the package manager will tell you if one was.
sudo reboot
2. Set a real hostname
Most images boot with a generic or randomly generated hostname. Set something meaningful — it shows up in your shell prompt, in log files, and in mail sent by system tools, and a sensible name saves confusion later when you are managing more than one server.
sudo hostnamectl set-hostname web1.yourdomain.com
Setting your server hostname and rDNS covers this in more detail, including the reverse DNS side that matters if the server will send email.
3. Create a sudo user and stop using root
Working as root for day-to-day tasks means every mistake — a wrong path in an rm command, a bad config edit — happens with no safety margin at all. Create a personal account instead:
sudo adduser yourname # Ubuntu / Debian
sudo usermod -aG sudo yourname
sudo useradd -m yourname # RHEL / Rocky / AlmaLinux
sudo passwd yourname
sudo usermod -aG wheel yourname
Full detail, including copying your SSH key across to the new account, is in how to create a non-root sudo user. Log out and back in as that user before continuing, and confirm sudo whoami returns root.
4. Lock down SSH
With a working sudo account in place, remove the two weakest points in a default SSH setup: password login and direct root login. Securing a new Linux server walks through this in full, but the short version is to install your SSH key, confirm it works in a fresh session, then edit /etc/ssh/sshd_config:
PasswordAuthentication no
PermitRootLogin no
sudo systemctl restart ssh # Debian / Ubuntu
sudo systemctl restart sshd # RHEL / Rocky / AlmaLinux
Keep your current SSH session open throughout, and open a brand new terminal window to test the key before relying on it. If the new session fails and you have already closed the old one, you are locked out with no way back in except your host's rescue console — see recovering a VPS you are locked out of if that has already happened to you.
5. Turn on the firewall
Allow SSH before you enable anything, or the very next command locks you out:
sudo ufw allow OpenSSH
sudo ufw enable # Ubuntu / Debian, using ufw
sudo systemctl enable --now firewalld # RHEL / Rocky / AlmaLinux
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
Once SSH is confirmed allowed, open only what the server actually needs — typically HTTP and HTTPS for a web server, added as you install one. Full detail, including checking rules and adding ports for specific services, is in setting up a firewall on your VPS.
6. Set the timezone and check disk space
Logs and cron jobs are much easier to reason about with a timezone you actually recognise, rather than the UTC default:
sudo timedatectl set-timezone Europe/London
timedatectl
Confirm the disk layout and free space while everything is still fresh:
df -h
7. Add swap, if the server is memory-constrained
On a smaller VPS, a modest swap file is a cheap insurance policy against an out-of-memory crash during a package install or a traffic spike. How to add swap space to a VPS covers sizing it and setting it up in a few commands.
8. Install what the server is actually for
Everything above is baseline hygiene that applies to any VPS regardless of purpose. From here, install the software the server actually exists to run:
- A web server — Nginx or Apache.
- A full LAMP stack if you need PHP and a database together.
- A free SSL certificate once a domain is pointed at the server.
- Fail2ban, to automatically block repeated login attempts against SSH and anything else you expose.
It is tempting to install the web server first and "get to security later". Do the update, sudo user, SSH hardening and firewall first regardless of what the server is for — it takes fifteen minutes, and it means the server is never sitting on the internet in its most vulnerable default state for longer than necessary.
Once the base system is secured and patched, keep it that way — how to keep a Linux server patched covers setting up unattended security updates so this does not become a manual chore every week.
Related reading
SSH is how you control a VPS from the command line, and setting it up properly the first time saves you a lot of trouble later.
How to create a non-root sudo userWhy root should not be your daily account, and the exact commands to create a proper sudo user in its place on any major distribution.
How to secure a new Linux serverThe concrete steps that actually matter for securing a fresh Linux server, in the order that keeps you from locking yourself out.
How to keep a Linux server patchedUpdating a VPS safely, handling the reboots that kernel updates require, and automating security patches so this stops being a manual chore.