How to Configure Fail2Ban to Prevent Brute-Force Attacks on Your Linux Server
Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
Search for a command to run...
Hi there! I'm Dinesh, a passionate Cloud and DevOps enthusiast. I love to dive into the latest new technologies and sharing my journey through blog.
No comments yet. Be the first to comment.
1. Difference between Docker and Kubernetes Docker → Builds and runs containers.Kubernetes → Orchestrates containers across multiple nodes. Key points: Docker = container runtime. Kubernetes = container orchestration tool. Kubernetes provides auto...
In this session, we learn how to monitor a Kubernetes cluster using Prometheus and Grafana.This is not just theory — there is a GitHub repository containing all installation commands and demo steps.The repo will also be enhanced later with advanced K...
1. What is a ConfigMap in Kubernetes? A ConfigMap is used to store non-sensitive configuration data that your application needs — such as: Database port Connection type Any general configuration values In normal applications (non-Kubernetes), de...
Kubernetes normally supports built-in resources like: Deployment Service Pod ConfigMap Secret Ingress These are called native resources. Sometimes companies (Istio, ArgoCD, Prometheus Operator, Kyverno, etc.) want to add new features that Kub...
1. Why Kubernetes Services Are Needed When a Pod is created in Kubernetes, it receives a dynamic IP address.If the Pod dies and restarts, its IP changes.So other Pods (like checkout → payments) cannot rely on Pod IP because it changes, creating issue...
When managing a Linux server, security is paramount. One of the most common types of attacks servers face is brute-force attempts on login services, especially SSH. Fail2Ban is an effective, open-source tool that automatically bans suspicious IPs after repeated failed login attempts, adding a crucial layer of security to your server. This guide will walk you through configuring Fail2Ban on your Linux server.
Fail2Ban is a tool that monitors your server’s log files for failed login attempts and other suspicious activities. When it detects too many failed attempts from a single IP address within a specified timeframe, it blocks that IP, effectively preventing brute-force attacks.
Fail2Ban can protect multiple services and is highly configurable, making it an essential addition to your server’s security setup.
Before configuring Fail2Ban, you’ll need to install it on your server. Fail2Ban is available in most Linux distributions’ repositories.
sudo apt update
sudo apt install fail2ban
sudo yum install epel-release
sudo yum install fail2ban
After installation, start the Fail2Ban service and enable it to start at boot:
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
Fail2Ban's main configuration file is located at /etc/fail2ban/jail.conf. To keep this file intact, create a local configuration file instead by copying it to jail.local:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Open the jail.local file for editing:
sudo nano /etc/fail2ban/jail.local
In the jail.local file, you’ll want to customize the following settings to enhance server protection:
Ignore IPs: Define trusted IPs that should never be banned, such as your own IP.
ignoreip = 127.0.0.1/8 ::1 192.168.1.1
Ban Time: Set how long an IP remains banned (in seconds). For example, 600 will ban the IP for 10 minutes.
bantime = 600
Find Time and Max Retry: These parameters work together to decide when an IP should be banned. findtime is the time window (in seconds) in which Fail2Ban counts failed attempts, and maxretry sets the allowed number of failed attempts within this period.
findtime = 600
maxretry = 5
Fail2Ban comes with a default filter for SSH, making it straightforward to secure your SSH service from brute-force attacks.
In the jail.local file, find the [sshd] section and enable it by setting enabled to true:
[sshd]
enabled = true
port = 2222 # Update this if you have changed the default SSH port
logpath = /var/log/auth.log # Log file for Debian/Ubuntu
# logpath = /var/log/secure # Log file for CentOS/RHEL
maxretry = 5
Ensure that port and logpath match your SSH settings.
Once you’ve made changes to the configuration, restart Fail2Ban to apply them:
sudo systemctl restart fail2ban
To verify Fail2Ban is running and protecting SSH, you can check its status:
sudo fail2ban-client status
Fail2Ban provides commands for monitoring and managing bans. Here are some useful ones:
View Banned IPs for SSH:
sudo fail2ban-client status sshd
Unban an IP Address:
sudo fail2ban-client set sshd unbanip <IP_ADDRESS>
Replace <IP_ADDRESS> with the actual IP you wish to unban.
Fail2Ban can send you an email when it bans an IP. To set this up, open jail.local and update the following settings:
Email Settings:
destemail = your_email@example.com
sendername = Fail2Ban
mta = sendmail
action = %(action_mw)s # Action for Ban & Email notification
Make sure your server is configured to send emails, either with sendmail, postfix, or another service.
With Fail2Ban configured, your server now has an automated defense system that monitors for malicious IPs and protects against brute-force attacks. This setup provides peace of mind, knowing your server has a robust layer of protection.
Fail2Ban is a flexible tool, and by adjusting its configurations, you can expand its monitoring to other services on your server, making it an indispensable part of your security toolkit.