SOFTELメモ Developer's blog

[linux] fail2ban インストール例

2024/06/19 linux

準備

EPELリポジトリ追加

# dnf install epel-release

インストール

# dnf install fail2ban

なお、インストール初期状態では、起動しない、自動起動しない、何もブロックしない。何もしない。

自動起動、起動

# systemctl enable fail2ban
# systemctl start fail2ban

設定する

/etc/fail2ban/ の中に fail2ban.conf、jail.conf のファイルがあるが、編集しない。

以下の2つのファイルで設定を上書きする形。

/etc/fail2ban/fail2ban.local
/etc/fail2ban/jail.local

設定例

vi /etc/fail2ban/jail.local

sshの監視を追加したい場合、以下だけで追加できる。ban時間や試行回数などデフォルトでよければ。

[sshd]
enabled = true

設定反映

systemctl reload fail2ban

状態確認

何も監視していないと、Number of jail は0件。

# fail2ban-client status
Status
|- Number of jail:      1
`- Jail list:   sshd

動作確認

わざとbanされてみる。

# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed:     8
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd + _COMM=sshd-session
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   111.222.333.444

ban解除

fail2ban-client set sshd unbanip 111.222.333.444

関連するメモ

コメント