SSH Security Hardening: Protecting Your VPS from Attacks

Published on

Advanced SSH security configuration for VPS servers. Covers key management, port knocking, 2FA, jump hosts, and monitoring SSH access attempts.

Written by Jochem, Infrastructure Expert, 5-10 years experience in game server hosting, VPS infrastructure, and 24/7 streaming solutions. Read author bio →

SSH is your front door. Every VPS on the internet faces thousands of SSH brute force attempts daily. Here's how to make your door unbreakable.

SSH security and server hardening

The Threat

A new VPS receives its first SSH brute force attempt within minutes of going online:

MetricTypical Value
Login attempts per day1,000-10,000+
Unique attacker IPs per day50-200
Common usernames triedroot, admin, ubuntu, test
Common passwords tried123456, password, admin

Layer 1: Key-Based Authentication

Disable password authentication entirely:

# /etc/ssh/sshd_config
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
PubkeyAuthentication yes

With passwords disabled, brute force attacks become meaningless.

Key Management

Key TypeSecurityRecommendation
RSA 2048AdequateMinimum acceptable
RSA 4096GoodGood choice
Ed25519ExcellentBest choice
ECDSAGoodGood choice

Generate an Ed25519 key:

ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/vps_key

Layer 2: SSH Configuration Hardening

# /etc/ssh/sshd_config

# Change default port
Port 2222

# Only allow specific users
AllowUsers yourusername

# Disable root login
PermitRootLogin no

# Limit authentication attempts
MaxAuthTries 3

# Disconnect idle sessions
ClientAliveInterval 300
ClientAliveCountMax 2

# Disable X11 forwarding (if not needed)
X11Forwarding no

# Disable agent forwarding (if not needed)
AllowAgentForwarding no

# Use only Protocol 2
Protocol 2

Layer 3: Fail2Ban Advanced Configuration

# /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
findtime = 300
bantime = 86400
SettingValueEffect
maxretry: 33 attemptsLow tolerance
findtime: 3005 minutesShort window
bantime: 8640024 hoursLong ban

Monitor bans:

sudo fail2ban-client status sshd

Layer 4: Two-Factor Authentication

Add TOTP (Google Authenticator) to SSH:

sudo apt install libpam-google-authenticator
google-authenticator

Configure PAM:

# /etc/pam.d/sshd
auth required pam_google_authenticator.so

Configure SSH:

# /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
AuthenticationMethods publickey,keyboard-interactive

Now login requires both your SSH key and a TOTP code.

Layer 5: Port Knocking (Advanced)

Hide your SSH port behind a knock sequence:

sudo apt install knockd
# /etc/knockd.conf
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 10
command = /sbin/iptables -A INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
tcpflags = syn

[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 10
command = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 2222 -j ACCEPT
tcpflags = syn

To connect: knock on ports 7000, 8000, 9000 in sequence, then SSH connects.

Monitoring

Log Analysis

# Failed login attempts
grep "Failed password" /var/log/auth.log | tail -20

# Successful logins
grep "Accepted" /var/log/auth.log | tail -20

# Currently banned IPs
sudo fail2ban-client status sshd

Real-Time Monitoring

# Watch auth log in real time
tail -f /var/log/auth.log | grep ssh

Security Checklist

LayerProtectionStops
Key-only auth99% of brute forcePassword guessing
Non-standard port90% of automated scannersScript kiddies
Fail2banRepeated offendersPersistent attackers
2FACompromised key scenarioStolen keys
Port knockingPort detectionPort scanners

You don't need all five layers. Key-only auth + Fail2ban + non-standard port stops 99.9% of attacks. Add 2FA for critical servers.

Space-Node's VPS hosting gives you full SSH control from deployment. KVM isolation means your SSH configuration can't be affected by other users, and the clean OS installation lets you harden from a known-good state.

Jochem

About the Author

Jochem, Infrastructure Expert, expert in game server hosting, VPS infrastructure, and 24/7 streaming solutions with 5-10 years experience.

Since 2023
500+ servers hosted
4.8/5 avg rating

I specialize in Minecraft, FiveM, Rust, and 24/7 streaming infrastructure, operating enterprise-grade AMD Ryzen 9 hardware in Netherlands datacenters.

View my full bio and credentials →

Launch Your VPS Today

Get started with professional VPS hosting powered by enterprise hardware. Instant deployment and 24/7 support included.