Brute-force attacks on SSH (Secure Shell) are a common method used by attackers to gain unauthorized access to servers. These attacks involve systematically trying a large number of username and password combinations until the correct credentials are found. Fortunately, several effective techniques and tools can prevent brute-force attacks and secure your server.
In this guide, we’ll cover step-by-step methods to block brute-force SSH attacks, including configuring fail2ban, using iptables, changing the default SSH port, enabling key-based authentication, and implementing advanced security measures.
By default, SSH listens on port 22, which makes it an easy target. Changing the port to a non-standard port reduces the likelihood of brute-force attempts.
sudo nano /etc/ssh/sshd_config
2. Locate the line
#Port 22
3. Uncomment and change the port number to a high, unused port, e.g., 22000:
Port 22000
4. Save and exit the file.
5. Restart the SSH service to apply changes:
sudo systemctl restart sshd
6. Update firewall rules to allow traffic on the new port:
sudo ufw allow 22000/tcp
Fail2ban is an intrusion prevention tool that monitors system logs and bans IP addresses exhibiting malicious behavior.
sudo apt update
sudo apt install fail2ban -y
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled = true
port = 22000
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
bantime = 600
findtime = 300
maxretry Locate of failed attempts before banning
bantime Duration (in seconds) to ban the IP
findtime Period (in seconds) to monitor for failed attempts
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
iptables is a powerful Linux firewall that can block unwanted traffic.
sudo iptables -A INPUT -p tcp --dport 22000 -m state --state NEW -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22000 -m recent --set --name SSH
sudo iptables -A INPUT -p tcp --dport 22000 -m recent --update
--seconds 60 --hitcount 3 --rttl --name SSH -j DROP
sudo iptables-save > /etc/iptables/rules.v4
sudo apt install iptables-persistent
sudo iptables -L
sudo fail2ban-client status sshd
ssh-keygen -t rsa -b 4096
ssh-copy-id -p 22000 user@your_server_ip
cat ~/.ssh/id_rsa.pub | ssh -p 22000 user@your_server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
sudo nano /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes
sudo systemctl restart sshd
Restricting SSH access to specific IPs significantly reduces the attack surface.
sudo nano /etc/hosts.allow
sshd: 192.168.1.10
sshd: 203.0.113.25
sudo nano /etc/hosts.deny
sshd: ALL
sudo systemctl restart sshd
TCP wrappers provide another layer of control over SSH access.
sudo nano /etc/hosts.allow
sshd: 192.168.1.0/24
sudo nano /etc/hosts.deny
sshd: ALL
sudo systemctl restart sshd
Adding an extra layer of security with 2FA enhances the protection of your SSH service.
sudo apt install libpam-google-authenticator
google-authenticator
sudo nano /etc/pam.d/sshd
auth required pam_google_authenticator.so
sudo nano /etc/ssh/sshd_config
ChallengeResponseAuthentication yes
sudo systemctl restart sshd
Limiting SSH connection rates reduces brute-force attempts.
sudo ufw enable
sudo ufw limit 22000/tcp
sudo ufw reload
sudo ufw status
sudo nano /etc/ssh/sshd_config
PermitRootLogin no
sudo systemctl restart sshd
LoginGraceTime 60