0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linux(Ubuntu,CentOS)で不正アクセスをブロックする方法

Posted at

1. Fail2ban のインストール

Debian/Ubuntu の場合:

sudo apt-get update
sudo apt-get install fail2ban

CentOS/RHEL の場合:

sudo yum install epel-release
sudo yum install fail2ban

2. Fail2ban の設定

Fail2ban のデフォルト設定は /etc/fail2ban/jail.conf にありますが、直接編集せずに上書き用の /etc/fail2ban/jail.local を作成するのが推奨されます。この場合、

nano /etc/fail2ban/jail.local

Fail2ban は、起動時に自動的に /etc/fail2ban/jail.conf とその上書きファイルである /etc/fail2ban/jail.local(存在する場合)を読み込みます。そのため、特に設定ファイル名をどこかに登録したり、指定したりする必要はありません。

たとえば、SSH に対する保護設定は以下のようになります。
※ログファイルのパスはディストリビューションによって異なります(Debian/Ubuntu では /var/log/auth.log、CentOS/RHEL では /var/log/secure )。

/etc/fail2ban/jail.conf
[sshd]
enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log    ; Debian/Ubuntuの場合(CentOS/RHELでは /var/log/secure)
maxretry = 5
bantime  = 600    

3. サービスの再起動

設定を変更したら、Fail2ban サービスを再起動して新しい設定を反映させます。

sudo systemctl restart fail2ban

4. 動作確認

設定が正しく読み込まれているか確認するには、次のコマンドを使用します。

sudo fail2ban-client status sshd
sudo fail2ban-client status recidive

これにより、SSH jail の状態やブロック対象の IP などが表示されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?