Setting up a VPS
Context
To practice with Linux, I decided to create this script that will prepare and secure a brand-new VPS (rented from Netcup).
Thanks to snapshots, I was able to test the script's execution multiple times and revert to a fresh VPS each time, allowing me to conduct tests safely.
Step-by-step Script Breakdown
Server initialization
Connect to the VPS via SSH as root, then update the package list and packages to ensure a clean and up-to-date environment (~3 seconds).
Installation of essential tools
Install essential packages like ufw
for the firewall, fail2ban
for protection against attacks, and nginx
to host a website (~15 seconds).
Securing the root account
Change the password of the root
account to enhance security (~5 seconds).
Time zone configuration
Update the time zone to Paris for better log readability and time consistency (~2 seconds).
Terminal customization
Download a customized .bashrc
including aliases and command timestamping in the history for a more ergonomic environment (~5 seconds).
Strengthening SSH security
Change the default SSH port to a non-standard port (49152–65535), disable root login, and set up automatic disconnection after 120 seconds of inactivity (~5 seconds).
Creating a personal user account
Create a non-root account with sudo privileges, add it to the SSH whitelist to restrict access to authorized users only (~5 seconds).
Firewall configuration
Allow the new SSH port and web traffic for Nginx, then enable the ufw
firewall to block everything else (~5 seconds).
Fix for NGINX bug
Add a special configuration to avoid a common error when starting NGINX on some VPS (~5 seconds).
Fail2ban configuration for SSH
Create the log file /var/log/auth_fail2ban.log
if needed, then configure a specific jail for SSH to protect against brute-force attacks (~10 seconds).
Switching to the user account
Log in with the new user account, marking the end of root configuration and the start of secure VPS management (~3 seconds).