Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 3 min read

Securing a Fresh Linux VPS: SSH Keys, UFW, and Fail2ban in 20 Minutes

Introduction to Securing Your Linux VPS Congratulations on your new Linux VPS! Whether you're hosting a website, setting up a development environment, or experimenting with server management, securing your server shoul

Introduction to Securing Your Linux VPS

Congratulations on your new Linux VPS! Whether you're hosting a website, setting up a development environment, or experimenting with server management, securing your server should be your top priority. In this guide, we'll explore how to enhance the security of your VPS by setting up SSH keys, configuring a firewall with UFW, and using Fail2ban to protect against unauthorized access. This quick, 20-minute walkthrough will provide you with a solid foundation for server security.

Setting Up SSH Key Authentication

One of the most effective ways to secure your VPS is by using SSH key authentication instead of passwords. SSH keys provide stronger security and eliminate the risk of brute-force attacks on your server's login credentials. Here’s how to set up SSH key authentication:

  1. Generate an SSH Key Pair: On your local machine, open a terminal and run ssh-keygen. Follow the prompts to create a new key pair. By default, the keys will be stored in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.
  2. Copy the Public Key to Your VPS: Use the command ssh-copy-id user@your_vps_ip to transfer your public key to the VPS. This command will prompt you for your user password on the VPS.
  3. Disable Password Authentication: Edit the SSH configuration file on your VPS by running sudo nano /etc/ssh/sshd_config. Find the line PasswordAuthentication and change its value to no. Save the file and restart the SSH service with sudo systemctl restart ssh.

For a more detailed guide, you can refer to this resource on how to set up SSH key authentication.

Enabling UFW for Firewall Protection

The Uncomplicated Firewall (UFW) is a user-friendly interface to iptables, making it easier to manage firewall rules. By default, UFW is disabled on most Linux distributions, so you'll need to activate it and configure the necessary rules:

  1. Install UFW: If UFW is not already installed, you can install it using sudo apt install ufw on Debian-based systems or sudo yum install ufw on Red Hat-based systems.
  2. Allow SSH Connections: Since you'll be managing your VPS remotely, ensure that SSH connections are allowed by running sudo ufw allow ssh.
  3. Enable UFW: Activate the firewall by executing sudo ufw enable. You can check the status of UFW and the rules in place with sudo ufw status.
  4. Add Additional Rules: Depending on the services you plan to run on your VPS, you'll need to allow other ports. For example, if you're hosting a web server, you would use sudo ufw allow http and sudo ufw allow https.

Implementing Fail2ban for Intrusion Prevention

Fail2ban is a powerful tool that protects your server from brute-force attacks by scanning log files and banning IPs that show suspicious behavior. Follow these steps to set up Fail2ban:

  1. Install Fail2ban: On Debian-based systems, use sudo apt install fail2ban. For Red Hat-based systems, install the EPEL repository first and then use sudo yum install fail2ban.
  2. Configure Fail2ban: Create a local configuration file by copying the default configuration: sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local. Edit the jail.local file to configure the services you want to protect. Ensure that SSH is included.
  3. Start and Enable Fail2ban: Start the service with sudo systemctl start fail2ban and enable it to start on boot with sudo systemctl enable fail2ban.
  4. Monitor Fail2ban: You can monitor its activity using sudo fail2ban-client status and check which IPs have been banned.

Conclusion

By implementing these security measures, you've taken significant steps toward ensuring the safety of your Linux VPS. Remember, security is an ongoing process, and staying informed about potential threats and updates is essential. If you're ready to put these practices to use on your own server, consider taking the next step: Buy Linux VPS and start your journey in secure server management today.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.