Back to articles
Security & Anti-DDoS

Hardening Ubuntu 24.04 VPS Against SSH Brute-Force with Key-Only Auth

Detailed guide on configuring SSH key-only authentication, integrating CrowdSec, and using nftables to block brute-force attacks on Ubuntu 24.04 VPS, ensuring maximum security for...

DPTCloud Engineering7 min read
Hardening Ubuntu 24.04 VPS Against SSH Brute-Force with Key-Only Auth

As SSH brute-force attacks on VPS systems continue to rise, securing Ubuntu 24.04 with integrated defenses is essential. This guide details how to lock down SSH on Ubuntu 24.04 using public key authentication, integrate CrowdSec for automated threat detection and blocking, and leverage nftables instead of UFW to build a robust, high-performance firewall.

1. Configure SSH for Key-Only Authentication

Disabling password-based SSH authentication is the first and most critical step to prevent brute-force attacks.

sudo nano /etc/ssh/sshd_config

Modify the following lines in the configuration file:

  • PermitRootLogin: Set to no
  • PasswordAuthentication: Set to no
  • PubkeyAuthentication: Set to yes
  • ChallengeResponseAuthentication: Set to no

After saving, restart the SSH service:

sudo systemctl restart ssh

2. Generate and Install SSH Key on Client and VPS

On your local machine, generate a key pair:

ssh-keygen -t ed25519 -C "[email protected]"

Copy the public key to your VPS:

ssh-copy-id username@your_vps_ip

Ensure the ~/.ssh/authorized_keys file on the VPS has correct permissions (600).

3. Install and Configure CrowdSec

CrowdSec is an automated threat detection and blocking system that leverages community data.

curl -s https://packagecloud.io/install/repositories/crowdsec/crowdsec/script.deb.sh | sudo bash
sudo apt install crowdsec

Install and start the service:

sudo systemctl enable crowdsec
sudo systemctl start crowdsec

CrowdSec will automatically detect and block IP addresses attempting SSH attacks.

4. Replace UFW with nftables for a Stronger Firewall

UFW is a user-friendly interface, but nftables offers more granular control and better performance, especially with many rules.

sudo apt install nftables
sudo systemctl enable nftables
sudo systemctl start nftables

Create a firewall configuration file at /etc/nftables.conf:

table inet filter {
  chain input {
    type filter hook input priority 0;
    policy drop;

    # Allow SSH connections (port 22)
    tcp dport 22 accept

    # Allow established and related connections
    ct state established,related accept

    # Allow ICMP
    icmp type echo-request accept

    # Allow loopback
    ip saddr 127.0.0.1 accept
    ip6 saddr ::1 accept
  }
}

# Block IPs flagged by CrowdSec
include "/var/lib/crowdsec/data/banlist.nft"

Start nftables:

sudo nft -f /etc/nftables.conf

Troubleshooting Common Errors

  • Cannot SSH after configuration: Verify /etc/ssh/sshd_config settings: PasswordAuthentication no and PubkeyAuthentication yes. Ensure the public key is in ~/.ssh/authorized_keys.
  • CrowdSec not blocking attack IPs: Check if the CrowdSec service is running: sudo systemctl status crowdsec. Review logs: sudo journalctl -u crowdsec -f. Ensure the sshd plugin is enabled.
  • UFW and nftables running simultaneously: Disable and stop UFW: sudo ufw disable and sudo systemctl disable ufw.
Important: Before making changes, ensure you have console access (VNC/SSH console) to avoid lockout. Always test configurations before restarting SSH or firewall services.

Related infrastructure & engineering guides: Cloud VPS with L3/L4/L7 Anycast Anti-DDoS · Automating ACME SSL/TLS with ECDSA P-256 and Zero-Downtime Nginx Reload Hooks

DPTCloud Tier III Infrastructure

Ready to deploy on PCIe Gen4 NVMe VPS & 10Gbps Uplinks?

Provision servers in under 60 seconds with edge Anti-DDoS protection and 99.99% SLA uptime.

Related Articles

View all