systems:media_server:secure_the_server:install_fail2ban
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
systems:media_server:secure_the_server:install_fail2ban [2025/05/30 20:30] – peter | systems:media_server:secure_the_server:install_fail2ban [2025/05/30 21:31] (current) – peter | ||
---|---|---|---|
Line 22: | Line 22: | ||
---- | ---- | ||
- | |||
- | ===== Configure Fail2Ban ===== | ||
- | |||
- | The default configuration is defined in **/ | ||
- | |||
- | <WRAP important> | ||
- | **WARNING: | ||
- | </ | ||
- | |||
- | Here are salient lines from the default configuration: | ||
- | |||
- | <file bash / | ||
- | # line 87 : ignore your own local IP | ||
- | #ignoreself = true | ||
- | |||
- | # line 92 : possible to add ignored networks | ||
- | #ignoreip = 127.0.0.1/8 ::1 | ||
- | |||
- | # line 101 : number of seconds that a host is banned | ||
- | # - 1m ⇒ 1 minutes | ||
- | # - 1h ⇒ 1 houer | ||
- | # - 1d ⇒ 1 day | ||
- | # - 1mo ⇒ 1 month | ||
- | # - 1y ⇒ 1 year | ||
- | bantime | ||
- | |||
- | # line 105 : A host is banned if it has generated " | ||
- | findtime | ||
- | |||
- | # line 108 : " | ||
- | maxretry = 5 | ||
- | |||
- | # line 178 : destination email address if enabling email notification | ||
- | destemail = root@localhost | ||
- | |||
- | # line 181 : sender address if enabling email notification | ||
- | sender = root@< | ||
- | |||
- | # line 263 : default action | ||
- | # - %(action_)s ⇒ ban only | ||
- | # - %(action_mw)s ⇒ band and email notification (includes Whois info) | ||
- | # - %(action_mwl)s ⇒ band and email notification (includes Whois info and logs) | ||
- | action = %(action_)s | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Override the default values ===== | ||
- | |||
- | As root, create a **/ | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | ignoreip = 127.0.0.1/8 ::1 | ||
- | bantime | ||
- | findtime | ||
- | maxretry = 5 | ||
- | destemail = root@localhost | ||
- | sender = root@mediaserver | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Restart Fail2Ban ===== | ||
- | |||
- | <code bash> | ||
- | sudo systemctl restart fail2ban | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Verify Fail2Ban ===== | ||
- | |||
- | <code bash> | ||
- | sudo systemctl status fail2ban | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | < | ||
- | fail2ban.service - Fail2Ban Service | ||
- | | ||
- | | ||
- | Docs: man: | ||
- | Main PID: 3108 (fail2ban-server) | ||
- | Tasks: 5 (limit: 154383) | ||
- | | ||
- | CPU: 110ms | ||
- | | ||
- | | ||
- | |||
- | May 30 19:38:01 mediaserver systemd[1]: Started fail2ban.service - Fail2Ban Service. | ||
- | May 30 19:38:02 mediaserver fail2ban-server[3108]: | ||
- | May 30 19:38:02 mediaserver fail2ban-server[3108]: | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Check the fail2ban status ===== | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client status | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | < | ||
- | Status | ||
- | |- Number of jail: 1 | ||
- | `- Jail list: sshd | ||
- | </ | ||
<WRAP info> | <WRAP info> | ||
- | **NOTE: | + | **NOTE: |
- | </ | + | |
- | ---- | + | |
- | + | ||
- | ===== Check the fail2ban status of the sshd service ===== | + | |
- | + | ||
- | <code bash> | + | |
- | sudo fail2ban-client status sshd | + | |
- | </ | + | |
- | + | ||
- | returns: | + | |
- | + | ||
- | < | + | |
- | Status for the jail: sshd | + | |
- | |- Filter | + | |
- | | |- Currently failed: 0 | + | |
- | | |- Total failed: 0 | + | |
- | | `- Journal matches: | + | |
- | `- Actions | + | |
- | | + | |
- | |- Total banned: 0 | + | |
- | `- Banned IP list: | + | |
- | </ | + | |
- | + | ||
- | <WRAP info> | + | |
- | **NOTE: | + | |
- | + | ||
- | The actual ban action is controlled by nftables. | + | |
- | + | ||
- | * Check the nft status with: <code bash> | + | |
- | sudo nft list ruleset | + | |
- | </ | + | |
</ | </ | ||
Line 173: | Line 32: | ||
---- | ---- | ||
- | ===== To manually add banned hosts ===== | + | ===== References |
- | <code bash> | + | [[Ubuntu: |
- | sudo fail2ban-client set sshd banip 1.2.3.4/28 | + | |
- | </ | + | |
- | <WRAP info> | ||
- | **NOTE: | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== To manually add banned hosts ===== | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client set sshd unbanip 1.2.3.4 | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== To remove all banned hosts ===== | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client unban --all | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Have fail2ban monitor other services ===== | ||
- | |||
- | By default, fail2ban only monitors sshd. | ||
- | |||
- | Confirm this: | ||
- | |||
- | <code bash> | ||
- | ll / | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | drwxr-xr-x 2 root root 4096 May 30 19:31 ./ | ||
- | drwxr-xr-x 6 root root 4096 May 30 19:37 ../ | ||
- | -rw-r--r-- 1 root root 117 Jun 10 2024 defaults-debian.conf | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== View this file ==== | ||
- | |||
- | <code bash> | ||
- | most / | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | banaction = nftables | ||
- | banaction_allports = nftables[type=allports] | ||
- | backend = systemd | ||
- | |||
- | [sshd] | ||
- | enabled = true | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | |||
- | * This also shows that the actual ban action is controlled by nftables. | ||
- | |||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Override the default values for a specific service ==== | ||
- | |||
- | Edit that **/ | ||
- | |||
- | * As the only service currently being monitored is sshd, for now changes will be overridden for sshd. | ||
- | |||
- | <file bash / | ||
- | [DEFAULT] | ||
- | banaction = nftables | ||
- | banaction_allports = nftables[type=allports] | ||
- | backend = systemd | ||
- | |||
- | [sshd] | ||
- | enabled = true | ||
- | |||
- | # override the default values for the sshd service | ||
- | bantime = 600 | ||
- | findtime = 3m | ||
- | maxretry = 5 | ||
- | action = %(action_mw)s | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Enable the new changes ==== | ||
- | |||
- | Reload fail2ban to enable the changes. | ||
- | |||
- | <code bash> | ||
- | sudo systemctl reload fail2ban | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Check what other services could be monitored by fail2ban ==== | ||
- | |||
- | <code bash> | ||
- | grep ' | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | < | ||
- | [sshd] | ||
- | [dropbear] | ||
- | [selinux-ssh] | ||
- | [apache-auth] | ||
- | [apache-badbots] | ||
- | [apache-noscript] | ||
- | [apache-overflows] | ||
- | [apache-nohome] | ||
- | [apache-botsearch] | ||
- | [apache-fakegooglebot] | ||
- | [apache-modsecurity] | ||
- | [apache-shellshock] | ||
- | [openhab-auth] | ||
- | [nginx-http-auth] | ||
- | [nginx-limit-req] | ||
- | [nginx-botsearch] | ||
- | [nginx-bad-request] | ||
- | [php-url-fopen] | ||
- | [suhosin] | ||
- | [lighttpd-auth] | ||
- | ... | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== To monitor another service ==== | ||
- | |||
- | Create a new file under **/ | ||
- | |||
- | For example, to monitor Vsftpd, create a new file **/ | ||
- | |||
- | <file bash / | ||
- | [vsftpd] | ||
- | enabled = true | ||
- | backend = systemd | ||
- | journalmatch = _SYSTEMD_UNIT=vsftpd.service | ||
- | action = %(action_mw)s | ||
- | </ | ||
- | |||
- | <WRAP info> | ||
- | **NOTE: | ||
- | |||
- | * **[vsftpd]** - is the name of a service, from one of the services that fail2ban can monitor. | ||
- | |||
- | The actual name of the file under **/ | ||
- | |||
- | * It is the contents of the file that are used. | ||
- | |||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Enable the new changes ==== | ||
- | |||
- | Reload fail2ban to enable the new service to be monitored. | ||
- | |||
- | <code bash> | ||
- | sudo systemctl reload fail2ban | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Check the fail2ban-client status ==== | ||
- | |||
- | <code bash>` | ||
- | sudo fail2ban-client status | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | < | ||
- | Status | ||
- | |- Number of jail: 2 | ||
- | `- Jail list: sshd, vsftpd | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Verify settings for the new service being monitored ==== | ||
- | |||
- | <code bash> | ||
- | sudo fail2ban-client status vsftpd | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | < | ||
- | Status for the jail: vsftpd | ||
- | |- Filter | ||
- | | |- Currently failed: 0 | ||
- | | |- Total failed: | ||
- | | `- Journal matches: | ||
- | `- Actions | ||
- | |- Currently banned: 1 | ||
- | |- Total banned: | ||
- | `- Banned IP list: | ||
- | </ | ||
- | |||
- | ---- | ||
systems/media_server/secure_the_server/install_fail2ban.1748637006.txt.gz · Last modified: 2025/05/30 20:30 by peter