TL;DR
You can’t completely prevent a brute force attack on a shell, but you can make it incredibly difficult and time-consuming for attackers. This guide shows how to use tools like fail2ban, strong passwords, key-based authentication, account lockouts, and limiting login attempts.
How to Prevent Shell Brute Force Attacks
- Use Strong Passwords: This is the first line of defence.
- Passwords should be long (12+ characters).
- Mix uppercase and lowercase letters, numbers, and symbols.
- Avoid dictionary words or personal information.
- Use a password manager to generate and store strong passwords.
- Disable Password Authentication: If possible, switch to key-based authentication (see step 3). This removes the possibility of password guessing.
- Edit your SSH configuration file:
sudo nano /etc/ssh/sshd_config - Find the line
PasswordAuthentication yesand change it toPasswordAuthentication no. - Restart the SSH service:
sudo systemctl restart sshd
- Edit your SSH configuration file:
- Implement Key-Based Authentication: This is much more secure than passwords.
- Generate a key pair on your client machine (e.g., using
ssh-keygen). - Copy the public key to the server’s
~/.ssh/authorized_keysfile for the user you want to log in as. Usessh-copy-id user@server_ip.
- Generate a key pair on your client machine (e.g., using
- Account Lockout Policies: Prevent repeated failed login attempts.
- Using PAM (Pluggable Authentication Modules), you can configure account lockouts. Edit the file
sudo nano /etc/pam.d/common-auth.
- Add this line to the end of the file:
auth required pam_tally2.so deny=3 unlock_time=600This will lock an account after 3 failed attempts for 10 minutes (600 seconds).
- To check locked accounts:
sudo pam_tally2 --user <username> - To unlock an account:
sudo pam_tally2 --user <username> --reset
- Using PAM (Pluggable Authentication Modules), you can configure account lockouts. Edit the file
- Use Fail2ban: This tool monitors log files for malicious activity and automatically blocks IPs.
- Install fail2ban:
sudo apt update && sudo apt install fail2ban(Debian/Ubuntu) or
sudo yum install fail2ban(CentOS/RHEL).
- Configure Fail2ban for SSH. Edit the file
sudo nano /etc/fail2ban/jail.local. If it doesn’t exist, copy from jail.conf:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local - Enable the SSH jail by setting
enabled = trueunder the [sshd] section. Adjust other settings as needed (e.g.,bantime,findtime,maxretry). - Restart Fail2ban:
sudo systemctl restart fail2ban - Check the status of jails:
sudo fail2ban-client status sshd
- Install fail2ban:
- Limit Login Attempts: Configure SSH to limit the number of login attempts per connection.
- Edit your SSH configuration file:
sudo nano /etc/ssh/sshd_config.
- Add or modify these lines:
MaxAuthTries 3ClientAliveInterval 60ClientAliveCountMax 3This limits attempts to 3, and disconnects after 3 missed ‘alive’ signals (every 60 seconds).
- Restart the SSH service:
sudo systemctl restart sshd.
- Edit your SSH configuration file:
- Change Default SSH Port: Changing from port 22 to a non-standard port can reduce automated attacks.
- Edit your SSH configuration file:
sudo nano /etc/ssh/sshd_config.
- Find the line
Port 22and change it to a different port number (e.g.,Port 2222). Choose a port above 1024. - Restart the SSH service:
sudo systemctl restart sshd.
- Remember to update your firewall rules to allow traffic on the new port!
- Edit your SSH configuration file:
- Firewall Configuration: Only allow SSH access from trusted IPs.
- Use a firewall (e.g.,
ufworiptables) to block all incoming connections except those from your known IP addresses. - Example using ufw:
sudo ufw allow from <your_ip> to any port ssh. Then enable the firewall with
sudo ufw enable.
- Use a firewall (e.g.,
Important Note: No system is 100% secure. These steps significantly increase security, but determined attackers may still find ways to compromise your shell. Regularly review logs and keep your system updated.

