# apt update
# apt upgrade

패키지 목록 새로고침, 실제 패키지 업데이트 적용

# apt install -y fail2ban

fail2ban 패키지 설치

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

원본 보호를 위해 jail.local 파일을 생성해 오버라이드

# vim /etc/fail2ban/jail.local

92 주석 제거

ignoreip = 127.0.0.1/8 ::1

-> 감시 대상에서 제외할 IP 설정

132 내용 수정

backend = systemd

-> systemd 기반 로그를 사용하기 때문에 backend 값을 systemd로 바꾸어

감시가 정확해지도록 설정

208 banaction = ufw

209 banaction_allports = ufw

-> SSH 등에서 공격 IP가 감지되면 Fail2ban이 UFW를 통해 해당 IP를 차단

280 내용 추가

[sshd]

enabled = true

logpath = /var/log/auth.log

maxretry = 5

-> SSH 무차별 대입 공격을 자동으로 차단하도록 설정

# systemctl restart fail2ban

을하고 다른 서버에서 ssh 접속 시도(없는 사용자 또는 틀린 비밀번호 입력)

4~5번하고 나서 fail2ban 서버에서

# fail2ban-client status sshd 입력

아래에 목록에 해당 서버 IP가 뜨면 성공

# /var/www/html# fail2ban-client status sshd

Status for the jail: sshd

|- Filter

| |- Currently failed: 15

| |- Total failed: 135

| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd

`- Actions

|- Currently banned: 2 <- 현재 차단된 IP 개수

|- Total banned: 8

`- Banned IP list: (차단된 IP) <- 실제 차단된 IP 목록

총 8개의 IP가 차단이 됐었고 현재는 2개의 IP가 차단된 상태이다.