هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
dnf -y install epel-release && dnf -y install fail2ban fail2ban-firewalld
apt-get install -y fail2ban[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime = 1h
findtime = 10m
maxretry = 5
bantime.increment = true
backend = systemd
[sshd]
enabled = true
mode = aggressivesystemctl enable --now fail2ban
fail2ban-client status sshdStep by step
- Install (quick answer: RHEL needs EPEL;
fail2ban-firewalldmakes it ban with firewalld). On Ubuntu with UFW addbanaction = ufwto[DEFAULT]. - Never ban yourself — put your office/VPN IPs and the monitoring server in
ignoreip. - Understand the limits: 5 failures (
maxretry) within 10 minutes (findtime) → ban for 1 hour (bantime); repeat offenders get longer bans (bantime.increment).mode = aggressivealso catches other SSH attack patterns. - See what it does:
fail2ban-client status
Jail list: sshd
fail2ban-client status sshd
|- Filter
|- Currently failed: 3
`- Total failed: 1287
`- Actions
|- Currently banned: 12
`- Banned IP list: 198.51.100.23 203.0.113.77 ...- Unban (a customer or a colleague locked out):
fail2ban-client set sshd unbanip 198.51.100.23
fail2ban-client unban --all- Protect a web login too — a jail for Nginx HTTP auth (or your app's login log with your own filter):
[nginx-http-auth]
enabled = true
logpath = /var/log/nginx/error.log
backend = auto
maxretry = 5Test a filter against the real log before you trust it:
fail2ban-regex /var/log/nginx/error.log /etc/fail2ban/filter.d/nginx-http-auth.conf- Fail2ban is a helper, not the wall: with key-only SSH, password guessing can not succeed anyway — Fail2ban mainly cuts the noise and load. Behind a load balancer or CDN, the real client IP must be in the logs, or you ban the proxy.
Check that it worked
fail2ban-client status sshd shows the jail with failed and banned counts, and the firewall shows the bans:
firewall-cmd --list-rich-rules | head
nft list ruleset | grep -A3 f2bCommon problems
| Problem | Cause | Fix |
|---|---|---|
Jail does not start (Have not found any log file) | Wrong log path / backend for this distro. | backend = systemd for SSH; check logpath for others. |
| Nothing is ever banned | Filter does not match the log lines. | fail2ban-regex LOG FILTER; check the log format. |
| You banned yourself | Your IP is not in ignoreip. | Unban from the console; add your IP. |
| Only the proxy/load balancer IP is banned | Logs show the proxy, not the client. | Log the real client IP (X-Forwarded-For / real_ip module). |
| Bans gone after restart | Normal for short bans. | Use bantime.increment; for permanent blocks use the firewall itself. |
| IPv6 attacks not banned | Old version / action without IPv6. | Update Fail2ban; use firewalld/nftables actions. |
Go further
- Command pages: fail2ban-client, firewall-cmd, journalctl.
- Next how-tos: Harden the SSH server, See who logged in and when, Open a port in the firewall, Rate-limit requests in Nginx.
- Full guide: Install and set up Fail2ban. At work: Fail2ban in host security.
1What do maxretry = 5, findtime = 10m and bantime = 1h mean together?
An IP with 5 failures within 10 minutes is banned for 1 hour.
2How do you test if a filter matches your log lines?
fail2ban-regex LOGFILE FILTERFILE.