How to Secure Vps Server

How to Secure VPS Server A Virtual Private Server (VPS) offers the power and flexibility of a dedicated server at a fraction of the cost. However, with greater control comes greater responsibility. An unsecured VPS is an open door for cybercriminals—used to launch attacks, mine cryptocurrency, host malware, or steal sensitive data. According to recent cybersecurity reports, over 60% of compromised

Nov 10, 2025 - 11:26
Nov 10, 2025 - 11:26
 0

How to Secure VPS Server

A Virtual Private Server (VPS) offers the power and flexibility of a dedicated server at a fraction of the cost. However, with greater control comes greater responsibility. An unsecured VPS is an open door for cybercriminalsused to launch attacks, mine cryptocurrency, host malware, or steal sensitive data. According to recent cybersecurity reports, over 60% of compromised servers globally are VPS instances with misconfigurations or outdated software. Securing your VPS isnt optional; its a fundamental requirement for any website, application, or service you intend to run reliably and safely.

This comprehensive guide walks you through every critical step to harden your VPS from the moment you receive your login credentials. Whether you're hosting a personal blog, an e-commerce store, or a business application, following these protocols will drastically reduce your attack surface, protect your data, and ensure compliance with industry security standards. By the end of this tutorial, youll have a fully fortified VPS that resists common threats and operates with confidence.

Step-by-Step Guide

1. Change the Default Root Password and Create a New User

When you first provision a VPS, most providers assign a default root password, often generated automatically and sent via email. This password is frequently weak, publicly known in templates, or exposed in provider logs. The first rule of security: never trust defaults.

Immediately after logging in as root via SSH, change the root password using the passwd command:

passwd

Choose a strong, unique passwordminimum 16 characters, including uppercase, lowercase, numbers, and symbols. Avoid dictionary words or personal information.

Next, create a new non-root user with sudo privileges. This limits the risk of accidental or malicious damage to the system:

adduser username

usermod -aG sudo username

Replace username with your desired username. The usermod -aG sudo command adds the user to the sudo group, granting administrative privileges when needed. Log out of root and log back in as your new user:

exit

ssh username@your-server-ip

This small change dramatically reduces the risk of brute-force attacks targeting the root account.

2. Disable Root SSH Login

Even with a strong password, allowing direct root login via SSH is a major vulnerability. Attackers constantly scan the internet for open SSH ports and attempt to brute-force root access. Disabling root SSH login forces attackers to guess both a valid username and password, significantly increasing the difficulty of compromise.

Open the SSH configuration file:

sudo nano /etc/ssh/sshd_config

Find the line:

PermitRootLogin yes

Change it to:

PermitRootLogin no

If the line is commented out (starts with

), remove the # and make the change. Save and exit (Ctrl+O, Enter, Ctrl+X).

Restart the SSH service to apply changes:

sudo systemctl restart ssh

Before closing your current session, open a new terminal and test logging in as your new user. If you cannot log in, you risk locking yourself out. Always test before disconnecting from the primary session.

3. Configure SSH Key Authentication

Password-based SSH authentication is vulnerable to brute-force attacks, even with strong passwords. SSH key authentication is cryptographic, far more secure, and immune to brute-force attempts.

On your local machine (Mac, Linux, or Windows with WSL or Git Bash), generate an SSH key pair:

ssh-keygen -t ed25519 -C "your_email@example.com"

If your system doesnt support Ed25519, use RSA with a 4096-bit key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Press Enter to accept the default location. Set a passphrase for added security (recommended).

Copy your public key to the VPS:

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

If ssh-copy-id is unavailable, manually append the public key:

cat ~/.ssh/id_ed25519.pub | ssh username@your-server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Set proper permissions on the server:

chmod 700 ~/.ssh

chmod 600 ~/.ssh/authorized_keys

Now, disable password authentication entirely in the SSH config file:

sudo nano /etc/ssh/sshd_config

Find and modify:

PasswordAuthentication yes

To:

PasswordAuthentication no

Restart SSH again:

sudo systemctl restart ssh

Test logging in from a new terminal using your key. If successful, youve eliminated one of the most common attack vectors.

4. Change the Default SSH Port

While not a substitute for key authentication, changing the default SSH port (22) reduces automated bot traffic. Most scanners target port 22 exclusively. Moving SSH to a non-standard port (e.g., 2222, 54321) filters out the majority of script-based attacks.

Back in /etc/ssh/sshd_config, find:

Port 22

Change it to:

Port 2222

Save and restart SSH:

sudo systemctl restart ssh

Now, when connecting, specify the port:

ssh -p 2222 username@your-server-ip

Important: Before closing your current session, ensure your firewall allows the new port (see next step). Otherwise, you may lose access.

5. Configure a Firewall (UFW or Firewalld)

A firewall acts as a gatekeeper, allowing only necessary traffic and blocking everything else. Most VPS providers offer cloud firewalls, but configuring one at the OS level adds a critical layer of defense.

For Ubuntu/Debian, use UFW (Uncomplicated Firewall):

sudo apt update

sudo apt install ufw

Allow SSH on your custom port:

sudo ufw allow 2222/tcp

Allow HTTP and HTTPS if youre running a web server:

sudo ufw allow 80/tcp

sudo ufw allow 443/tcp

Enable the firewall:

sudo ufw enable

Check status:

sudo ufw status

You should see:

Status: active

To Action From

-- ------ ----

2222/tcp ALLOW Anywhere

80/tcp ALLOW Anywhere

443/tcp ALLOW Anywhere

2222/tcp (v6) ALLOW Anywhere (v6)

80/tcp (v6) ALLOW Anywhere (v6)

443/tcp (v6) ALLOW Anywhere (v6)

For CentOS/RHEL/Fedora, use firewalld:

sudo systemctl enable firewalld

sudo systemctl start firewalld

sudo firewall-cmd --permanent --add-port=2222/tcp

sudo firewall-cmd --permanent --add-service=http

sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --reload

Always test connectivity before closing sessions. A misconfigured firewall can lock you out permanently.

6. Install and Configure Fail2Ban

Fail2Ban monitors log files for repeated failed login attempts and automatically blocks the offending IP addresses. Its an essential tool to combat brute-force attacks.

Install Fail2Ban:

sudo apt install fail2ban

Copy the default configuration:

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Edit the local config:

sudo nano /etc/fail2ban/jail.local

Ensure the SSH section is enabled:

[sshd]

enabled = true

port = 2222

filter = sshd

logpath = /var/log/auth.log

maxretry = 3

bantime = 86400

findtime = 600

Adjust port to match your custom SSH port. maxretry = 3 means three failed attempts trigger a ban. bantime = 86400 bans for 24 hours. findtime = 600 means attempts within 10 minutes count toward the limit.

Restart Fail2Ban:

sudo systemctl restart fail2ban

sudo systemctl enable fail2ban

Check status:

sudo fail2ban-client status sshd

Youll see active bans and the number of IPs blocked. This tool is highly effective against automated attacks.

7. Keep Your System Updated

Outdated software is the

1 cause of server breaches. Vulnerabilities in old versions of Apache, PHP, OpenSSL, or the Linux kernel are well-documented and exploited daily.

Regularly update your system:

sudo apt update && sudo apt upgrade -y

For CentOS/RHEL:

sudo yum update -y

Or on newer versions:

sudo dnf update -y

Automate updates to reduce human error. On Ubuntu, install unattended-upgrades:

sudo apt install unattended-upgrades

sudo dpkg-reconfigure -plow unattended-upgrades

During setup, select Yes to enable automatic updates. You can also configure it to auto-reboot after kernel updates:

sudo nano /etc/apt/apt.conf.d/20auto-upgrades

Add:

APT::Periodic::Update-Package-Lists "1";

APT::Periodic::Unattended-Upgrade "1";

APT::Periodic::AutocleanInterval "7";

APT::Periodic::Download-Upgradeable-Packages "1";

APT::Periodic::Unattended-Upgrade-Allowed-Patterns {

"nginx";

"php";

"mysql-server";

};

And in /etc/apt/apt.conf.d/50unattended-upgrades, ensure:

Unattended-Upgrade::Allowed-Origins {

"${distro_id}:${distro_codename}";

"${distro_id}:${distro_codename}-security";

"${distro_id}ESMApps:${distro_codename}";

"${distro_id}ESMInfra:${distro_codename}";

};

Enable automatic reboots:

Unattended-Upgrade::Automatic-Reboot "true";

Unattended-Upgrade::Automatic-Reboot-Time "02:00";

Rebooting during low-traffic hours ensures patches are applied without manual intervention.

8. Secure Your Web Server (Apache/Nginx)

If your VPS hosts a website, securing the web server is critical. Start by disabling server version headers to prevent attackers from identifying software versions.

For Nginx, edit:

sudo nano /etc/nginx/nginx.conf

Add inside the http block:

server_tokens off;

For Apache, edit:

sudo nano /etc/apache2/conf-available/security.conf

Set:

ServerTokens Prod

ServerSignature Off

Restart the respective service:

sudo systemctl restart nginx

or

sudo systemctl restart apache2

Next, restrict file permissions. Web directories should be owned by the web server user (e.g., www-data) and have restricted permissions:

sudo chown -R www-data:www-data /var/www/html

sudo find /var/www/html -type d -exec chmod 755 {} \;

sudo find /var/www/html -type f -exec chmod 644 {} \;

Disable directory listing in Nginx:

autoindex off;

In Apache:

Options -Indexes

Implement a Web Application Firewall (WAF) like ModSecurity for Apache or Naxsi for Nginx to filter malicious requests.

9. Harden PHP (If Used)

PHP is a common attack vector for web applications. Edit the PHP configuration:

sudo nano /etc/php/8.1/apache2/php.ini

Or for CLI:

sudo nano /etc/php/8.1/cli/php.ini

Apply these settings:

expose_php = Off

disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

allow_url_fopen = Off

allow_url_include = Off

upload_max_filesize = 2M

post_max_size = 2M

max_execution_time = 30

memory_limit = 256M

Restart Apache or PHP-FPM:

sudo systemctl restart apache2

or

sudo systemctl restart php8.1-fpm

These settings prevent remote code execution, file uploads from malicious scripts, and resource exhaustion attacks.

10. Install and Configure a Reverse Proxy with SSL/TLS

Never serve content over HTTP. Always use HTTPS with a valid SSL/TLS certificate. Lets Encrypt provides free, automated certificates.

Install Certbot:

sudo apt install certbot python3-certbot-nginx

For Nginx:

sudo certbot --nginx

Follow prompts to select your domain and enable HTTPS redirection. Certbot will automatically configure SSL and set up auto-renewal.

Test renewal:

sudo certbot renew --dry-run

Ensure the renewal cron job is active:

sudo systemctl status certbot.timer

For Apache, use:

sudo certbot --apache

Once SSL is active, enforce HTTPS by redirecting all HTTP traffic. In Nginx, ensure your server block includes:

server {

listen 80;

server_name yourdomain.com;

return 301 https://$server_name$request_uri;

}

SSL/TLS encryption protects data in transit and is required for modern web standards, SEO ranking, and user trust.

11. Monitor Logs and Set Up Alerts

Proactive monitoring detects intrusions before they escalate. Regularly review logs:

sudo tail -f /var/log/auth.log        

SSH attempts

sudo tail -f /var/log/nginx/error.log

Web server errors

sudo tail -f /var/log/syslog

System events

Use tools like logwatch or logcheck to generate daily summaries:

sudo apt install logwatch

sudo logwatch --detail High --output mail --mailto your@email.com

Set up centralized logging with tools like Graylog or ELK Stack for multi-server environments.

12. Disable Unused Services and Ports

Every running service is a potential entry point. Identify whats listening:

sudo ss -tuln

Or:

sudo netstat -tuln

Look for unexpected services (e.g., FTP, Telnet, SMB). Disable them:

sudo systemctl stop vsftpd

sudo systemctl disable vsftpd

Remove unused packages:

sudo apt autoremove

Use lsof -i to see which processes are bound to network ports. Close anything not required for your application.

Best Practices

Security is not a one-time setupits an ongoing discipline. Below are essential best practices to maintain a hardened VPS environment.

Use the Principle of Least Privilege

Never run applications as root. Create dedicated system users for services like databases, web servers, and cron jobs. For example, if youre running a Node.js app, create a user named nodeapp and run the process under that account.

Implement Regular Backups

Even the most secure server can be compromised or corrupted. Schedule daily automated backups using rsync, borg, or cloud-based tools. Store backups off-serverpreferably encrypted and in a separate geographic location.

Example cron job for daily backup:

0 2 * * * tar -czf /backups/server-backup-$(date +\%Y\%m\%d).tar.gz /var/www/html /etc/nginx /var/lib/mysql

Test your backups monthly by restoring to a sandbox environment.

Use Strong, Unique Passwords and a Password Manager

Even with SSH keys, you may need passwords for databases, admin panels, or SFTP. Use a password manager like Bitwarden or 1Password to generate and store complex passwords. Never reuse passwords across services.

Enable Two-Factor Authentication (2FA) for Administrative Access

For web-based admin interfaces (e.g., phpMyAdmin, Webmin), enable 2FA using TOTP (Time-Based One-Time Password). Install Google Authenticator on your phone and configure it with your admin panel. This adds a second layer even if credentials are leaked.

Restrict Access by IP (Whitelisting)

If you access your server only from a fixed location (home or office), restrict SSH access to your IP address:

sudo nano /etc/hosts.allow

Add:

sshd: YOUR.IP.ADDRESS.HERE

Then in /etc/hosts.deny:

sshd: ALL

This blocks all SSH attempts except from your specified IP. Use this cautiouslyensure you wont lose access if your IP changes.

Audit User Accounts Regularly

Periodically check for unauthorized users:

cat /etc/passwd

Look for unfamiliar usernames or UIDs under 1000 (system users). Remove any that arent legitimate:

sudo deluser username

Also check sudoers:

sudo cat /etc/sudoers

sudo cat /etc/sudoers.d/*

Remove unnecessary users from sudo groups.

Monitor Resource Usage and Set Alerts

Unusual spikes in CPU, memory, or bandwidth may indicate a compromised server (e.g., crypto mining). Install monitoring tools like Netdata or Prometheus + Grafana. Set up email or SMS alerts for thresholds (e.g., >90% CPU for 5 minutes).

Disable ICMP Ping (Optional but Recommended)

While not a major security risk, disabling ICMP responses reduces visibility to network scanners:

echo 1 | sudo tee /proc/sys/net/ipv4/icmp_echo_ignore_all

To make it permanent, add to /etc/sysctl.conf:

net.ipv4.icmp_echo_ignore_all = 1

Apply with:

sudo sysctl -p

Tools and Resources

Security is enhanced with the right tools. Below is a curated list of essential utilities and resources.

Essential Security Tools

  • Fail2Ban Blocks brute-force login attempts.
  • UFW / firewalld Simple firewall management.
  • Certbot Automates Lets Encrypt SSL certificate issuance.
  • ClamAV Open-source antivirus scanner for detecting malware.
  • OSSEC Host-based intrusion detection system (HIDS) with log analysis.
  • lynis Security auditing tool that scans for misconfigurations and vulnerabilities.
  • Netdata Real-time performance and health monitoring.
  • Logwatch Daily log summary generator.

Security Auditing Tools

Run these periodically to assess your servers security posture:

sudo apt install lynis

sudo lynis audit system

Lynis provides a detailed report with recommendations, risk scores, and compliance checks. Its invaluable for identifying overlooked misconfigurations.

Security News and Resources

  • CVE Details https://www.cvedetails.com Track vulnerabilities by software.
  • OWASP Top 10 https://owasp.org/www-project-top-ten/ Web application security risks.
  • Linux Security Blog https://linuxsecurity.com Tutorials and advisories.
  • GitHub Security Advisories https://github.com/advisories Monitor open-source package vulnerabilities.
  • National Institute of Standards and Technology (NIST) https://www.nist.gov/cyberframework Security frameworks and guidelines.

Automated Security Scanners

For advanced users, consider automated scanning tools:

  • Nmap Scan open ports and services.
  • OpenVAS Full vulnerability scanner.
  • Trivy Container and OS vulnerability scanner.

Use these tools in a controlled environment to audit your server before going live.

Real Examples

Example 1: E-Commerce Site Compromised by Outdated WordPress

A small business hosted a WordPress site on an unsecured VPS. The server ran Ubuntu 18.04 with Apache, PHP 7.2, and WordPress 5.2all outdated. An attacker exploited a known vulnerability in an old WordPress plugin (CVE-2020-11501) to upload a PHP shell. The shell allowed full system access, leading to data theft and use of the server to mine Monero.

What went wrong:

  • No automatic updates.
  • Root SSH login enabled.
  • No firewall or Fail2Ban.
  • Unused plugins not removed.

Fix applied:

  • Upgraded to Ubuntu 22.04 and PHP 8.1.
  • Disabled root login and enabled SSH keys.
  • Installed UFW and Fail2Ban.
  • Removed all unused plugins and themes.
  • Enabled automatic updates and daily backups.
  • Added Cloudflare WAF and SSL.

Within 48 hours, the server was clean and secured. No further breaches occurred.

Example 2: API Server Attacked via Exposed Docker Port

A developer deployed a Node.js API on a VPS using Docker. They exposed port 3000 directly to the internet without authentication. An attacker discovered the open port and exploited a misconfigured API endpoint to gain shell access via command injection.

What went wrong:

  • Docker exposed directly to public internet.
  • No API key or authentication layer.
  • No rate limiting.
  • Container ran as root.

Fix applied:

  • Placed Nginx as reverse proxy in front of Docker.
  • Added API key authentication and JWT validation.
  • Configured rate limiting with Nginx.
  • Modified Dockerfile to run as non-root user.
  • Added UFW to block all ports except 80 and 443.

The API became significantly more resilient and compliant with OWASP API Security Top 10.

Example 3: DNS Hijacking via Weak DNS Provider Credentials

A servers domain was redirected to a phishing site. The attacker gained access to the domain registrars control panel using a weak password reused from the VPS root account.

Lesson: Never reuse passwords. Even if your server is secure, your domain can be hijacked via weak external credentials.

Fix:

  • Changed all passwords using a password manager.
  • Enabled 2FA on the domain registrar.
  • Enabled domain locking.
  • Set up DNSSEC for cryptographic validation of DNS records.

FAQs

How often should I update my VPS?

Apply security updates immediately. Enable unattended-upgrades for critical patches. Perform full system updates weekly. Always test updates in a staging environment before applying to production.

Is a VPS more secure than shared hosting?

Yes, but only if properly secured. Shared hosting often has built-in protections, but you have no control over the environment. A VPS gives you full control, which means youre responsible for security. With proper configuration, a VPS is far more secure than poorly managed shared hosting.

Can I use a free SSL certificate?

Yes. Lets Encrypt provides free, trusted SSL certificates that are automatically renewable. There is no security difference between a free certificate and a paid oneboth use the same encryption standards.

Whats the biggest mistake people make when securing a VPS?

Assuming the provider secures it for them. VPS providers deliver a blank OS. Its your responsibility to harden it. Most breaches occur due to misconfigurations, not provider failures.

Do I need antivirus on my Linux VPS?

Not typically for personal use, but recommended if you host user-uploaded files or serve as a file server. ClamAV is lightweight and effective for scanning uploads or shared directories.

Should I disable IPv6?

No. IPv6 is secure when configured properly. Instead, configure your firewall to allow only necessary IPv6 traffic. Disabling it may cause future compatibility issues.

How do I know if my server has been compromised?

Signs include: unexpected processes in top, high CPU usage at odd hours, unfamiliar files in /tmp or /var/www, new user accounts, outbound traffic spikes, or unexpected DNS changes. Use lynis, chkrootkit, and rkhunter to scan for rootkits.

Can I use a GUI to manage my VPS securely?

Yes, but with caution. Tools like Webmin or Cockpit can be useful, but they add another attack surface. Always secure them with strong passwords, 2FA, and restrict access by IP. Prefer command-line tools for maximum control and security.

What should I do if my VPS is hacked?

Immediately disconnect it from the network. Do not reboot. Take a forensic image if possible. Investigate logs to determine the entry point. Rebuild the server from scratchnever trust files or configurations from a compromised system. Restore data from a clean backup.

Conclusion

Securing a VPS is not a task to be completed onceits an ongoing commitment to digital hygiene. Every step outlined in this guidefrom disabling root login and enforcing SSH keys to automating updates and monitoring logsbuilds a layered defense that makes your server a poor target for attackers.

Modern cyber threats are automated, persistent, and relentless. But with the right configuration, tools, and discipline, your VPS can stand as a fortress rather than a vulnerability. Remember: security is not about perfectionits about reducing risk at every level.

Apply these practices now. Test each step. Automate what you can. Monitor continuously. And never assume your server is safe because it hasnt been hacked yet. Proactive security saves time, money, and reputation. Your data, your users, and your business depend on it.