You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fail2Ban is a useful tool that analyses server log files for recurring patterns of failures. This allows blocking IPs trying to run brute force attacks against a server.
18
-
19
-
In this tutorial, you will learn how to configure the service on an Ubuntu Bionic server to protect the SSH service. Fail2Ban can be used with all services generating log files.
16
+
Fail2Ban is a powerful tool that analyzes server log files for recurring patterns of failed login attempts, enabling the blocking of IPs attempting brute force attacks against a server.
17
+
In this tutorial, you will learn how to configure Fail2Ban on an Ubuntu 24.04 LTS (Noble Numbat) server to protect the SSH service. Fail2Ban can be used with any service that generates log files.
20
18
21
19
<Requirements />
22
20
@@ -28,79 +26,87 @@ In this tutorial, you will learn how to configure the service on an Ubuntu Bioni
28
26
29
27
## Installing Fail2Ban
30
28
31
-
1.The required packages are available in the repositories of Ubuntu and can be installed with `apt`:
32
-
```
29
+
1.Install Fail2Ban and Postfix (optional, for email notifications) using the package manager:
30
+
```bash
33
31
sudo apt-get install fail2ban postfix
34
32
```
35
-
2.Choose `Internet Site` when asked for the configuration:
33
+
2.During Postfix installation, select **Internet Site** when prompted for configuration.
36
34
<Lightboximage={image}alt="" />
37
-
3. Once the installation has completed, open the file `/etc/aliases` and add the following line:
35
+
36
+
3. After installation, edit `/etc/aliases` to configure email notifications:
37
+
```bash
38
+
sudo nano /etc/aliases
38
39
```
40
+
41
+
4. Add the following line, replacing [email protected] with your email address:
Make sure to replace `[email protected]` with your actual email address.
43
-
44
-
<Messagetype="tip">
45
-
To receive notifications by email, it is required that the [email ports are unlocked](/instances/how-to/send-emails-from-your-instance/).
46
-
</Message>
46
+
5. Save the file, exit nano and run the following command:
47
+
```bash
48
+
sudo newaliases
49
+
```
50
+
<Messagetype="note">
51
+
- To receive email notifications, ensure outbound email ports (e.g., 25, 587) are open on your server.
52
+
- Postfix is optional. Alternatives like `ssmtp` or external SMTP services can be used for notifications.
53
+
</Message>
47
54
48
55
## Configuring Fail2Ban
49
56
50
-
1.Start by copying the configuration file:
51
-
```
57
+
1.Copy the default configuration file to create a custom configuration:
58
+
```bash
52
59
cd /etc/fail2ban && sudo cp jail.conf jail.local
53
60
```
61
+
The jail.local file overrides jail.conf for custom settings, preserving the default configuration.
54
62
55
-
The file `jail.conf` contains the default parameters. If a file `jail.local` is available, it will have priority over `jail.conf` if parameters are modified.
56
-
2. Edit the file `/etc/fail2ban/jail.local` with your preferred editor.
57
-
58
-
Following are the parameters which should be modified:
59
-
-`ignoreip = 127.0.0.1/8` - By default the IPs of localhost are ignored, self-banning would not be very useful. It is possible to exclude other IPs from being banned.
60
-
-`bantime = 600` - The duration of a ban. By default, it is set to 10 Minutes. The value has to be specified in seconds and it is recommended to set it at least to one hour, or one day.
61
-
-`findtime = 600` - The timespan which will be considered for maxretry. If you want for example to ban somebody who made more than 3 malicious attempts during the last hour or, as here, in the last 10 minutes.
62
-
-`maxretry = 3` - Amount of attempts before being banned.
63
-
-`destemail = root@localhost` - The recipient of the mail. As an alias for root has been set during the installation, this value can be left as it is.
64
-
-`sendername = Fail2Ban` - The name of the sender of the mail.
65
-
-`action = %(action_)s` - This defines the action to execute when a limit is reached.
66
-
By default, it will only block the user.
67
-
68
-
To receive an email at each ban, set it to:
69
-
70
-
-`action = %(action_mw)`
71
-
72
-
To receive the logs with the mail, set it to:
73
-
74
-
-`action = %(action_mwl)`
75
-
76
-
Further down in the configuration file, it comes to the “Jails”. These are configurable blocks per service to filter logs and ban in cases where patterns are matched.
77
-
As a minimum, it is recommended to activate the jail ssh as follows:
78
-
```
79
-
[ssh]
80
-
81
-
enabled = true
82
-
port = ssh
83
-
filter = sshd
84
-
logpath = /var/log/auth.log
85
-
```
86
-
87
-
<Messagetype="tip">
88
-
If your SSH daemon is listening on multiple ports or a different port, you have to modify the line port with the correct parameters:
89
-
For example:
90
-
```
91
-
port = ssh,1234
92
-
```
93
-
Fail2Ban analyses the logs and will ban the users who made several intrusion attempts on ports 22 (SSH by default) & 1234.
94
-
</Message>
95
-
3. Save the file once you have edited it.
96
-
97
-
Fail2Ban uses filters, pre-made configuration files indicating what to parse in a log.
98
-
99
-
They can be found in `/etc/fail2ban/filter.d`.
100
-
You can create your own filters in case you need to.
101
-
4. Restart the service to take the actions into effect:
63
+
2. Edit `/etc/fail2ban/jail.local` with your preferred editor (e.g., nano):
64
+
```bash
65
+
sudo nano /etc/fail2ban/jail.local
66
+
```
67
+
Modify the following parameters:
68
+
```bash
69
+
ignoreip = 127.0.0.1/8 - Ignores localhost IPs to prevent self-banning. Add other trusted IPs if needed (e.g., 127.0.0.1/8 192.168.1.0/24).
70
+
bantime = 3600 - Duration of a ban, set to 1 hour (3600 seconds) by default in newer versions. Consider increasing to 86400 (1 day) for stronger protection.
71
+
findtime = 3600 - Time window for counting failed attempts (1 hour). Adjust to 600 (10 minutes) for stricter monitoring if preferred.
72
+
maxretry = 5 - Number of failed attempts before a ban. The default in Ubuntu 24.04 is 5.
73
+
destemail = root@localhost - Email recipient for notifications. Leave as is if /etc/aliases is configured.
74
+
sendername = Fail2Ban - Sender name for notification emails.
75
+
banaction = nftables[multiport] - Default ban action using nftables, which is preferred in Ubuntu 24.04 over iptables.
76
+
action = %(action_mwl)s - Sends email with logs when banning. Use %(action_mw)s for email without logs, or %(action_)s for no email.
102
77
```
103
-
sudo service fail2ban restart
78
+
79
+
3. Enable the SSH jail by ensuring the following configuration is present:
80
+
```bash
81
+
[sshd]
82
+
enabled = true
83
+
port = ssh
84
+
filter = sshd
85
+
logpath = /var/log/auth.log
86
+
```
87
+
If your SSH service uses a non-standard port, update the `port` line. For example, for ports 22 and 1234:
88
+
```
89
+
port = ssh,1234
90
+
```
91
+
Fail2Ban will monitor the specified ports for intrusion attempts.
92
+
<Message type="tip">
93
+
For systems using `systemd` logging (e.g., Proxmox), use:
94
+
```
95
+
[sshd]
96
+
enabled = true
97
+
port = ssh
98
+
filter = sshd
99
+
logpath = %(sshd_log)s
100
+
backend = systemd
101
+
```
102
+
</Message>
103
+
104
+
4. Save the file.
105
+
Fail2Ban uses filter files in`/etc/fail2ban/filter.d` to parse logs. The `sshd` filter is pre-configured for SSH. Custom [filters](https://fail2ban.readthedocs.io/en/latest/filters.html) can be created for other services.
106
+
107
+
5. Restart Fail2Ban to apply changes:
108
+
```bash
109
+
sudo systemctl restart fail2ban
104
110
```
105
111
106
-
The service will now analyze the connections made to the SSH service. The logs of Fail2Ban are located in the file `var/log/fail2ban.log`.
112
+
Fail2Ban will now monitor SSH connections. Check logs at `/var/log/fail2ban.log`for activity.
0 commit comments