When setting up a Linux VPS, security is a top priority. By default, a newly installed server can be vulnerable to unauthorized access, brute-force attacks, and malware. This guide will walk you through essential steps to secure your Linux VPS.
📌 Prerequisites
-
A Linux VPS (Ubuntu, Debian, CentOS, or AlmaLinux) from VolticHost
-
Root or sudo user access
1️⃣ Update Your System
Before making any changes, update your server to get the latest security patches.
For Ubuntu/Debian, run:
sudo apt update && sudo apt upgrade -y
For CentOS/AlmaLinux/RockyLinux, run:
sudo yum update -y
✅ Your system is now up to date!
2️⃣ Change the Default SSH Port
By default, SSH runs on port 22, making it a common target for attacks. Changing the port helps reduce unauthorized login attempts.
-
Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
-
Find the line:
#Port 22
-
Change it to a custom port (e.g.,
2222
):Port 2222
-
Save and exit (
CTRL + X
, thenY
, thenEnter
). -
Restart SSH:
sudo systemctl restart sshd
✅ SSH is now running on a custom port!
3️⃣ Disable Root Login via SSH
Using the root user directly over SSH is a security risk. It’s better to use a non-root user with sudo
privileges.
-
Open the SSH configuration file:
sudo nano /etc/ssh/sshd_config
-
Find the line:
PermitRootLogin yes
-
Change it to:
PermitRootLogin no
-
Save and exit.
-
Restart SSH:
sudo systemctl restart sshd
✅ Root login is now disabled!
4️⃣ Create a New Sudo User
Instead of using root
, create a new user with administrative privileges.
-
Create a new user (replace
youruser
with your username):sudo adduser youruser
-
Give the user sudo privileges:
sudo usermod -aG sudo youruser
-
Switch to the new user:
su - youruser
✅ You now have a secure sudo user!
5️⃣ Set Up a Firewall (UFW or Firewalld)
For Ubuntu/Debian (UFW - Uncomplicated Firewall)
-
Install UFW (if not installed):
sudo apt install ufw -y
-
Allow your custom SSH port:
sudo ufw allow 2222/tcp
-
Enable the firewall:
sudo ufw enable
For CentOS/AlmaLinux (Firewalld)
-
Start and enable Firewalld:
sudo systemctl start firewalld sudo systemctl enable firewalld
-
Allow your custom SSH port:
sudo firewall-cmd --permanent --add-port=2222/tcp sudo firewall-cmd --reload
✅ Your VPS firewall is now active!
6️⃣ Install Fail2Ban to Prevent Brute-Force Attacks
Fail2Ban monitors login attempts and bans IPs that fail too many times.
For Ubuntu/Debian:
sudo apt install fail2ban -y
For CentOS/AlmaLinux:
sudo yum install fail2ban -y
Start and enable Fail2Ban:
sudo systemctl enable fail2ban --now
✅ Your VPS is now protected from brute-force attacks!
7️⃣ Enable Automatic Security Updates
Keeping your server updated ensures protection against vulnerabilities.
For Ubuntu/Debian:
-
Install the unattended-upgrades package:
sudo apt install unattended-upgrades -y
-
Enable automatic updates:
sudo dpkg-reconfigure unattended-upgrades
For CentOS/AlmaLinux:
-
Install
dnf-automatic
:sudo yum install dnf-automatic -y
-
Enable automatic updates:
sudo systemctl enable --now dnf-automatic.timer
✅ Your system will now update itself automatically!
🎉 Conclusion
You've successfully secured your Linux VPS by: ✅ Updating the system
✅ Changing the SSH port
✅ Disabling root login
✅ Creating a sudo user
✅ Setting up a firewall
✅ Installing Fail2Ban
✅ Enabling automatic updates
For more assistance, contact VolticHost Support. 🚀