Skip to content

Commit a0cb44e

Browse files
authored
Merge pull request linode#885 from EdwardAngert/f2b-markdownfix
Fail2ban markdownfix and some light bonus copyedit
2 parents b919674 + 26884d9 commit a0cb44e

File tree

1 file changed

+49
-45
lines changed

1 file changed

+49
-45
lines changed

docs/security/using-fail2ban-for-security.md

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
author:
33
name: Elle Krout
44
email: ekrout@linode.com
5-
description: 'Use Fail2ban to block automated system attacks and further harden your server. Fail2ban monitors system logs for symptoms of an automated attack, bans the IP and alerts you of the attach through email.'
6-
keywords: 'fail2ban'
5+
description: 'Fail2ban monitors system logs for symptoms of an automated attack, bans the IP and alerts you of the attach through email. Use Fail2ban to thwart automated system attacks and further harden your server.'
6+
keywords: 'fail2ban,ip whitelisting,jail.local'
77
alias: ['tools-reference/tools/using-fail2ban-to-block-network-probes/']
88
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
9-
modified: Wednesday, March 8th, 2017
9+
modified: Wednesday, August 23, 2017
1010
modified_by:
1111
name: Linode
1212
published: 'Monday, October 12th, 2015'
1313
title: Using Fail2ban to Secure Your Server
1414
---
1515

16-
Fail2ban is a log-parsing application that monitors system logs for symptoms of an automated attack on your Linode. When an attempted compromise is located, using the defined parameters, Fail2ban will add a new rule to iptables, thus blocking the IP address of the attacker, either for a set amount of time or permanently. Fail2ban will also alert you through email that an attack is occurring.
16+
Fail2ban is a log-parsing application that monitors system logs for symptoms of an automated attack on your Linode. When an attempted compromise is located, using the defined parameters, Fail2ban will add a new rule to iptables to block the IP address of the attacker, either for a set amount of time or permanently. Fail2ban can also alert you through email that an attack is occurring.
1717

1818
![Using Fail2ban to secure your server](/docs/assets/fail2ban_tg.png "Using Fail2ban to secure your server")
1919

@@ -27,7 +27,7 @@ Fail2ban is primarily focused on SSH attacks, although it can be further configu
2727
>
2828
>Fail2ban is intended to be used in conjunction with an already-hardened server and should not be used as a replacement for secure firewall rules.
2929
30-
## Installing Fail2ban
30+
## 1) Install Fail2ban
3131

3232
Follow the [Getting Started](/docs/getting-started) guide to configure your basic server. You may also want to review the [Securing Your Server](/docs/security/securing-your-server) guide before beginning.
3333

@@ -121,55 +121,59 @@ Follow the [Getting Started](/docs/getting-started) guide to configure your basi
121121
ufw allow ssh
122122
ufw enable
123123

124-
## Configuring Fail2ban
124+
## 2) Configure Fail2ban
125125

126-
Fail2ban reads its configuration files so that all `.conf` files are read first and `.local` files override any settings. Because of this, all changes to the configuration are generally done in `.local` files, leaving the `.conf` files untouched.
126+
Fail2ban reads `.conf` configuration files first, then `.local` files override any settings. Because of this, all changes to the configuration are generally done in `.local` files, leaving the `.conf` files untouched.
127127

128-
### fail2ban.local Configuration
128+
### Configure fail2ban.local
129129

130-
1. The file `fail2ban.conf` contains the default configuration profile. The default settings will give you a sane and working setup so this is the best place to start. If you want to make any changes, it's best to do it in a separate file, `fail2ban.local`, which overrides `fail2ban.conf`. Rename a copy `fail2ban.conf` to `fail2ban.local`.
130+
1. `fail2ban.conf` contains the default configuration profile. The default settings will give you a reasonable working setup. If you want to make any changes, it's best to do it in a separate file, `fail2ban.local`, which overrides `fail2ban.conf`. Rename a copy `fail2ban.conf` to `fail2ban.local`.
131131

132132
cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local
133133

134-
3. From here, you can opt to edit the definitions in `fail2ban.local` to match your desired configuration. The values that can be changed are:
134+
2. From here, you can opt to edit the definitions in `fail2ban.local` to match your desired configuration. The values that can be changed are:
135135

136136
- `loglevel`: The level of detail that Fail2ban's logs provide can be set to 1 (error), 2 (warn), 3 (info), or 4 (debug).
137-
- `logtarget`: Logs actions into a specific file. The default value of `/var/log/fail2ban.log` puts all logging into the defined file. Alternately, you can change the value to STDOUT, which will output any data; STDERR, which will output any errors; SYSLOG, which is message-based logging; and FILE, which outputs to a file.
137+
- `logtarget`: Logs actions into a specific file. The default value of `/var/log/fail2ban.log` puts all logging into the defined file. Alternately, you can change the value to:
138+
- `STDOUT`: output any data
139+
- `STDERR`: output any errors
140+
- `SYSLOG`: message-based logging
141+
- `FILE`: output to a file
138142
- `socket`: The location of the socket file.
139143
- `pidfile`: The location of the PID file.
140144

141-
### jail.local Basic Configuration
145+
## 3) Configure jail.local Settings
142146

143-
1. The `jail.conf` file will enable Fail2ban for SSH by default for Debian and Ubuntu, but not CentOS. All other protocols and configurations (HTTP, FTP, etc.) are commented out. If you want to change this, it's recommended to create a `jail.local` for editing just like you did with `fail2ban.local`.
147+
1. The `jail.conf` file will enable Fail2ban for SSH by default for Debian and Ubuntu, but not CentOS. All other protocols and configurations (HTTP, FTP, etc.) are commented out. If you want to change this, create a `jail.local` for editing:
144148

145149
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
146150

147151
2. **If using CentOS or Fedora** you will need to change the `backend` option in `jail.local` from *auto* to *systemd*. This is not necessary on Debian 8 or Ubuntu 16.04, even though both use systemd as well.
148152

149-
{: .file-excerpt}
150-
/etc/fail2ban/jail.local
151-
: ~~~ conf
152-
# "backend" specifies the backend used to get files modification.
153-
# Available options are "pyinotify", "gamin", "polling", "systemd" and "auto".
154-
# This option can be overridden in each jail as well.
155-
156-
. . .
157-
158-
backend = systemd
159-
~~~
153+
{: .file-excerpt}
154+
/etc/fail2ban/jail.local
155+
: ~~~ conf
156+
# "backend" specifies the backend used to get files modification.
157+
# Available options are "pyinotify", "gamin", "polling", "systemd" and "auto".
158+
# This option can be overridden in each jail as well.
159+
160+
. . .
161+
162+
backend = systemd
163+
~~~
160164

161-
3. No jails are enabled by default in CentOS 7. For example, to enable the SSH daemon jail, uncomment the following lines in `jail.local:
165+
No jails are enabled by default in CentOS 7. For example, to enable the SSH daemon jail, uncomment the following lines in `jail.local`:
162166

163-
{: .file-excerpt}
164-
/etc/fail2ban/jail.local
165-
: ~~~ conf
166-
[sshd]
167-
enabled = true
168-
~~~
167+
{: .file-excerpt}
168+
/etc/fail2ban/jail.local
169+
: ~~~ conf
170+
[sshd]
171+
enabled = true
172+
~~~
169173

170-
#### IP Whitelisting
174+
### Whitelist IP
171175

172-
Add any IPs to the `ignoreip` line that you wish Fail2ban to ignore. By default, this command will not ban the localhost. If you work from a single IP address often, it may be beneficial to add it to the ignore list:
176+
To ignore specific IPs, add them to the `ignoreip` line. By default, this command will not ban the localhost. If you work from a single IP address often, it may be beneficial to add it to the ignore list:
173177

174178
{: .file-excerpt}
175179
/etc/fail2ban/jail.local
@@ -182,13 +186,13 @@ Add any IPs to the `ignoreip` line that you wish Fail2ban to ignore. By default,
182186
ignoreip = 127.0.0.1/8 123.45.67.89
183187
~~~
184188

185-
If you wish to whitelist IPs only for certain jails, this can be done with the `fail2ban-client` command. Replace `JAIL` with the name of your fail, and `123.45.67.89` with the IP you wish to whitelist.
189+
If you wish to whitelist IPs only for certain jails, this can be done with the `fail2ban-client` command. Replace `JAIL` with the name of your jail, and `123.45.67.89` with the IP you wish to whitelist.
186190

187191
fail2ban-client set JAIL addignoreip 123.45.67.89
188192

189-
#### Ban Time and Retry Amount
193+
### Ban Time and Retry Amount
190194

191-
The `bantime`, `findtime`, and `maxretry` then need to be set. These are the values that define the circumstances and the length of time of a ban.
195+
Set `bantime`, `findtime`, and `maxretry` to define the circumstances and the length of time of a ban:
192196

193197
{: .file-excerpt}
194198
/etc/fail2ban/jail.local
@@ -208,9 +212,9 @@ The `bantime`, `findtime`, and `maxretry` then need to be set. These are the val
208212

209213
- `maxretry`: How many attempts can be made to access the server from a single IP before a ban is imposed. The default is set to 3.
210214

211-
#### Email Alerts
215+
### Email Alerts
212216

213-
If you wish to receive email when Fail2ban is triggered, adjust the email settings:
217+
To receive email when fail2ban is triggered, adjust the email settings:
214218

215219
- `destemail`: The email address where you would like to receive the emails.
216220

@@ -224,7 +228,7 @@ If you wish to receive email when Fail2ban is triggered, adjust the email settin
224228
225229
You will also need to adjust the `action` setting, which defines what actions occur when the threshold for ban is met. The default, `%(action_)s`, only bans the user. `%(action_mw)s` will ban and send an email with a WhoIs report; while `%(action_mwl)s` will ban and send an email with the WhoIs report and all relevant lines in the log file. This can also be changed on a jail-specific basis.
226230

227-
### Jail Configuration
231+
### Other Jail Configuration
228232

229233
Beyond the basic settings address above, `jail.local` also contains various jail configurations for a number of common services, including SSH. By default, only SSH is enabled.
230234

@@ -255,11 +259,11 @@ An average jail configuration will resemble the following:
255259
256260
## Failregexs
257261

258-
Although Fail2ban comes with a number of filters for use, you may want to further customize these filters or create your own to suit your needs. Fail2ban uses *regular expressions* (*regex*) to parse log files, looking for instances of attempted break-ins and password failures. Fail2ban uses Python's regex extensions.
262+
Although Fail2ban comes with a number of filters, you may want to further customize these filters or create your own to suit your needs. Fail2ban uses *regular expressions* (*regex*) to parse log files, looking for instances of attempted break-ins and password failures. Fail2ban uses Python's regex extensions.
259263

260-
The best way to understand how failregex works is to write one. Although we do not advise having Fail2ban monitor your Wordpress' `access.log` on heavily-trafficked websites due to CPU concerns, it provides an instance of an easy-to-understand log file that you can use to learn about the creation of any failregex.
264+
The best way to understand how failregex works is to write one. Although we do not advise having Fail2ban monitor your Wordpress's `access.log` on heavily-trafficked websites due to CPU concerns, it provides an instance of an easy-to-understand log file that you can use to learn about the creation of any failregex.
261265

262-
### Writing the Regex
266+
### Write a Regex for Fail2ban
263267

264268
1. Navigate to your website's `access.log` (generally located at `/var/www/example.com/logs/access.log`) and find a failed login attempt. It will resemble:
265269

@@ -313,7 +317,7 @@ The best way to understand how failregex works is to write one. Although we do n
313317

314318
<HOST> - - \[\d{2}/\w{3}/\d{4}:\d{2}:\d{2}:\d{2} -\d{4}\] "POST /wp-login.php HTTP/1.1" 200
315319

316-
### Using the Failregex
320+
### Apply the Failregex
317321

318322
With the failregex created, it then needs to be added to a filter.
319323

@@ -356,9 +360,9 @@ With the failregex created, it then needs to be added to a filter.
356360

357361
## Using the Fail2ban Client
358362

359-
Fail2ban provides a command `fail2ban-client` that can be used to run Fail2ban from the command line. The input should be as follows:
363+
Fail2ban provides a command `fail2ban-client` that can be used to run Fail2ban from the command line:
360364

361-
`fail2ban-client COMMAND`
365+
fail2ban-client COMMAND
362366

363367
- `start`: Starts the Fail2ban server and jails.
364368
- `reload`: Reloads Fail2ban's configuration files.

0 commit comments

Comments
 (0)