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 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…
Welcome to securing your new Linux VPS! This guide will walk you through enhancing your server's security with three key steps - utilizing SSH keys, configuring a firewall with UFW, and employing Fail2ban against unauthorized access. Within just 20 minutes, you'll establish a robust foundation for server protection. Establishing SSH Key Authentication Enhances security by opting for SSH key authentication over passwords.
SSH keys provide robust protection against brute-force attacks on login credentials. Here's how to configure it: Generate an SSH key pair on your local machine via a terminal by executing ssh-keygen. Follow the prompts to create the key pair, which will be stored in ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub by default. Transfer the public key to your VPS using the command ssh-copy-id user@your_vps_ip.
You'll be prompted to enter your user password on the VPS. Afterward, disable password authentication by editing the SSH configuration file on the VPS with sudo nano /etc/ssh/sshd_config. Locate the PasswordAuthentication line and change its value to no. Finally, restart the SSH service with sudo systemctl restart ssh. A comprehensive guide on setting up SSH key authentication is also available for further clarification.
Activating UFW for Firewall Protection UFW, an uncomplicated firewall, simplifies iptables management for firewall rules. To enable protection, you must first install UFW and configure necessary rules: Install UFW with sudo apt install ufw for Debian-based systems or sudo yum install ufw for Red Hat-based systems. Allow SSH connections by running sudo ufw allow ssh.
Activate the firewall using sudo ufw enable. Check the current status and applied rules with sudo ufw status. Customize additional rules based on the services you intend to run on your VPS. For instance, hosting a web server would require using sudo ufw allow http and sudo ufw allow https. Implementing Fail2ban for Intrusion Prevention Protects your server from brute-force attacks by monitoring log files and banning IPs with suspicious activities.
To set up Fail2ban: Install Fail2ban on Debian-based systems using sudo apt install fail2ban, or on Red Hat-based systems with sudo yum install fail2ban after enabling the EPEL repository first. Configure Fail2ban by creating a local configuration file from the default by copying /etc/fail2ban/jail.conf to /etc/fail2ban/jail.local.
Edit jail.local to specify the services you wish to safeguard, ensuring SSH is included. Start Fail2ban service with sudo systemctl start fail2ban, and enable it to start on boot with sudo systemctl enable fail2ban. Monitor its activity via sudo fail2ban-client status and identify any banned IPs. To conclude, by applying these security measures, you've safeguarded your Linux VPS effectively.
Remember, security necessitates continuous vigilance and staying updated about potential threats and updates. If you're prepared to implement these practices on your server, consider purchasing Linux VPS to embark on your journey of secure server management.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to