DEV Community

Arthur
Arthur

Posted on

How to Secure a Linux VPS: 7 Practical Steps for Beginners

Hello, I'm Arthur. Setting up a Linux VPS is exciting, especially when you're preparing to launch a website or application.

But before making your server publicly accessible, there are a few important security steps you should take.

A server with weak passwords, unnecessary open ports, or outdated software can become an easy target for attackers.

In this guide, I'll explain seven practical ways to improve the security of a Linux VPS without making the setup unnecessarily complicated.

1. Update Your Linux System

One of the simplest ways to improve server security is to keep your operating system and installed packages updated.

On Ubuntu or Debian, run:

sudo apt update
sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

The first command refreshes the package information, while the second installs available updates.

Security updates can fix vulnerabilities in system packages and applications.

For production servers, review important updates and plan maintenance carefully, especially when an update requires a reboot.

2. Use SSH Keys Instead of Passwords

SSH allows you to connect to your Linux server remotely.

Password-based authentication can be vulnerable to brute-force attempts, especially when weak passwords are used.

SSH keys provide a more secure authentication method.

On your local computer, generate a key pair:

ssh-keygen -t ed25519
Enter fullscreen mode Exit fullscreen mode

This creates a public key and a private key.

Copy the public key to your server:

ssh-copy-id username@your-server-ip
Enter fullscreen mode Exit fullscreen mode

Replace username and your-server-ip with your actual server details.

Test the connection in a new terminal before changing your SSH configuration.

Never share your private key with anyone. Keep it protected on your own device.

3. Create a Separate User for Administration

Using the root account for everyday tasks is not a good security practice.

Instead, create a regular user:

sudo adduser adminuser
Enter fullscreen mode Exit fullscreen mode

Give the user administrative privileges:

sudo usermod -aG sudo adminuser
Enter fullscreen mode Exit fullscreen mode

You can now perform administrative tasks using sudo when necessary.

For example:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

This approach helps reduce the risk of accidentally making system-wide changes while working as root.

4. Configure a Firewall

A firewall controls which network connections are allowed to reach your server.

On Ubuntu, you can use UFW.

Install it if necessary:

sudo apt install ufw
Enter fullscreen mode Exit fullscreen mode

Allow SSH connections before enabling the firewall:

sudo ufw allow OpenSSH
Enter fullscreen mode Exit fullscreen mode

If your server hosts a website, allow HTTP and HTTPS traffic:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Enter fullscreen mode Exit fullscreen mode

Enable the firewall:

sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

Check the active rules:

sudo ufw status
Enter fullscreen mode Exit fullscreen mode

Only open ports that your applications actually need.

Important: If your SSH service uses a custom port, allow that port before enabling the firewall. Otherwise, you could lose remote access to your server.

5. Disable Unnecessary Services

Every service running on your server may introduce additional security risks.

Check which services are currently listening for network connections:

sudo ss -tulpn
Enter fullscreen mode Exit fullscreen mode

This command displays listening TCP and UDP ports along with the associated processes.

If you find a service that you do not need, investigate it before disabling it.

For example, you can check whether a service is running with:

sudo systemctl status service-name
Enter fullscreen mode Exit fullscreen mode

Replace service-name with the actual service name.

Do not disable services simply because you do not recognize them. Some are essential for your operating system or applications.

6. Protect Your Website With HTTPS

If your VPS hosts a website, HTTPS encrypts the connection between your visitors and your server.

Without HTTPS, information sent between a browser and a website may be exposed to interception.

For a website using Nginx on Ubuntu, Certbot can help configure a TLS certificate.

Install Certbot and its Nginx plugin:

sudo apt install certbot python3-certbot-nginx
Enter fullscreen mode Exit fullscreen mode

Request a certificate:

sudo certbot --nginx
Enter fullscreen mode Exit fullscreen mode

Follow the instructions and provide a domain that already points to your server.

After setup, test that your website loads over HTTPS.

You should also make sure certificate renewal is configured and working.

7. Monitor Your Server and Keep Backups

Security is not a one-time task.

Regular monitoring can help you identify unusual activity, while backups give you a way to recover from certain failures.

Check recent authentication activity:

sudo journalctl -u ssh --since "24 hours ago"
Enter fullscreen mode Exit fullscreen mode

Depending on your Linux distribution, SSH logs may use a different service name or be stored in a different location.

You can also check failed login attempts and investigate unfamiliar activity.

For backups, keep copies of your important website files, databases, and configuration files in a separate location.

Test your restoration process regularly. A backup that cannot be restored is not very useful during an emergency.

Choosing a VPS for Your Website

Security also depends on having a server environment that supports your application requirements.

When comparing VPS hosting options, consider:

  • The operating system and available updates
  • CPU, RAM, and storage resources
  • Network connectivity and bandwidth
  • Backup and recovery options
  • Your ability to manage the server securely

If you're planning to deploy a website or application, you can explore Seimaxim VPS hosting to review available server options.

Final Thoughts

Securing a Linux VPS does not have to be complicated.

Start by updating your system, protecting SSH access, configuring a firewall, and removing unnecessary services.

Then add HTTPS, regular monitoring, and a reliable backup routine.

These basic practices can help you build a more secure foundation for your website or application.

The most important thing is to understand what each security setting does before changing it on a production server.

Top comments (0)