fail2ban-regex 로 sshd 필터 미리 시험하기
jail 을 켜기 전에 fail2ban-regex 로 auth 로그가 sshd 필터에 몇 줄 걸리는지 세고, 놓친 줄과 mode 별 차이를 확인해 필터 쪽 문제부터 가립니다.
Lines: 12 lines, 3 ignored, 6 matched, 3 missedfail2ban-regex 는 로그를 필터에 흘려 보고 마지막에 이 줄을 냅니다. 서비스도 방화벽 규칙도 건드리지 않아서 jail 을 켜기 전이나 차단이 안 걸리는 이유를 찾을 때 먼저 돌려 볼 수 있습니다. 아래 출력은 직접 만든 표본 로그 12줄(주소는 203.0.113.x)로 받았습니다. 실제 로그는 /var/log/auth.log 를 그대로 넘기면 됩니다.
[1] 표본의 일부입니다. sshd 줄 사이에 sudo 줄을 섞어 두었습니다.
2026-10-19T09:14:02.113456+09:00 server1 sshd[2101]: Failed password for invalid user admin from 203.0.113.7 port 51022 ssh22026-10-19T09:15:11.004412+09:00 server1 sshd[2110]: Invalid user test from 203.0.113.8 port 401122026-10-19T09:17:30.118004+09:00 server1 sshd[2131]: Unable to negotiate with 203.0.113.10 port 39880: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1 [preauth]2026-10-19T09:18:01.900311+09:00 server1 sshd[2140]: Did not receive identification string from 203.0.113.11 port 600002026-10-19T09:19:00.010000+09:00 server1 sudo: alice : TTY=pts/0 ; PWD=/home/alice ; USER=root ; COMMAND=/usr/bin/apt update[2] 로그 파일, 필터 순서로 넘깁니다. sshd 처럼 이름만 적으면 /etc/fail2ban/filter.d/sshd.conf 를 읽습니다.
$ fail2ban-regex auth.log sshd요약 줄의 세 숫자는 이렇게 읽습니다.
* matched — 필터가 실패로 센 줄. 표본에서는 Failed password 4줄, Invalid user 1줄, maximum authentication attempts exceeded 1줄입니다. * ignored — 실패로 세지 않는 줄. 표본에서는 Accepted publickey, Failed publickey, Connection closed by invalid user … [preauth] 입니다. sshd.conf 가 이 줄들에 <F-NOFAIL> 을 달아 두었고 ignoreregex 는 비어 있습니다. * missed — 어느 정규식에도 안 걸린 줄.
Failregex: 아래에는 정규식마다 [5] 처럼 걸린 횟수가 붙고, 0 번 걸린 정규식은 나오지 않습니다.
[3] 놓친 줄은 기본 출력에 Missed line(s): 로 따라 나옵니다.
|- Missed line(s):| 2026-10-19T09:17:30.118004+09:00 server1 sshd[2131]: Unable to negotiate with 203.0.113.10 port 39880: no matching key exchange method found. Their offer: diffie-hellman-group1-sha1 [preauth]| 2026-10-19T09:18:01.900311+09:00 server1 sshd[2140]: Did not receive identification string from 203.0.113.11 port 60000| 2026-10-19T09:19:00.010000+09:00 server1 sudo: alice : TTY=pts/0 ; PWD=/home/alice ; USER=root ; COMMAND=/usr/bin/apt update`-놓친 줄이 많으면 숨깁니다. sshd 와 무관한 줄을 19개 넣었을 때는 전부 나왔고 20개부터는 다음 문구로 바뀌었습니다.
Missed line(s): too many to print. Use --print-all-missed to print all 20 lines$ fail2ban-regex auth.log sshd --print-all-missed--print-all-matched · --print-all-ignored 도 있고, 반대로 아예 끄는 --print-no-missed · --print-no-ignored 도 있습니다.
[4] 놓친 줄 중 sudo 는 sshd 와 무관하니 놓치는 게 맞습니다. 나머지 두 줄은 mode 를 바꾸면 걸립니다. sshd.conf 의 mode 는 normal(기본) · ddos · extra · aggressive 이고, 필터 이름 뒤 대괄호로 지정합니다.
$ fail2ban-regex auth.log 'sshd[mode=aggressive]'같은 표본으로 비교한 결과입니다.
| mode | ignored | matched | missed | normal 과 달라진 줄 |
|---|---|---|---|---|
| normal | 3 | 6 | 3 | — |
| ddos | 2 | 8 | 2 | Did not receive identification string 과 Connection closed … [preauth] 가 matched |
| extra | 2 | 7 | 3 | Unable to negotiate … 가 matched, Connection closed … [preauth] 는 ignored 에서 missed 로 |
| aggressive | 2 | 9 | 1 | 위 셋 모두 matched, 남은 1줄은 sudo |
어느 mode 가 어떤 줄을 잡는지는 sshd.conf 의 mdre-… 항목에 있습니다. 이 결과는 jail 의 mode 를 정할 때의 참고입니다.
[5] Failed publickey 도 같은 방식입니다. 기본(publickey = nofail)에서는 ignored 이고, publickey=any 로 주면 matched 로 넘어갑니다.
$ fail2ban-regex auth.log 'sshd[publickey=any]'Lines: 12 lines, 2 ignored, 7 matched, 3 missed[6] 로그 자리에 줄 자체를, 필터 자리에 정규식 문자열을 넘기면 한 줄만 바로 시험할 수 있습니다. 정규식은 날짜가 지워진 나머지에 맞춰지므로 ^ 로 시작하면 어긋납니다.
$ L='Oct 19 09:20:01 server1 sshd[2200]: Failed password for bob from 203.0.113.30 port 50001 ssh2'$ fail2ban-regex "$L" '^Failed password for .* from <HOST>'Lines: 1 lines, 0 ignored, 0 matched, 1 missed^ 를 빼거나 ^.*Failed password … 로 시작하면 1 matched 가 됩니다.
[7] 실패로 센 주소만 보려면 -o ip 를 줍니다. 요약 대신 매치된 줄의 주소가 한 줄씩 나옵니다.
$ fail2ban-regex auth.log sshd -o ip203.0.113.7203.0.113.7203.0.113.7203.0.113.8203.0.113.9203.0.113.7출력에 maxretry 판정은 없습니다. 주소별 횟수만 세어 jail 설정과 맞춰 봅니다.
auth.log (SSH) 로그를 브라우저에서 바로 분석 ↗ — 아무것도 올라가지 않습니다.