How to install and secure a VPS server step by step
A new VPS server is empty, and within minutes of starting it bots begin scanning it. Scanners look across the whole internet for open SSH ports and weak passwords, so the first hours after installation are worth spending on security. Below is the order we recommend. The examples are for Ubuntu 24.04 LTS and work in a similar way on Debian and Rocky Linux.
1. First login
After you order a VPS, the control panel gives you the IP address and the password for the root user. Connect to the server from a terminal (Linux, macOS) or with PuTTY (Windows):
ssh root@SERVER_IP
The first time, accept the server's key fingerprint. Change the root password straight away with passwd, and don't log in as root for everyday work.
2. Update the system
The system image may be weeks old. Update the package list and the installed software:
apt update && apt upgrade -y
If the update installs a new kernel, a restart helps: reboot.
3. A separate user with sudo rights
Create an account for everyday work and give it the rights to run administrative commands:
adduser deploy
usermod -aG sudo deploy
Before you close your current session, check that the new account works. In a second window, log in as deploy and run sudo whoami. It should print root.
4. SSH key login instead of a password
An SSH key is much harder to crack than a password. On your own computer, generate a key pair (if you don't have one yet):
ssh-keygen -t ed25519 -C "you@email"
Then copy the public key to the server:
ssh-copy-id deploy@SERVER_IP
Once key login works, turn off password login and remote root login. Edit /etc/ssh/sshd_config:
PasswordAuthentication no
PermitRootLogin no
Then check the syntax and reload the service:
sudo sshd -t && sudo systemctl reload ssh
Before you close your session, check in a second window that key login works. If you lock yourself out, the console in the VPS panel is your way back in.
5. UFW firewall
UFW is a simple firewall for Ubuntu. First block all incoming traffic, then open only what you need. The order matters: add the SSH rule before you enable the firewall, or you will cut yourself off.
Before you change the firewall rules, take a snapshot of the server or a copy of its configuration. If you block yourself, you can return to the state from before the change in a few minutes.
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
Open ports 80 and 443 only if a website or application on the server uses them.
6. Limit SSH to your IP address
Port 22 should not be open to the whole internet. Bots scan it non-stop, and every attack on a password or an SSH bug starts there. If you connect from one fixed IP address, leave only that address on port 22. Replace YOUR_IP with yours (you can check it by searching for "what is my IP"):
sudo ufw delete allow OpenSSH
sudo ufw allow from YOUR_IP to any port 22 proto tcp
You can set the same rule in the firewall section of the VPS panel. Then unwanted traffic is stopped before it reaches the server.
If you don't have a fixed IP (for example, you connect from home or from different networks), you have two options: a VPN that you allow on port 22, or a rule that you open while you work and close afterwards. When you close it, make sure you don't cut yourself off in the middle of a session.
7. fail2ban: blocking after failed logins
Even with limited access, it's worth adding fail2ban. It temporarily blocks addresses after several failed attempts:
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd
8. Automatic security updates
Security fixes shouldn't wait until you remember them. Ubuntu has a ready-made package for this:
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
9. Backups
A firewall and fail2ban don't replace a backup. Decide what it should contain (databases, application files, configuration) and once a month check that you can restore the server from it. Keep the backup outside the server it protects.
Before every change on the server (firewall, SSH, a system update, a new application), take a full copy of the machine or a snapshot. A snapshot returns the whole server to its state before the change. If snapshots are not available, make at least a copy of the configuration:
sudo tar czf ~/etc-backup-$(date +%F).tgz /etc
10. Quick checklist
- Root password changed and root login over SSH turned off.
- SSH key login works, password login doesn't.
- Firewall enabled, only the ports you need are open.
- Port 22 limited to a trusted IP address.
- fail2ban and automatic updates are running.
- A backup exists and has been tested.
- Before every change you have a snapshot or a copy of the machine.
Need help with the configuration, or would you like us to do it for you? Write to us and we will check your server.
The Chmura.tech team