متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 78Security · Fail2ban

How to block brute-force logins with Fail2ban

Bots try thousands of passwords against SSH, WordPress logins, mail servers and APIs. Fail2ban reads the logs, and when one IP fails too often it is banned in the firewall for a while. This how-to turns on the SSH jail with good limits, never bans your own office, adds a jail for Nginx, and shows how to see and remove bans.

  • 5 الدروس
  • 8 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

78.1

The quick answer

dnf -y install epel-release && dnf -y install fail2ban fail2ban-firewalld
apt-get install -y fail2ban
/etc/fail2ban/jail.local
[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    = aggressive
systemctl enable --now fail2ban
fail2ban-client status sshd

78.2

Step by step

  1. Install (quick answer: RHEL needs EPEL; fail2ban-firewalld makes it ban with firewalld). On Ubuntu with UFW add banaction = ufw to [DEFAULT].
  2. Never ban yourself — put your office/VPN IPs and the monitoring server in ignoreip.
  3. Understand the limits: 5 failures (maxretry) within 10 minutes (findtime) → ban for 1 hour (bantime); repeat offenders get longer bans (bantime.increment). mode = aggressive also catches other SSH attack patterns.
  4. 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 ...
  1. Unban (a customer or a colleague locked out):
fail2ban-client set sshd unbanip 198.51.100.23
fail2ban-client unban --all
  1. Protect a web login too — a jail for Nginx HTTP auth (or your app's login log with your own filter):
/etc/fail2ban/jail.d/nginx.local
[nginx-http-auth]
enabled  = true
logpath  = /var/log/nginx/error.log
backend  = auto
maxretry = 5

Test 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
  1. 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.

78.3

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 f2b

78.4

Common problems

ProblemCauseFix
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 bannedFilter does not match the log lines.fail2ban-regex LOG FILTER; check the log format.
You banned yourselfYour IP is not in ignoreip.Unban from the console; add your IP.
Only the proxy/load balancer IP is bannedLogs show the proxy, not the client.Log the real client IP (X-Forwarded-For / real_ip module).
Bans gone after restartNormal for short bans.Use bantime.increment; for permanent blocks use the firewall itself.
IPv6 attacks not bannedOld version / action without IPv6.Update Fail2ban; use firewalld/nftables actions.

78.5

Go further

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.

كل الأوامر في هذا الفصل