Server security is not optional - it is a fundamental requirement. Every VPS connected to the internet faces constant automated attacks, brute-force login attempts, and vulnerability scans. This comprehensive guide covers three essential layers of defense: SSH key authentication, firewall configuration, and intrusion prevention with Fail2ban.
1. SSH Key Authentication
SSH keys provide cryptographic authentication that is vastly more secure than passwords. A key pair consists of a private key (kept on your local machine) and a public key (placed on the server).
Generate an SSH Key Pair
On your local machine (not the server), generate a new Ed25519 key pair:
When prompted, accept the default file location and set a strong passphrase. Ed25519 keys are shorter, faster, and more secure than traditional RSA keys.
Copy the Public Key to Your Server
If ssh-copy-id is not available (common on Windows), manually copy the key:
Test Key-Based Login
If the key is correctly installed, you will not be asked for a server password (though your key passphrase will be requested if set).
Disable Password Authentication
Find and modify these directives:
2. Advanced Firewall Configuration with UFW
While basic UFW setup was covered in our Getting Started with Your SakuraHost VPS guide, production servers require more granular rules.
Essential Firewall Rules
Rate Limiting SSH Connections
UFW includes a built-in rate limiter that blocks IP addresses attempting more than 6 connections within 30 seconds:
View your active rules at any time with sudo ufw status verbose.
3. Installing and Configuring Fail2ban
Fail2ban monitors log files for suspicious activity and automatically bans offending IP addresses. It is your active defense against brute-force attacks.
Recommended Jail Configuration
Add or modify the following in jail.local:
This configuration bans any IP that fails SSH login 3 times within 10 minutes, blocking them for 24 hours (86400 seconds). The general default bans for 1 hour.
Monitoring Fail2ban
4. Additional Security Hardening
Change the Default SSH Port
While security through obscurity is not a complete solution, changing the default SSH port dramatically reduces automated scan noise:
Install and Configure Logwatch
Logwatch sends you daily email summaries of server activity:
5. Security Checklist
- SSH key authentication enabled, password authentication disabled
- Root login disabled via SSH
- UFW firewall active with minimal open ports
- Fail2ban running with SSH jail configured
- Automatic security updates enabled
- Non-standard SSH port (optional but recommended)
- Regular log review process in place