fail2ban¶
Install¶
Install with your package manager.
Verify
Configuration¶
Configure fail2ban to ban after several unsuccessful SSH connections attemps.
Uncomment/add the following entry for sshd
:
Restart the fail2ban daemon.
Test that the configuration worked.
Use a throwaway machine to try connecting to the host unsuccessfully 6 times.
If successful, you will need to ^C
the process.
Then go back to the host with fail2ban
on it and check the logs.
Check the banned IP:
This will show a list of IPs that are banned (from SSH).Unbanning an IP¶
The fail2ban-client
command is provided to configure and control the fail2ban
server.
We can use this to unban an IP.
Replace the<banned_IP>
with the IP you want to unban.
It should output 1
if successful.
Test:
Integraging with Loki/Promtail¶
The fail2ban
tool can be integrated with logging tools.
For example, using Grafana (visualizations) with Loki and Telegraf.
Install the tools (Grafana, Loki, Promtail) -- see installing monitoring tools
In your Promtail config, add a rule for scraping and pushing fail2ban logs.
- job_name: fail2ban
static_configs:
- targets:
- localhost
labels:
job: fail2ban
__path__: /var/log/fail2ban.log
Setting up Alerts with Telegraf, InfluxDB, and Grafana¶
# Install InfluxDB
wget -q https://repos.influxdata.com/influxdata-archive_compat.key
echo '393e8779c89ac8d958f81f942f9ad7fb82a25e133faddaf92e15b16e6ac9ce4c influxdata-archive_compat.key' | sha256sum -c && cat influxdata-archive_compat.key | gpg --dearmor | tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null
echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | tee /etc/apt/sources.list.d/influxdata.list
apt-get update && apt-get -y install influxdb2
systemctl enable --now influxdb
http://localhost:8086
) and set up a bucket.
Add:
- Username
- password
- Organization name
- Bucket name
The bucket name is what you'll use in your queries.
a238XZQivdW16OBDdrk3YS7zeoTCUQvThq0YuHry4VW8JKcLoxKW-c9VqwOcU1nQ_7b1320wY51o1R8QJSEGLg==