Prerequisites
Before securing your VPS, make sure you have:
- SSH access to your VPS
- Root or sudo privileges
Step 1: Update Your System
apt update && apt upgrade -y
Step 2: Create a Sudo User
adduser username
usermod -aG sudo username
Step 3: Set Up SSH Key Authentication
On local machine:
ssh-keygen -t rsa -b 4096
ssh-copy-id username@YOUR_SERVER_IP
Step 4: Disable Root Login and Password Auth
Edit /etc/ssh/sshd_config:
PermitRootLogin no
PasswordAuthentication no
systemctl restart sshd
Step 5: Change SSH Port
Port 2222
Step 6: Set Up UFW Firewall
ufw allow 2222/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
Step 7: Install Fail2ban
apt install fail2ban -y
Step 8: Enable Automatic Security Updates
apt install unattended-upgrades -y
dpkg-reconfigure --priority=low unattended-upgrades
Step 9: Set Up Regular Backups
Create backup script and add to cron.
Step 10: Monitor Logs Regularly
journalctl -u ssh -n 50
grep "Failed password" /var/log/auth.log
✅ Your VPS is now significantly more secure.