How to Protect Your Linux Server from Brute Force SSH Attacks
Posted By
kamlesh paulon
Dec 13, 2024Brute Force SSH Attacks are among the most common threats to Linux servers. Cybercriminals use automated tools to guess passwords and gain unauthorized access. In this article, we’ll explore effective strategies to safeguard your Linux server from these attacks.
Table of contents
- 1. Disable Root Login
- 2. Implement Public Key Authentication
- 3. Use Fail2Ban
- 4. Monitor Login Attempts
1. Disable Root Login
Why Disable Root Login?
- Allowing root login via SSH can make it easier for attackers to gain full access. Instead, use a regular user account with sudo privileges.
How to Disable Root Login
- To disable root login, edit the SSH configuration file:
- Find the line:
- Change it to:
- Then, restart the SSH service to apply the changes:
2. Implement Public Key Authentication
Why Use Public Key Authentication?
- Public key authentication is more secure than password-based logins. It relies on a pair of cryptographic keys, making it much harder for attackers to compromise
How to Set Up Public Key Authentication
- Generate SSH Keys (Local PC):
- Copy the Public Key to the Server (Local PC):
Disable Password Authentication (Server) : Edit the SSH configuration file:
- Find and change:
to
Finally, restart SSH to apply the changes:
3. Use Fail2Ban
What is Fail2Ban?
- Fail2Ban is a security tool that scans log files and bans IP addresses with too many failed login attempts, significantly reducing the risk of brute force attacks.
How to Install and Configure Fail2Ban
- Install Fail2Ban:
- Configure Fail2Ban for SSH: Open the configuration file:
Add the following lines:
- Start and Enable Fail2Ban:
- Check Banned IP List:
- Add an IP to the Ban List:
Replace
<IP>
with the actual IP address you want to ban.
- Remove an IP from the Ban List:
Replace
<IP>
with the IP address you wish to unban.
4. Monitor Login Attempts
Why Monitor Login Attempts?
Regularly reviewing login attempts can help you identify and respond to potential threats quickly.
Tools for Monitoring
- SSH Logs: Check logs at
/var/log/auth.log
for unauthorized access attempts:
You can also view recent activity with the following command:
Share this article
41 views