1
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?

fail2ban sshのポート変更バージョン #メモ

Last updated at Posted at 2024-07-02

sshでポートを変更した際のfail2banの設定について解説する
環境はUbuntu 22.04

最初に

/etc/fail2ban/jail.confはパッケージアップデートで変わってしまう可能性があるので

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

でファイルをコピーして/etc/fail2ban/jail.localを編集する

設定

必要ならかえる

ignoreip = 127.0.0.1/8 ::1
bantime  = 10m
chain = INPUT
findtime  = 10m
maxretry = 5
usedns = warn

action

実際おこなわれるアクションのはここに書いてある
iptablesで特定のipからのポート0-65535へのアクセスをbanする

port = 0:65535
banaction = iptables-multiport
banaction_allports = iptables-allports
action_ = %(banaction)s[port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
action = %(action_)s

アクションは/etc/fail2ban/action.d/iptables-multiport.confで定義されている

sshd

sshdの監視を有効にするのは

/etc/fail2ban/jail.d/defaults-debian.conf
[sshd]
enabled = true

/etc/fail2ban/filter.d/sshd.confでfilterしている

filter = %(__name__)s[mode=%(mode)s]

[sshd]
port    = $your-port
logpath = %(sshd_log)s
backend = %(sshd_backend)s

sshdの設定をかえておく

LogLevel VERBOSE

そのままだと下記のエラーがでるので

ERROR   Failed during configuration: Have not found any log file for sshd jail.
/etc/fail2ban/paths-overrides.local
[DEFAULT]
default/backend = systemd

とする

このファイルは以下で読み込まれている

/etc/fail2ban/paths-common.conf
after  = paths-overrides.local

default_backend = %(default/backend)s

sshd_backend = %(default_backend)s

起動

実際に起動して確認

sudo systemctl enable --now fail2ban
sudo systemctl status fail2ban
● fail2ban.service - Fail2Ban Service
     Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2024-06-28 17:54:26 UTC; 5s ago
       Docs: man:fail2ban(1)
   Main PID: 22196 (fail2ban-server)
      Tasks: 5 (limit: 1065)
     Memory: 16.7M
        CPU: 338ms
     CGroup: /system.slice/fail2ban.service
             └─22196 /usr/bin/python3 /usr/bin/fail2ban-server -xf start

Jun 28 17:54:26 servername systemd[1]: Started Fail2Ban Service.
Jun 28 17:54:26 servername fail2ban-server[22196]: Server ready

References

centos - fail2ban iptables having port 22 and fails to block ssh on custom port - Server Fault

How Fail2Ban Works to Protect Services on a Linux Server | DigitalOcean

[BR]: fail2ban does not start on some debian/ubuntu systems - backend should probably be set to systemd on all systemd-based distros #3292

1
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
1
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?