Initial Ubuntu VPS Setup — Step-by-Step Guide
Run on the server with one command (check the script contents below first):
curl -sSL https://cp.lv/scripts/pervichnaya-nastrojka-vps-ubuntu.sh | sudo bash…or with wget:
wget -qO- https://cp.lv/scripts/pervichnaya-nastrojka-vps-ubuntu.sh | sudo bashScript contents pervichnaya-nastrojka-vps-ubuntu.sh:
#!/usr/bin/env bash
# Initial Ubuntu setup: updates, user with sudo, time zone, ufw
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
NEWUSER=deploy
TIMEZONE=Europe/Riga
# 1. Update the system (non-interactive)
sudo apt-get update -y
sudo apt-get upgrade -y
# 2. Create the user non-interactively and grant sudo
if ! id "$NEWUSER" &>/dev/null; then
sudo adduser --disabled-password --gecos "" "$NEWUSER"
fi
sudo usermod -aG sudo "$NEWUSER"
# 3. Time zone
sudo timedatectl set-timezone "$TIMEZONE"
# 4. Basic firewall (SSH first, or you lock yourself out!)
sudo apt-get install -y ufw
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw --force enable
sudo ufw status verbose
echo "Done: system updated, user $NEWUSER created with sudo, time zone $TIMEZONE, ufw enabled."
echo "Remember to set the user password: sudo passwd $NEWUSER"
The first things to do on a new Ubuntu VPS are: update the packages, create a separate user with sudo rights, set the time zone and turn on a basic firewall. Below is the exact sequence of commands, and it takes about 10 minutes.
Step 1. Log in as root
After the server is created you get an IP address and the root password (or a key). Connect over SSH:
ssh root@YOUR_IP
Step 2. Update the system
Refresh the package list and install the available updates. This closes known vulnerabilities in software that is already installed.
apt update
apt upgrade -y
Installing a new kernel sometimes requires a reboot:
reboot
Step 3. Create a new user
Working as root all the time is unsafe. Create a separate user — the command asks for a password and some optional details.
adduser deploy
Replace deploy with any name you like. Set a strong password.
Step 4. Grant sudo rights
Add the user to the sudo group so they can run administrative commands:
usermod -aG sudo deploy
Check that the rights work. Log in again as the new user and run:
su - deploy
sudo whoami
If the answer is root, the rights were granted correctly.
Step 5. Set the time zone
By default a server is often on UTC. Logs and cron jobs are easier to follow if you set the zone you actually work in:
sudo timedatectl set-timezone Europe/Riga
Check the result:
timedatectl
To see the list of available zones:
timedatectl list-timezones | grep Europe
Step 6. Turn on a basic firewall
Ubuntu ships with the handy ufw utility. Allow SSH, HTTP and HTTPS, then enable the firewall:
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
Check the status and the open ports:
sudo ufw status verbose
Important: allow SSH first and only then enable ufw, otherwise you risk losing access to the server.
Step 7. Set up SSH key login
Keys are safer than a password. On your own computer generate a pair (if you do not have one yet) and copy the public key to the server:
ssh-keygen -t ed25519
ssh-copy-id deploy@YOUR_IP
After that you can disable password login in /etc/ssh/sshd_config — see the separate guide on SSH keys.
Basic setup checklist
| Task | Command | Why |
|---|---|---|
| Update | apt update && apt upgrade |
Close vulnerabilities |
| User | adduser deploy |
Do not work as root |
| Sudo rights | usermod -aG sudo deploy |
Administrative commands |
| Time zone | timedatectl set-timezone |
Correct logs |
| Firewall | ufw enable |
Limit open ports |
| SSH keys | ssh-copy-id |
Secure login |
Frequently asked questions
Do I need to reboot the server after apt upgrade?
Only if the kernel or system libraries were updated. You can check with ls /var/run/reboot-required — if the file exists, a reboot is recommended.
Can I delete the root user?
No, root must not be deleted — it is a system account. It is enough to restrict root login over SSH with PermitRootLogin no in sshd_config.
What if I forgot to grant sudo and logged out of root?
Connect again as root, or use the KVM console in the server control panel, and repeat usermod -aG sudo.
Is a firewall mandatory?
It is strongly recommended. Even basic ufw rules reduce the attack surface by closing unnecessary ports.
Summary
After these steps you have a secure starting point: the system is updated, there is a separate user with sudo, the time zone is set and the firewall is on. Next you can install a web server, databases and applications.
Need a server to practise on? Take a look at Linux VPS, pick a plan in the VPS hosting section, or order Server administration if you would rather leave the setup to specialists.