PROMO20% OFF with code QY3UJAEZ59 - Limited time offer!
Getting Started

Secure Your Linux VPS

Complete guide to harden your Linux VPS security. Fail2ban, SSH key authentication, and security best practices.

πŸ“–8 min read
⚑Intermediate
πŸ“…1/13/2025
πŸ”„Updated 1/14/2026

Secure Your Linux VPS

Now that your VPS is up and running, it's time to strengthen its security. This guide walks you through protecting your server against common threats.


πŸ“‹ Prerequisites

βœ… Have a working Linux VPS βœ… SSH access with a non-root user βœ… Sudo privileges configured βœ… Completed the Getting Started with Your Linux VPS guide


1

Configure SSH Key Authentication

SSH key authentication is much more secure than passwords.

On Your Local Computer - Windows (PowerShell)

β–Έ Generate an SSH key pair
# Generate an SSH key pair
ssh-keygen -t ed25519 -C "your-email@example.com"

# Press Enter to accept the default location
# Set a passphrase (optional but recommended)

πŸ’‘ Tip: The ed25519 key is more secure and faster than RSA. If your system doesn't support it, use ssh-keygen -t rsa -b 4096.

β–Έ Copy the public key to your VPS
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh yourname@YOUR_VPS_IP "cat >> ~/.ssh/authorized_keys"

On Your Local Computer - macOS / Linux

β–Έ Generate and copy the key
# Generate an SSH key pair
ssh-keygen -t ed25519 -C "your-email@example.com"

# Copy the key to your VPS
ssh-copy-id yourname@YOUR_VPS_IP

Test Key Authentication

# Connect to your VPS (should not ask for password)
ssh yourname@YOUR_VPS_IP

βœ… If the connection works without a password, your SSH key is properly configured!


2

Disable Password Authentication

Once SSH keys are configured, disable password authentication.

⚠️ WARNING

Before disabling password authentication, verify that you can connect with your SSH key! Keep a backup SSH session open.

β–Έ Edit SSH configuration
# Edit SSH configuration
sudo nano /etc/ssh/sshd_config
β–Έ Modify or add these lines
# Disable password authentication
PasswordAuthentication no

# Disable challenge-response authentication
ChallengeResponseAuthentication no

# Completely disable root login
PermitRootLogin no

# Allow only your user (optional)
AllowUsers yourname
β–Έ Restart SSH service
sudo systemctl restart sshd

3

Change Default SSH Port

Changing the SSH port reduces automated intrusion attempts.

β–Έ Edit SSH configuration
# Edit SSH configuration
sudo nano /etc/ssh/sshd_config
β–Έ Change the port
# Default port: 22
Port 2222

πŸ’‘ Tip: Choose a port between 1024 and 65535. Avoid common ports like 2222, prefer something like 49152.

Update Firewall - UFW (Ubuntu/Debian)

# Allow the new port
sudo ufw allow 2222/tcp

# Remove the old port
sudo ufw delete allow 22/tcp

# Reload UFW
sudo ufw reload

Update Firewall - FirewallD (CentOS/Rocky)

# Add the new port
sudo firewall-cmd --permanent --add-port=2222/tcp

# Remove the old port
sudo firewall-cmd --permanent --remove-service=ssh

# Reload firewall
sudo firewall-cmd --reload
β–Έ Restart SSH and reconnect
# Restart SSH
sudo systemctl restart sshd

# To reconnect with the new port
ssh -p 2222 yourname@YOUR_VPS_IP

4

Install and Configure Fail2ban

Fail2ban protects your server against brute force attacks by temporarily banning suspicious IPs.

Installation

β–Έ On Ubuntu / Debian
sudo apt update
sudo apt install fail2ban -y
β–Έ On CentOS / Rocky Linux
sudo yum install epel-release -y
sudo yum install fail2ban -y

Configure Fail2ban

β–Έ Create a local configuration file
# Create a local configuration file
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
β–Έ Recommended configuration
[DEFAULT]
# Ban duration (in seconds) - 1 hour
bantime = 3600

# Detection window duration (10 minutes)
findtime = 600

# Number of attempts before ban
maxretry = 5

# Action to perform (ban the IP)
banaction = iptables-multiport

[sshd]
enabled = true
port = 2222
logpath = %(sshd_log)s
backend = %(sshd_backend)s
maxretry = 3
bantime = 86400

Configuration explained:

  • bantime = 86400 β†’ Ban for 24 hours
  • findtime = 600 β†’ Monitor over a 10-minute window
  • maxretry = 3 β†’ Ban after 3 failed attempts
  • port = 2222 β†’ If you changed the SSH port
β–Έ Start and enable Fail2ban
# Start and enable Fail2ban
sudo systemctl start fail2ban
sudo systemctl enable fail2ban

# Check status
sudo systemctl status fail2ban

Verify Fail2ban

View SSH protection status

sudo fail2ban-client status sshd

View all active jails

sudo fail2ban-client status

Useful Fail2ban commands:

CommandDescription
sudo fail2ban-client status sshdSSH jail status
sudo fail2ban-client set sshd unbanip IPUnban an IP
sudo fail2ban-client reloadReload configuration
sudo tail -f /var/log/fail2ban.logView logs in real-time

5

Configure Advanced Firewall

Configure your firewall to only allow necessary services.

UFW Configuration (Ubuntu/Debian)

# Reset UFW (optional)
sudo ufw --force reset

# Default policy: deny all incoming traffic
sudo ufw default deny incoming
sudo ufw default allow outgoing

# Allow SSH (with new port)
sudo ufw allow 2222/tcp

# Allow HTTP and HTTPS (if you have a web server)
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

# Limit SSH connections (anti-bruteforce protection)
sudo ufw limit 2222/tcp

# Enable UFW
sudo ufw enable

# Check status
sudo ufw status verbose

FirewallD Configuration (CentOS/Rocky)

# Set default zone
sudo firewall-cmd --set-default-zone=public

# Allow SSH
sudo firewall-cmd --permanent --add-port=2222/tcp

# Allow HTTP/HTTPS
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# Limit SSH connections
sudo firewall-cmd --permanent --add-rich-rule='rule service name=ssh limit value=3/m accept'

# Reload
sudo firewall-cmd --reload

# Verify
sudo firewall-cmd --list-all

6

Monitor System Logs

Regularly monitor logs to detect suspicious activity.

View recent SSH login attempts

sudo grep "Failed password" /var/log/auth.log | tail -20

View successful SSH connections

sudo grep "Accepted" /var/log/auth.log | tail -20

Monitor logs in real-time

sudo tail -f /var/log/auth.log

πŸ’‘ Tip: On CentOS/Rocky, use /var/log/secure instead of /var/log/auth.log.


7

Install Security Audit Tools

Lynis - Automatic Security Audit

β–Έ Installation and usage
# Installation on Ubuntu/Debian
sudo apt install lynis -y

# Installation on CentOS/Rocky
sudo yum install lynis -y

# Run a complete audit
sudo lynis audit system

RKHunter - Rootkit Detection

β–Έ Installation and usage
# Installation on Ubuntu/Debian
sudo apt install rkhunter -y

# Installation on CentOS/Rocky
sudo yum install rkhunter -y

# Update database
sudo rkhunter --update

# Run a scan
sudo rkhunter --check

8

Keep System Updated

β–Έ On Ubuntu/Debian
# Configure automatic security updates
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgrades

# Check for updates manually
sudo apt update && sudo apt upgrade -y

⚠️ Important

Security updates are crucial. Configure automatic updates and regularly verify your system is up to date.


9

Configure Automatic Backups

Never neglect backups!

β–Έ Create directory and script
# Create a directory for backups
mkdir -p ~/backups

# Create backup script
sudo nano /usr/local/bin/backup.sh
β–Έ Backup script
#!/bin/bash

# Configuration
BACKUP_DIR="/home/yourname/backups"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
BACKUP_FILE="backup_$DATE.tar.gz"

# Create archive
tar -czf "$BACKUP_DIR/$BACKUP_FILE" \
    /etc \
    /home/yourname \
    --exclude=/home/yourname/backups

# Keep only the last 7 backups
cd "$BACKUP_DIR"
ls -t | tail -n +8 | xargs -r rm

echo "Backup created: $BACKUP_FILE"
β–Έ Automate with cron
# Make the script executable
sudo chmod +x /usr/local/bin/backup.sh

# Add a cron job (daily backup at 3 AM)
sudo crontab -e
β–Έ Add this line to crontab
0 3 * * * /usr/local/bin/backup.sh

10

Security Checklist

Verify that you have:

βœ… Configured SSH key authentication βœ… Disabled password authentication βœ… Disabled root login βœ… Changed default SSH port βœ… Installed and configured Fail2ban βœ… Properly configured your firewall βœ… Installed audit tools (Lynis, RKHunter) βœ… Enabled automatic updates βœ… Configured regular backups βœ… Monitored system logs


πŸ’¬ Need Help?

If you encounter difficulties:


πŸ”’ Congratulations!

Your VPS is now well secured. Security is an ongoing process: regularly monitor your logs and keep your system updated!

Tags

#vps#linux#security#fail2ban#ssh
Questions? Let’s chat.
contact [at] atmosnode [dot] com
Live Chat
AtmosNode
Β© 2025 AtmosNode.com β€’ Company number: 899 139 182
Loading...
Company not subject to VAT

πŸ”’ Secure Payment Methods (Crypto accepted)

Visa
MasterCard
PayPal
Apple Pay
Google Pay