A public VPS can start receiving automated connection attempts very quickly. You do not need to panic, but you should secure the basic entry points before you put important applications or data on the server.
This guide is written for beginners and uses Ubuntu or Debian commands. The same security ideas apply to most Linux VPS servers, but package names and service commands can be different on other distributions.
1. Update the server first
Start by installing the latest available package updates. A VPS image may have been created before the most recent security fixes were released.
sudo apt update
sudo apt upgrade -yIf the system says a reboot is required, reboot when it is safe to do so.
test -f /var/run/reboot-required && echo "Reboot recommended"If it prints Reboot recommended, save your work and reboot when it is safe.
sudo rebootFor a brand new server, doing this before installing your own applications gives you a cleaner starting point.
2. Create a separate admin user
Avoid using the root account for normal administration. Create a separate user, give that user sudo access, and use it for day to day work.
sudo adduser adminuser
sudo usermod -aG sudo adminuserReplace adminuser with the username you want to use. Then switch to the new account and confirm that sudo works.
su - adminuser
sudo whoamiIf the last command prints root, your sudo access is working. This does not mean you are logged in as root. It means sudo can run an administrative command when you need it.
3. Use an SSH key instead of a password
SSH keys are much stronger against password guessing attacks. Generate the key on your own computer, not on the VPS.
ssh-keygen -t ed25519 -C "your@email.com"Copy the public key to the new admin account and then test a new login.
ssh-copy-id adminuser@SERVER_IP
ssh adminuser@SERVER_IPReplace SERVER_IP with the public IP address of your VPS.
4. Disable direct root login and SSH password login
Once the new admin user can log in with an SSH key, you can reduce two common attack paths. Direct SSH login as root can be disabled, and password authentication can be turned off.
On Ubuntu and Debian, a small configuration file is easier to manage than repeatedly editing the main SSH configuration. Create this file:
sudo nano /etc/ssh/sshd_config.d/00-hardening.confAdd these settings:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yesBefore reloading SSH, check the configuration for syntax errors.
sudo sshd -tIf that command returns no error, reload SSH.
sudo systemctl reload sshChanging the SSH port is optional. It can reduce automated log noise, but it is not a replacement for key authentication, a firewall, and proper access control.
5. Configure UFW with a deny by default policy
A firewall should allow only the services your server actually needs. UFW gives Ubuntu and Debian users a simple way to manage host firewall rules.
sudo apt install ufw -y
sudo ufw allow OpenSSHIf this VPS will host a website, allow HTTP and HTTPS as well.
sudo ufw allow 80/tcp
sudo ufw allow 443/tcpThen set the default policies and enable the firewall.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw status verbose
6. Install Fail2ban for repeated login attempts
Fail2ban watches logs and can temporarily block IP addresses that repeatedly fail authentication. It is useful for reducing repeated SSH login attempts and keeping authentication logs cleaner.
sudo apt install fail2ban -yInstead of editing the main package file, create a small local SSH jail configuration.
sudo nano /etc/fail2ban/jail.d/sshd.localAdd the following example settings:
[sshd]
enabled = true
bantime = 1h
findtime = 10m
maxretry = 5Enable the service and check the SSH jail.
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshdIf you already allow only SSH key authentication, Fail2ban is an extra layer rather than your main protection. Strong SSH authentication remains more important.
7. Enable automatic security updates
Security updates are easy to forget when a server is quietly doing its job. Ubuntu uses unattended-upgrades to install configured updates automatically, and many Ubuntu images already include it.
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgradesYou can check the automatic update settings here:
cat /etc/apt/apt.conf.d/20auto-upgradesAutomatic updates reduce the time your server stays exposed to known software vulnerabilities. You should still review the server regularly and plan controlled reboots when required.
8. Check which services are listening
A firewall is important, but it is also useful to know which programs are listening for network connections. This helps you find services that were installed by mistake or exposed more widely than expected.
sudo ss -tulpnAs a simple rule, an address such as 127.0.0.1 is local to the server. An address such as 0.0.0.0 or [::] means the service is listening on network interfaces and may be reachable if the firewall allows it.
Do not close a port just because you do not recognise it. First identify the service and confirm whether your application needs it.
9. Use your provider firewall if one is available
Many VPS providers offer a network firewall or security group outside the operating system. If your provider has one, use it as an additional layer.
A simple setup is to allow the same inbound ports you intentionally allowed in UFW, such as SSH, HTTP, and HTTPS, then deny other unsolicited inbound traffic.
This provider level firewall does not replace UFW. The two layers protect different parts of the path. If your provider does not offer this feature, your VPS can still be secured properly with a well configured host firewall and strong SSH access.
Optional: add two factor authentication to SSH
For servers that need stronger access controls, you can require a time based one time password in addition to the SSH key. Ubuntu documents this with the Google Authenticator PAM module.
This is more advanced than the steps above. A PAM or SSH mistake can lock you out, so make sure key authentication already works and that you have provider console access before enabling it.
Ubuntu’s official guide explains the full setup: Two factor authentication with TOTP or HOTP.
Final checks before you deploy applications
Run a few quick checks before you consider the basic setup finished.
sudo ufw status verbose
sudo ss -tulpn
sudo fail2ban-client status sshd
sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication|pubkeyauthentication'SSH key login works from a new terminal.
Root SSH login is disabled.
Password authentication is disabled if you do not need it.
Only required ports are open.
Automatic security updates are enabled.
You know why every public service is listening.
Do not forget backups
Security reduces risk, but it cannot protect you from every failure. Hardware problems, accidental deletion, a bad deployment, or a compromised application can still cause data loss. Keep tested backups outside the VPS and make sure you know how to restore them.
Official references
A secure VPS does not need to be complicated. Start with a small attack surface, strong SSH access, regular updates, and a firewall that allows only what you actually use. Add more controls when the server’s role and risk justify them.
