Skip to content
VPS

How to secure a new Linux VPS: the first steps

Published on 11 November 2026

You secure a new Linux VPS in about half an hour. Log in as root, install the updates, create your own user with sudo, log in with an SSH key from then on and turn off password logins. Then switch on the ufw firewall, install fail2ban and let security updates install themselves. The steps work on Ubuntu 24.04 LTS and on Debian 12 and 13; where they differ, it says so. Checked on 11 November 2026.

Securing a new VPS at a glance
StepHow
Log inssh root@203.0.113.10, with the IP address from the email
Updatesapt update and apt upgrade
Your own useradduser sam and usermod -aG sudo sam
SSH keyssh-keygen -t ed25519 on your own computer
No password loginPasswordAuthentication no and PermitRootLogin no
Firewallufw allow 22/tcp, then ufw enable
Block login attemptsapt install fail2ban
Automatic updatesunattended-upgrades, already on in Ubuntu

No VPS yet? At CakeHosting yours is ready within a few minutes, from €4.20 a month, VAT included.

Before you start

  • Your login details. After you order, you receive the IP address, username and password of your VPS by email. The examples use 203.0.113.10 as the IP address and sam as the new user. Fill in your own.
  • A terminal. On macOS and Linux, open Terminal. On Windows 10 and 11, ssh works in PowerShell or Windows Terminal. If it is missing, add OpenSSH Client under Optional features in Settings.
  • A safety net. If you lock yourself out, the console still gets you in. You find it in My CakeHosting: open your VPS service page and click Open VPS panel.

Step 1: log in as root

Open your terminal and log in with the IP address from the email:

ssh root@203.0.113.10

The first time, SSH asks whether you trust the server. Type yes, then the password from the email. Nothing appears while you type; that is normal. Change the root password straight away, because the old one was sent by email:

passwd

Step 2: install updates

apt update
apt upgrade

Confirm with y. Restart your VPS after this first round of updates with reboot, so it runs the newest kernel straight away. Log in again after a minute.

Step 3: your own user with sudo

Do not work as root all day: one typo can break everything. Create your own user and give it sudo rights. Debian does not always include the sudo program, so install it there first:

apt install sudo
adduser sam
usermod -aG sudo sam

adduser asks for a password for sam. Pick a strong one, because you need it for sudo and for the console. Skip the questions about name and phone number with Enter. Then test it:

su - sam
sudo whoami

If you see root, sudo works. Type exit twice to log out.

Step 4: log in with an SSH key

An SSH key is far safer than a password, because nobody can guess it. You create the key on your own computer, not on the VPS. Open a terminal on your computer and type:

ssh-keygen -t ed25519

Press Enter to keep the key in the default place and choose a passphrase. It protects your key if someone gets hold of your computer. Then put the public part of the key on your VPS. On macOS and Linux that is one command:

ssh-copy-id sam@203.0.113.10

Windows does not have ssh-copy-id. Show your public key in PowerShell instead:

cat ~/.ssh/id_ed25519.pub

Copy the whole line, which starts with ssh-ed25519. Log in to your VPS with ssh sam@203.0.113.10 and open the file for your keys:

mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys

Paste the line, save with Ctrl+O and Enter, and exit with Ctrl+X. Then set the right permissions:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Test it in a new terminal window with ssh sam@203.0.113.10. If SSH asks for the passphrase of your key, or logs you straight in, and does not ask for the password of sam, it works.

Step 5: turn off password logins

Once your key works, turn off logging in with a password, and logging in as root too. Keep your current terminal open until you are sure everything works. As sam, create a settings file of your own for SSH:

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

Put these three lines in it:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no

Why a separate file with 00 in front? Ubuntu and Debian read the files in sshd_config.d before the main file, in alphabetical order, and SSH uses the first value it finds for each setting. If another file allows passwords, yours now comes first. Check the settings and restart SSH:

sudo sshd -t
sudo systemctl restart ssh

If sshd -t prints nothing, all is well. With sudo sshd -T | grep -i -E "passwordauthentication|permitrootlogin" you see the values SSH really uses: both should say no.

Now open a new terminal window and log in as sam. If that works, you can close the old window. If it does not, undo the change in the window that is still open. Logging in as root over SSH no longer works from now on; sudo gives you the same rights.

Step 6: switch on the ufw firewall

A firewall only lets in the traffic you allow. Ubuntu uses ufw as its default firewall tool; on Debian you install it first. Always allow SSH first, or you lock yourself out:

sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw enable

Confirm with y. sudo ufw status verbose shows what is open. If you run a website later, also open ports 80 and 443 with sudo ufw allow 80/tcp and sudo ufw allow 443/tcp.

Using Docker? According to Docker, ports you publish with Docker bypass ufw. Publish internal services on 127.0.0.1 only, for example with -p 127.0.0.1:8080:80, and put a web server such as Nginx in front.

Step 7: fail2ban against login attempts

On every server connected to the internet, bots try to log in all day long. With SSH keys only they will not get in, but fail2ban blocks addresses that try too often. That keeps your logs quiet.

sudo apt install fail2ban

On Ubuntu 24.04 and Debian 13, fail2ban watches SSH right away. On Debian 12 you set one more thing. Debian 12 no longer installs rsyslog by default and keeps its logs in the systemd journal, so the log file fail2ban looks for does not exist there. Create the file /etc/fail2ban/jail.local with sudo nano and put in it:

[sshd]
backend = systemd

Restart fail2ban and check that it works:

sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

If you see an overview with Currently banned, the jail is running. If another system shows an error about a missing log file, the same lines in jail.local help.

Step 8: install security updates automatically

On Ubuntu this is already on: the unattended-upgrades package installs security updates every day. On Debian you switch it on yourself:

sudo apt install unattended-upgrades
sudo dpkg-reconfigure unattended-upgrades

Choose Yes when it asks whether to download and install stable updates automatically. By default this only covers security updates, and your VPS does not restart by itself for them. So restart it yourself now and then, at a quiet moment, with sudo reboot.

Starting with a VPS of your own? At CakeHosting you choose Ubuntu 24.04 LTS, Debian 12 or 13 and more at checkout, and your VPS is online within a few minutes. From €4.20 a month, VAT included. See the VPS plans.

What next?

  • See what is listening. sudo ss -tlnp shows which programs listen on a port.
  • Set the time zone, for example with sudo timedatectl set-timezone Europe/Amsterdam, or your own zone, so the times in your logs make sense.
  • Back up what matters, also to a place outside your VPS.
  • Know your options. Not sure yet which VPS fits? Read VPS in the Netherlands: what you get.

What goes wrong most often?

  • Permission denied (publickey). SSH cannot find your key, or the permissions on ~/.ssh are wrong. Check that you log in as the right user and that authorized_keys holds the whole line.
  • Locked out after ufw enable. Port 22 was not allowed first. Log in through the console in the VPS panel and type sudo ufw allow 22/tcp.
  • SSH still asks for a password. Another file takes precedence, or SSH was not restarted. sudo sshd -T shows the value SSH really uses.
  • fail2ban does not start on Debian 12. The log file is missing. Put backend = systemd in jail.local, as above.
  • Completely stuck? Reinstall from the VPS panel to start with a clean VPS. Everything that was on it will be gone, though.

Frequently asked questions

What should I do first on a new VPS?

Install updates, create your own user with sudo, log in with an SSH key and turn off password and root logins. Then switch on the ufw firewall with port 22 open, install fail2ban and turn on automatic security updates.

Should I change the SSH port?

You don't have to. With key-only logins and fail2ban, port 22 is fine. Another port makes your logs quieter, but you then have to open it in ufw and remember it everywhere.

What if I lock myself out of my VPS?

Use the console in the VPS panel, which you open with the Open VPS panel button in My CakeHosting. The console works even when SSH or the firewall is closed, so you can fix the setting there, for example with sudo ufw allow 22/tcp.

Do I still need fail2ban with SSH keys?

It is not strictly needed: without password logins nobody gets in by guessing. Fail2ban does block addresses that keep trying, which keeps your logs quiet.

Do these steps work on Rocky Linux or CentOS Stream?

The idea is the same, but the commands differ. There you install packages with dnf instead of apt, and firewalld is the default firewall instead of ufw.

More questions? See all frequently asked questions

Related articles