Linux VPS security hardening checklist

Securing a Linux VPS is a crucial task for anyone who wants to protect their data and maintain a robust online presence. With cyber threats becoming increasingly sophisticated, it’s essential to implement effective security measures to harden your server against potential attacks. This guide will walk you through a comprehensive checklist of 12 steps to fortify your Linux server, ensuring that your VPS remains resilient and secure.

This article is part of Rackset’s Server Security guides — browse the rest of the category for more in-depth tutorials on this topic.

From configuring firewalls and setting up SSH keys to monitoring logs and enforcing strong password policies, each step in this article is designed to enhance the security of your Linux VPS. Whether you’re a seasoned system administrator or a beginner looking to safeguard your server, these practical and actionable strategies will help you build a strong defense against unauthorized access and vulnerabilities. By following this checklist, you’ll be well-equipped to protect your server and keep your data safe.

Implement SSH Key Authentication

Enhancing the security of your Linux server involves replacing password-based logins with SSH key authentication. This method is more secure because it uses a pair of cryptographic keys: a public key and a private key. Here’s how you can set it up effectively:

Generate SSH Key Pair

  1. Open a terminal on your local machine.
  2. Use the following command to generate a new SSH key pair:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  3. When prompted, specify a file to save the key (press Enter to accept the default location).
  4. Enter a secure passphrase when asked. This adds an extra layer of security.

Copy Public Key to Server

To enable SSH key authentication, you need to copy your public key to the server:

  1. Use the ssh-copy-id command:
    ssh-copy-id username@your_server_ip
  2. If ssh-copy-id is not available, manually copy the key:
    cat ~/.ssh/id_rsa.pub | ssh username@your_server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Disable Password Authentication

After successfully configuring SSH key authentication, disable password authentication to enhance security:

  1. Open the SSH configuration file on your server:
    sudo nano /etc/ssh/sshd_config
  2. Find and modify the following line:
    PasswordAuthentication no
  3. Save and close the file, then restart the SSH service:
    sudo systemctl restart sshd

By implementing SSH key authentication, you significantly reduce the risk of unauthorized access to your Linux server. Ensure that your private key is kept secure and never shared.

Disable Root Login and Change SSH Port

Hardening a Linux server

Securing your Linux VPS involves several crucial steps, one of which is disabling root login and changing the default SSH port. These measures help prevent unauthorized access and mitigate potential brute force attacks.

Why Disable Root Login?

The root user has unrestricted access to all commands and files on a Linux system. Allowing remote root login poses a significant security risk. By disabling it, you force attackers to first guess a valid username before attempting to crack a password, adding an extra layer of security.

Steps to Disable Root Login

  1. Open the SSH configuration file using a text editor like nano or vi:
    sudo nano /etc/ssh/sshd_config
  2. Locate the line that reads PermitRootLogin yes and change it to:
    PermitRootLogin no
  3. Save the changes and exit the editor.
  4. Restart the SSH service to apply the changes:
    sudo systemctl restart sshd

Why Change the SSH Port?

The default SSH port is 22, which is well-known and often targeted by automated attacks. Changing it to a non-standard port can reduce the likelihood of attack and decrease the volume of unauthorized login attempts.

If you want VPS power without managing the server yourself, a managed VPS plan hands the setup, security patching and monitoring to Rackset’s team so you can focus on your project.

Steps to Change the SSH Port

  1. In the same SSH configuration file, find the line:
    #Port 22
  2. Uncomment the line by removing the # and specify a new port number, for example:
    Port 2222
  3. Save the changes and exit the editor.
  4. Update your firewall rules to allow traffic on the new port:
    sudo ufw allow 2222/tcp
  5. Restart the SSH service to implement the new configuration:
    sudo systemctl restart sshd
  6. Verify the SSH connection using the new port:
    ssh -p 2222 username@your_server_ip

By following these steps, you significantly enhance your server’s security by restricting direct root access and reducing exposure to common attack vectors.

Configure a Firewall with UFW or nftables

Securing your Linux VPS involves configuring a robust firewall to control incoming and outgoing traffic. Two popular tools for this purpose are UFW (Uncomplicated Firewall) and nftables. Both provide effective ways to manage firewall rules and protect your server from unauthorized access.

Using UFW (Uncomplicated Firewall)

UFW is a user-friendly command-line interface for managing iptables firewall rules. It simplifies the process of setting up a firewall on your Linux server. Follow these steps to configure UFW:

  1. Install UFW: Most distributions come with UFW pre-installed. If not, you can install it using the package manager. For example:
    sudo apt install ufw
  2. Enable UFW: Before enabling, ensure that the necessary ports are open. Enable UFW with:
    sudo ufw enable
  3. Allow SSH: Ensure SSH connections are allowed to maintain remote access:
    sudo ufw allow ssh
  4. Configure other rules: Add rules for other services you wish to allow, such as HTTP or HTTPS:
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
  5. Check status: Verify the status and rules with:
    sudo ufw status

Using nftables

nftables is a modern replacement for iptables, providing a more flexible and efficient firewall solution. To configure nftables, follow these steps:

  1. Install nftables: It is available in most Linux distributions. Install it using:
    sudo apt install nftables
  2. Create a ruleset: Define your ruleset in a file, for example:
    
    table inet filter {
      chain input {
        type filter hook input priority 0;
        policy drop;
        iif lo accept;
        ct state established,related accept;
        tcp dport { 22, 80, 443 } accept;
      }
    }
        
  3. Load the ruleset: Apply the ruleset using:
    sudo nft -f /etc/nftables.conf
  4. Enable nftables: Ensure nftables starts on boot:
    sudo systemctl enable nftables

By configuring a firewall with UFW or nftables, you add a critical layer of security to your Linux VPS, helping to protect it from unauthorized access and potential threats.

Install and Configure Fail2Ban

Firewall, SSH keys and fail2ban

Fail2Ban is a crucial tool for enhancing the security of your Linux server by automatically banning IP addresses that exhibit suspicious behavior. It helps prevent various attacks, such as brute force attempts, by monitoring log files and quickly responding to potential threats. Follow the steps below to install and configure Fail2Ban effectively.

Step 1: Install Fail2Ban

Begin by installing Fail2Ban from your package manager. Use the following commands depending on your Linux distribution:

  • For Debian/Ubuntu:
    sudo apt update
    sudo apt install fail2ban
  • For CentOS/RHEL:
    sudo yum install epel-release
    sudo yum install fail2ban

Step 2: Enable and Start Fail2Ban

Once installed, enable and start the Fail2Ban service to ensure it runs automatically at boot:

If you need this service, Rackset’s team provides professional server management with full support so you can rely on the quality and get set up quickly.

sudo systemctl enable fail2ban
sudo systemctl start fail2ban

Step 3: Configure Fail2Ban

Customize Fail2Ban settings to suit your server’s needs by editing the jail configuration file. It’s recommended to create a local copy for custom settings:

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

Open the jail.local file in a text editor to configure specific settings:

  • Ban Time: Set the duration for which an IP is banned.
  • Find Time: Define the time period within which the maximum allowed attempts are counted.
  • Max Retry: Specify the number of failed login attempts before an IP is banned.
sudo nano /etc/fail2ban/jail.local

Step 4: Restart Fail2Ban

After configuring, restart the Fail2Ban service to apply the changes:

sudo systemctl restart fail2ban

By following these steps, you enhance your Linux server’s security, reducing the risk of unauthorized access and attacks.

Set Up Automatic Security Updates and Least-Privilege Users

Ensuring that your Linux VPS is secure involves setting up automatic security updates and managing user privileges effectively. These steps help minimize vulnerabilities and protect your server from unauthorized access.

Automatic Security Updates

Keeping your Linux server updated with the latest security patches is crucial. Automating this process ensures that you do not miss critical updates. Here’s how to set up automatic security updates:

  1. Install Unattended Upgrades: This package allows automatic installation of security updates. Use the following command to install it:
    sudo apt install unattended-upgrades
  2. Enable Unattended Upgrades: Configure the package to apply updates automatically:
    sudo dpkg-reconfigure --priority=low unattended-upgrades
  3. Configure Automatic Updates: Edit the configuration file to customize the update settings:
    sudo nano /etc/apt/apt.conf.d/50unattended-upgrades

    Ensure that security updates are included by checking that the following line is uncommented:

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

Implement Least-Privilege Users

Limiting user privileges is a fundamental principle of Linux server hardening. By adhering to the least-privilege model, you reduce the risk of accidental or malicious damage. Follow these steps to implement least-privilege users:

  1. Create a New User: Avoid using the root account for daily operations. Create a new user with limited privileges:
    sudo adduser username
  2. Assign Sudo Privileges: Grant administrative privileges only when necessary by adding the user to the sudo group:
    sudo usermod -aG sudo username
  3. Restrict SSH Access: Allow only specific users to access the server via SSH:
    sudo nano /etc/ssh/sshd_config

    Add the following line to specify allowed users:

    For projects that need guaranteed resources and full root control, a VPS is the right step up from shared hosting. With VPS hosting you get dedicated CPU/RAM, NVMe storage, a dedicated IP and high uptime, deployed in minutes.

    AllowUsers username

By automating security updates and implementing least-privilege user accounts, you can significantly enhance the security posture of your Linux VPS.

Enable Audit Logging and Regular Backups

Ensuring your Linux server is secure involves not only protecting it from unauthorized access but also maintaining records of activities and having a recovery plan in place. Two crucial steps in this process are enabling audit logging and scheduling regular backups.

Enable Audit Logging

Audit logging is essential for tracking access and changes to your server. It provides detailed insights into who did what and when, which is invaluable for both troubleshooting and forensic analysis in case of security incidents.

  • Install Auditd: The Linux Audit system is a powerful tool for monitoring system calls and file access. To install Auditd, use the package manager specific to your distribution. For example, on Debian-based systems, you can run:
    sudo apt-get install auditd
  • Configure Audit Rules: Define what events you want to log. This can include file access, user logins, and system call tracking. Create or modify the /etc/audit/audit.rules file to specify these rules.
  • Review Logs Regularly: Regularly review the logs generated by Auditd to detect any suspicious activities. Log files are typically stored in /var/log/audit/.

Schedule Regular Backups

Backups are your safety net in case of data loss or server compromise. Regularly scheduled backups ensure that you can quickly restore your system to a previous state.

  • Automate Backups: Use tools like rsync or tar combined with cron jobs to automate the backup process. For example, a cron job to backup a directory could look like:
    0 2 * * * /usr/bin/rsync -a /home/user/ /backup/destination/
  • Store Backups Securely: Ensure that your backups are stored in a secure location, ideally offsite or in a separate network segment. Rackset’s hosting solutions offer secure backup storage options.
  • Test Backup Restorations: Periodically test your backups by restoring them to ensure that the data is intact and the restoration process works smoothly.

By incorporating these practices into your Linux server hardening strategy, you can significantly enhance your server’s security posture. Rackset provides robust hosting environments that support these security measures, giving you peace of mind.

Need expert help from Rackset?

Want this done fast, professionally and hassle-free? Rackset’s experienced team and full support are here to help you get the best result.

Buy managed VPS

Conclusion

Securing your Linux VPS is a critical step in ensuring the integrity and reliability of your server environment. By following the 12-step checklist for Linux server hardening, you can protect your server against unauthorized access, data breaches, and other security threats. From updating your system and configuring a firewall to implementing strong password policies and monitoring system logs, each step plays a vital role in fortifying your server’s defenses.

Remember, security is an ongoing process that requires regular updates and vigilance. By choosing Rackset for your hosting needs, you gain access to robust security features and expert support, helping you maintain a secure and efficient server environment. Let Rackset be your partner in achieving top-notch server security and performance.

Frequently Asked Questions

What is Linux server hardening?

Linux server hardening involves implementing security measures to protect your server from unauthorized access and vulnerabilities. This includes configuring firewalls, updating software, and setting strong passwords to enhance the overall security posture of your server.

Why is it important to regularly update my Linux server?

Regularly updating your Linux server ensures that you have the latest security patches and software enhancements. These updates help protect your server from known vulnerabilities and potential exploits that could be targeted by attackers.

How does disabling root login improve server security?

Disabling root login prevents attackers from easily accessing your server with the default administrative account. By using a normal user account with sudo privileges instead, you reduce the risk of brute-force attacks and unauthorized access.

What role does a firewall play in Linux server hardening?

A firewall acts as a barrier between your server and potential threats from the internet by controlling incoming and outgoing traffic. Properly configuring a firewall can block unwanted access and limit exposure to vulnerabilities, thereby enhancing server security.

Why should I monitor server logs for security purposes?

Monitoring server logs helps you detect suspicious activities and potential security breaches in real-time. By analyzing logs, you can identify patterns of unauthorized access attempts and take immediate action to secure your server.

Leave a Reply

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

Count: 74 - Average: 4.7

Rating Submitted.