...

XITENS INSIGHTS

How to Secure a Linux VPS: 9 Steps to a Secure Server

Secure your Linux VPS in 9 steps: updates, SSH keys, disabling root login, firewall, Fail2ban and backups. With examples for Ubuntu, Debian and AlmaLinux.

Securing a Linux VPS: firewall, Fail2ban and SSH settingsKNOWLEDGE TO MOVE YOU FORWARD

You secure a Linux VPS by keeping the server up to date, logging in with SSH keys instead of passwords, disabling root login, setting up a firewall and automatically blocking attack attempts. With that foundation your server becomes far less vulnerable to the automated attacks every server on the internet faces. In this guide you read what to do in each step and why.

The examples are for Ubuntu and Debian. If you use AlmaLinux, we mention the alternative in each step.

Why should you secure a Linux VPS?

With a VPS you have full root access, and with it the responsibility for security. As soon as a server is online, bots try to log in via SSH with common passwords within minutes. A well-secured server keeps those attempts out and protects your data, your customers and your reputation.

Step 1: keep your server up to date

Many attacks exploit known vulnerabilities for which an update has long been available. That is why you install all updates right after delivery:

apt update && apt upgrade -y

Then let security updates install automatically:

apt install unattended-upgrades -y
dpkg-reconfigure -plow unattended-upgrades

On AlmaLinux you use dnf upgrade -y and the dnf-automatic package for automatic updates.

Step 2: create your own user

Do not work as root every day. Create your own user and give it administrative rights via sudo:

adduser beheerder
usermod -aG sudo beheerder

On AlmaLinux the admin group is called wheel: usermod -aG wheel beheerder.

Step 3: log in with an SSH key

An SSH key is much more secure than a password, because it cannot be guessed. Create a key pair on your own computer and copy the public key to your server:

ssh-keygen -t ed25519
ssh-copy-id beheerder@JOUW-IP

Then test whether you can log in without a password using ssh beheerder@JOUW-IP.

Step 4: disable root login and passwords

Once logging in with your key works, disable logging in as root and with passwords. To do so, adjust these lines in the SSH configuration file:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes

Then restart SSH:

systemctl restart ssh

On AlmaLinux the service is called sshd. Only close your current session after checking in a second window that you can still log in.

Step 5: set up a firewall

A firewall only lets through the traffic you really need. With UFW you set that up with a few instructions:

ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow 80,443/tcp
ufw enable

If you do not run a web server, leave out ports 80 and 443. On AlmaLinux you use firewalld with firewall-cmd.

Step 6: block attacks with Fail2ban

Fail2ban monitors the log files and blocks IP addresses that repeatedly try to log in:

apt install fail2ban -y

Create your own configuration, so updates do not overwrite your settings:

[sshd]
enabled = true
maxretry = 5
bantime = 1h

Activate the settings with systemctl restart fail2ban.

Step 7: only run what you need

Every service listening on a port is a potential entry point. Check which services are reachable and disable what you do not use:

ss -tulpn

Step 8: make backups and snapshots

Security is never one hundred percent. That is why you regularly back up your data, preferably to another location, and take a snapshot before making major changes. An NVMe VPS from Xitens includes one free snapshot. Note: high availability protects against hardware failures, but does not replace a backup.

Step 9: secure your websites with SSL

If you run websites on your VPS, always encrypt the traffic with HTTPS. You can do that for free with Let’s Encrypt, or with a Sectigo SSL certificate if you want to show extra validation of your organisation.

Locked out? This is how you always reach your server

If something goes wrong with SSH or the firewall, at Xitens you can always reach your server via the noVNC console in the client area. It lets you work directly on the console, even when network access no longer works, so you can easily undo a wrong setting.

A secure Linux VPS at Xitens

Our Linux VPS runs on enterprise Ceph storage with high availability, in our own racks with our own network in a TIER III data centre in Amsterdam. You choose from AlmaLinux, Debian and Ubuntu, get full root access, and our support engineers are available 24/7 by ticket and email. Not sure which VPS suits you? See our guide Compare VPS Hosting.

Frequently asked questions about Linux VPS security

Should I change the SSH port?

A different SSH port reduces the number of automated login attempts in your log files, but it is not real security. Logging in with SSH keys, disabled passwords and Fail2ban are far more important.

Do I need a firewall if I already use Fail2ban?

Yes. Fail2ban blocks attackers after the fact, while a firewall ensures only the ports you really need are open. They complement each other.

How often should I update my server?

Security updates are best installed automatically and immediately. Check larger updates at least monthly, preferably after taking a snapshot.

What do I do if I have locked myself out?

Log in via the noVNC console in the client area and restore the setting, for example in the SSH configuration file or the firewall. If you cannot work it out, our support engineers will help you via a ticket.

KEEP EXPLORING

More knowledge. More possibilities.

All articles ↗