Your new VPS has been running for less than an hour, and its SSH log is already full of login attempts from IPs you don't know, as root, admin and ubuntu, guessing passwords one by one. Bots (automatic programs) on the internet don't wait for you to be ready.
A VPS (Virtual Private Server) is a virtual server you rent: one VM with its own CPU, RAM and disk, a public IP, and full root access. The moment it is created, anyone on the internet can reach it, so you spend its first hour making it safe. Picture getting a shophouse (a small shop building) for the second branch of your food stall: the shophouse is your VPS. The owner hands you the master key (the root password). You have your own key made for your staff (the deploy user and its SSH key), then try the new key in the door first before you stop using the master key (closing root and password logins). You shut every door (port) except the ones you use (the firewall), and hire a guard who stops anyone who enters the door code wrong five times (fail2ban). Last, you set the wall clock to local time (the time zone) and prepare a spare storeroom (swap).
The first hour on web-2
notes-api needs a second server, web-2 (Ubuntu 24.04, 1 GB of RAM, IP 203.0.113.11). The provider emails the root password. This table is the order of steps you run in the Lab, and the order matters:
| # | Step | Main command |
|---|---|---|
| 1 | Update packages | apt update && apt upgrade -y |
| 2 | A normal user with sudo | adduser deploy, usermod -aG sudo deploy |
| 3 | An SSH key for deploy, then a test | ssh-copy-id [email protected] (from the laptop) |
| 4 | Close root and passwords | PermitRootLogin no, PasswordAuthentication no |
| 5 | Open the ports in use | ufw allow 22/tcp, 80/tcp, 443/tcp |
| 6 | Turn on the firewall | ufw enable |
| 7 | An SSH guard | apt install -y fail2ban |
| 8 | Time zone | timedatectl set-timezone Europe/London |
| 9 | Swap | a 1 GB swap file |
The lesson Server Hardening Basics (Security track) explains the reasons behind each security setting in more depth.
First login and updates
From the laptop, log in as root with the password from the email. ssh (Secure Shell) is the command for getting into a server's terminal over an encrypted connection. On the server, install the updates with apt, Ubuntu's tool for installing programs (packages): apt update fetches the list of the latest versions, apt upgrade installs them, and -y answers "yes" automatically:
budi@laptop:~$ ssh [email protected]
root@web-2:~# apt update && apt upgrade -y
...
37 packages can be upgraded. Run 'apt list --upgradable' to see them.
...
37 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
The provider makes a VPS from an image (a ready-made copy of an Ubuntu disk, not a Docker image). That image was usually built weeks earlier, so the security patches released since then are installed first.
The deploy user and an SSH key
Root can do anything, so day-to-day work uses a normal user that raises its rights through sudo:
root@web-2:~# adduser deploy
info: Adding user `deploy' ...
...
info: Creating home directory `/home/deploy' ...
info: Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
...
root@web-2:~# usermod -aG sudo deploy
adduser deploycreates the user and the folder/home/deploy, and asks for its password. After password logins are closed, this password is only forsudo.usermod -aG sudo deployadds deploy to thesudogroup (members of this group may usesudo).-Gnames the group. The-a(append) matters: without-a, deploy is removed from its other groups.
Then on the laptop, install your public key on the server. An SSH key is a pair of keys: a private key that stays on your laptop, and a public key that goes on the server. If you don't have one, make it first with ssh-keygen -t ed25519 (ed25519 is the type of key):
budi@laptop:~$ ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/budi/.ssh/id_ed25519.pub"
...
[email protected]'s password:
Number of key(s) added: 1
ssh-copy-id logs in once as deploy with the password, then adds your public key to /home/deploy/.ssh/authorized_keys (the list of public keys allowed to log in as deploy). That's why it has to run before passwords are switched off. Then test ssh [email protected] in a second terminal. If you get in without a password prompt, the key works.
Closing root and passwords
Open /etc/ssh/sshd_config (the SSH server's settings file) with nano, and write these two lines:
# /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
Then check the config and apply it:
root@web-2:~# sshd -t
root@web-2:~# systemctl restart ssh
PermitRootLogin no: root can't log in over SSH at all.PasswordAuthentication no: logins only with a key. Bots guessing passwords are refused.sshdis the SSH server program.sshd -tchecks how the config is written and stays quiet when it's right.systemctl restart sshapplies the new config (on Ubuntu, the SSH server's service is calledssh). Files in/etc/ssh/sshd_config.d/can override these values (many cloud images putPasswordAuthentication yesthere), so check the value in effect withsshd -T | grep -i passwordauthentication. The details are in Server Hardening Basics.
Restarting SSH doesn't end the sessions that are open: only the next login changes, so the root session where you edited the config keeps running. Suppose the key isn't installed after all. A new login is refused with [email protected]: Permission denied (publickey)., because passwords are closed and the key isn't there. As long as the old root session is open, you can still fix it, so don't close it before a login in a second terminal works.
Firewall: allow first, then enable
ufw is Ubuntu's built-in firewall. ufw allow 22/tcp opens port 22 for TCP connections (which SSH uses). ufw enable turns the firewall on:
root@web-2:~# ufw allow 22/tcp
Rules updated
Rules updated (v6)
root@web-2:~# ufw allow 80/tcp && ufw allow 443/tcp
Rules updated
Rules updated (v6)
Rules updated
Rules updated (v6)
root@web-2:~# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
Once active, UFW on Ubuntu refuses every incoming connection except to the ports that were allowed. Port 22 (SSH) has to be allowed before ufw enable. If the order is reversed, the open session keeps running, but the next login ends with ssh: connect to host 203.0.113.11 port 22: Connection timed out.
fail2ban, time zone, swap
root@web-2:~# apt install -y fail2ban
root@web-2:~# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd
root@web-2:~# timedatectl set-timezone Europe/London
root@web-2:~# fallocate -l 1G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6f1c2a9e-3b7d-4e58-9a0c-2d4f8b1e7a63
root@web-2:~# echo '/swapfile none swap sw 0 0' >> /etc/fstab
- fail2ban reads the SSH log and blocks IPs that fail to log in again and again (5 times by default, then blocked for 10 minutes). On Ubuntu, the
sshdjail (fail2ban's blocking rule for SSH) is active as soon as fail2ban is installed. - The time zone
Europe/Londonmakes the times in the logs (journalctl, the app's log) match your working hours (UK time). - Swap is part of the disk used as extra memory when RAM is full. It's slow, but with 1 GB of RAM, an
npm ci(installing dependencies) that runs out of memory isn't stopped by Linux right away (Killed). Thefallocateline makes a 1 GB file/swapfile, marks it as swap (mkswap), then turns it on (swapon). The line in/etc/fstab(the list of what Linux attaches at boot) turns swap on again after a reboot.
From service names to UFW commands
In the exercise, generateUfwRules(services) turns a list of service names into UFW commands, then adds "sudo ufw enable" at the very end, exactly the safe order above. The port map: ssh 22, http 80, https 443, mysql 3306, postgres 5432, redis 6379. So ["ssh", "http", "https"] becomes ["sudo ufw allow 22/tcp", "sudo ufw allow 80/tcp", "sudo ufw allow 443/tcp", "sudo ufw enable"]. On a real server, database ports like 5432 and 6379 must not be open to everyone. This exercise only tests the port mapping.
⚠️ How to lock yourself out of your own server
- Switching off passwords before the key is in. The next login is refused:
Permission denied (publickey).Install the key, test it in a second terminal, then close passwords. ufw enablebeforeufw allow 22/tcp. The next login ends withConnection timed out. Allow first, enable last.- Closing the old session too soon. The open session is your way back into the server. Close it after a new login works.
usermod -Gwithout-a. The user is removed from its other groups. Always-aG.
TL;DR: A VPS (Virtual Private Server) = a rented VM (virtual server) with a public IP and root access, and you get into its terminal with ssh. The first hour on a new VPS, web-2, in order: updates with apt update && apt upgrade -y, a normal user with adduser deploy plus usermod -aG sudo deploy, then an SSH key (a key that replaces the password) with ssh-copy-id from the laptop and a test login in a second terminal. After that, close root logins and password logins: PermitRootLogin no and PasswordAuthentication no (check with sshd -t, apply with systemctl restart ssh), and the firewall: ufw allow 22/tcp, 80, 443 before ufw enable. Last, fail2ban (it blocks IPs that fail to log in again and again), timedatectl set-timezone Europe/London, and 1 GB of swap (disk used when RAM is full). The first security steps: disable root login, create a new user, use SSH keys, turn on the firewall. Restarting SSH doesn't end open sessions, so keep the old one until a new login works.
⚡ A normal user with sudo
Write:
root@web-2:~# adduser deploy
root@web-2:~# usermod -aG sudo deploy
Meaning: On the new VPS (rented virtual server) web-2, you are logged in as root. Root can do anything, so day-to-day work uses a normal user, here deploy, that raises its rights through sudo. usermod -aG sudo adds deploy to the sudo group. The -a (append) matters: without it, deploy is removed from its other groups.
⚡ Install the key first, then close passwords
Write:
# /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
Guess first: PasswordAuthentication no means logins only work with an SSH key (a key that replaces the password). This line is applied before ssh-copy-id has installed your key on the server. What happens at the next login?
It's refused with Permission denied (publickey). Passwords are closed, and the key isn't there. The root session that is still open is your only way to fix it.
Meaning: These two lines go in /etc/ssh/sshd_config, the SSH server's settings file: PermitRootLogin no closes root logins, PasswordAuthentication no closes password logins. The order: ssh-copy-id [email protected] from the laptop, test a login in a second terminal, then close root and passwords.
⚡ Firewall: allow first, enable last
Write:
root@web-2:~# ufw allow 22/tcp
root@web-2:~# ufw allow 80/tcp && ufw allow 443/tcp
root@web-2:~# ufw enable
Meaning: UFW is Ubuntu's built-in firewall (a program that filters incoming connections). ufw allow opens a port: 22 for SSH, 80 and 443 for the web. Once ufw enable turns it on, UFW refuses every incoming connection except to the allowed ports. If the order is reversed, the next login ends with Connection timed out.
⚡ A guard, a clock and a spare storeroom
Write:
root@web-2:~# apt install -y fail2ban
root@web-2:~# timedatectl set-timezone Europe/London
Meaning: The guard is fail2ban: it blocks IPs that fail to log in again and again. The clock is the time zone: the times in the logs match UK time. The spare storeroom is 1 GB of swap (disk used when RAM is full), so npm ci (installing dependencies) isn't Killed (stopped by Linux) right away.