India English
Kenya English
United Kingdom English
South Africa English
Nigeria English
United States English
United States Español
Indonesia English
Bangladesh English
Egypt العربية
Tanzania English
Ethiopia English
Uganda English
Congo - Kinshasa English
Ghana English
Côte d’Ivoire English
Zambia English
Cameroon English
Rwanda English
Germany Deutsch
France Français
Spain Català
Spain Español
Italy Italiano
Russia Русский
Japan English
Brazil Português
Brazil Português
Mexico Español
Philippines English
Pakistan English
Türkiye Türkçe
Vietnam English
Thailand English
South Korea English
Australia English
China 中文
Canada English
Canada Français
Somalia English
Netherlands Nederlands

How to Secure a VPS

To secure a VPS, you need to update its software, restrict SSH access, configure a firewall, monitor login attempts, and maintain reliable backups. These measures help protect your server, applications, and stored data from unauthorized access and security threats.

If you’re using a VPS in Uganda, these security tasks are your responsibility unless you choose managed hosting.

Unlike shared hosting, where your hosting provider handles much of the server maintenance, a VPS gives you greater control but also requires you to manage its security.

Automated scanners can start probing newly exposed services within minutes of a server going online. Therefore, it’s best to apply basic security measures as soon as your VPS is deployed.

These are six steps on how to secure a VPS and test your configurations.

Security Changes Preparation

Before changing your server’s security settings, take a few precautions. Incorrect SSH configurations or firewall rules can block your access, leaving you unable to manage your VPS remotely.

How to Secure a VPS: Prep

Here are three things to do before making changes:

  1. Keep an existing SSH session open while working. Test each change through a second SSH session before closing the original one.
  2. Back up configuration files before editing them so you can restore their previous settings if something goes wrong.
  3. Confirm that you can access your hosting provider’s control panel, console, or recovery option in case SSH access fails.

The commands in this guide use Ubuntu and Debian. If your VPS runs AlmaLinux or Rocky Linux, some commands and service names may differ.

Steps to Secure a VPS

1) Update the System and Automate Security Patches

Start by updating your VPS operating system and installed software. Outdated packages can contain known security vulnerabilities that attackers may use to gain access to your server.

On Ubuntu or Debian, run:

sudo apt update && sudo apt upgrade

This refreshes the package lists and installs available updates.

You should also configure automatic security updates so your server receives important patches without requiring you to run update commands manually each time.

On Ubuntu, you can use unattended-upgrades to automate this process. Test the configuration with:

sudo unattended-upgrades --dry-run --debug

For AlmaLinux and Rocky Linux, install dnf-automatic, configure it to apply security updates, and enable its systemd timer.

Remember to check if updates require a server restart, particularly after kernel updates.

2) Create a Non-Root User and Configure SSH Keys

The root account has unrestricted administrative privileges. Using it for everyday server management increases the risk of accidental system changes and unauthorized access.

A safer approach is to create a separate user account with administrative privileges.

How to Secure a VPS: New User

On Ubuntu or Debian, run:

sudo adduser yourname
sudo usermod -aG sudo yourname

Replace yourname with your preferred username. The commands create the account and add it to the sudo group, allowing you to perform administrative tasks when necessary.

On AlmaLinux and Rocky Linux, use the wheel group instead.

Next, configure SSH key authentication. SSH keys provide a more secure way to access your server than relying on passwords alone.

On your local computer, generate an SSH key:

ssh-keygen -t ed25519

Then copy the public key to your VPS:

ssh-copy-id yourname@your-server-ip

Replace the username and IP address with your server details.

Before proceeding, open another terminal and try logging in using the new account and SSH key. Confirm that you can access the server successfully.

3) Secure SSH Access

Once your new user account and SSH keys are working, restrict SSH access to reduce unauthorized login attempts.

Start by editing the SSH configuration file:

sudo nano /etc/ssh/sshd_config

Locate the relevant settings and configure them as follows:

PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AllowUsers yourname

These settings:

  • Disable direct SSH login through the root account.
  • Prevent users from authenticating through SSH passwords.
  • Disable keyboard-interactive authentication, which can provide another password-based login route.
  • Restrict SSH access to the username specified under AllowUsers.

Before applying the changes, validate the configuration:

sudo sshd -t

If the command returns no errors, check the effective SSH settings:

sudo sshd -T

SSH configuration files can contain earlier directives and included configuration files, so a setting you add may not always take effect as expected.

After confirming the settings, reload the SSH service:

sudo systemctl reload ssh

On AlmaLinux and Rocky Linux, the service is usually called sshd.

Open a second terminal and attempt to log in using your new account. Confirm that SSH access works before closing your original session.

4) Configure a Default-Deny Firewall

A firewall controls incoming and outgoing network connections to your VPS. It helps prevent unauthorized access by restricting connections to services that need to be publicly accessible.

On Ubuntu, you can use Uncomplicated Firewall (UFW) to manage these rules.

How to Secure a VPS: UFW

First, allow SSH connections:

sudo ufw allow OpenSSH

Next, allow HTTP and HTTPS traffic if your VPS hosts websites:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Configure the default firewall policies:

sudo ufw default deny incoming
sudo ufw default allow outgoing

Enable the firewall:

sudo ufw enable

Finally, check its status:

sudo ufw status verbose

Allowing SSH before enabling UFW is important because blocking your SSH port could disconnect you from the server.

If you’re using AlmaLinux or Rocky Linux, you can configure similar firewall rules using firewalld.

After enabling the firewall, test your publicly exposed ports from another computer or network to confirm that only the intended services are accessible.

If your VPS runs Docker, check its published container ports separately. Docker can create network rules that bypass some UFW restrictions. Where possible, bind private services such as databases to localhost instead of exposing them directly to the internet.

5) Install Fail2ban

Fail2ban adds another layer of protection by monitoring authentication logs and temporarily blocking IP addresses responsible for repeated failed login attempts.

For example, if someone repeatedly tries to access your VPS through SSH using incorrect credentials, Fail2ban can identify the activity and block the source IP address.

On Ubuntu or Debian, install it using:

sudo apt install fail2ban

After installation, check if the SSH protection jail is active:

sudo fail2ban-client status sshd

The output should display information about the SSH jail, including its current status and any banned IP addresses.

Fail2ban helps reduce repeated login attempts, but it should not replace SSH keys, restricted user access, or proper authentication settings.

6) Set up backups and test recovery

Even with the right security configurations, your VPS can still experience data loss, accidental file deletion, software failures, or security breaches.

Regular backups give you a way to recover your applications and important data if something goes wrong.

How to Secure a VPS: Backup

Here are four practices to follow:

  1. Back up important configuration files before making changes so you can restore previous settings when necessary.
  2. Store at least one copy of your important data outside the VPS to protect it if the server becomes inaccessible.
  3. Schedule regular backups of your website files, databases, and other important server data.
  4. Perform a test restore to confirm that your backups contain usable data and can be recovered successfully.

Truehost VPS hosting offers backup add-ons, including JetBackup 5 and Cloudoon Vault, which provide additional options for protecting your server data.

Secure your website with Truehost VPS hosting

Securing a VPS goes beyond installing a firewall. Regular updates, restricted access, monitored logins, and tested backups all play a part in keeping your applications and customer data safe.

If you’re comfortable managing these tasks yourself, our unmanaged VPS plans give you full root access and complete control over your server environment.

If you’d rather leave server setup, updates, security, and monitoring to our team, managed VPS hosting takes that off your plate.

Visit our VPS hosting page to compare the available plans and choose the option that fits your technical requirements.

Author

Leave a comment

Your email address will not be published. Required fields are marked *