# install-fail2ban

## Install Fail2ban

#### 1. Cài đặt Fail2ban

**Cập nhật hệ thống**

bash Copy

```bash
sudo apt update && sudo apt upgrade -y
```

**Cài đặt Fail2ban**

bash Copy

```bash
sudo apt install fail2ban -y
```

**Kiểm tra phiên bản**

bash Copy

```bash
fail2ban-client --version
```

Theo LinuxCapable , Fail2ban có sẵn trong repository mặc định của Ubuntu.

***

#### 2. Khởi động và kích hoạt dịch vụ

bash Copy

```bash
# Kích hoạt và khởi động ngay lập tức
sudo systemctl enable fail2ban --now

# Hoặc tách riêng lệnh
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
```

**Kiểm tra trạng thái**

bash Copy

```bash
sudo systemctl status fail2ban
```

Nếu thấy `Active: active (running)` là thành công .

***

#### 3. Cấu hình Fail2ban

**Nguyên tắc quan trọng**

**KHÔNG** sửa file `jail.conf` trực tiếp vì sẽ bị ghi đè khi cập nhật. Thay vào đó, tạo file `jail.local` .

**Tạo file cấu hình local**

bash Copy

```bash
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
```

**Cấu hình cơ bản (thêm vào file `jail.local`)**

ini Copy

```ini
[DEFAULT]
# Danh sách IP được bỏ qua (không bị ban)
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24 YOUR_IP_ADDRESS

# Thời gian ban (10 phút = 600 giây, -1 = vĩnh viễn)
bantime = 10m

# Thời gian theo dõi (10 phút)
findtime = 10m

# Số lần thử sai tối đa
maxretry = 5

# Backend firewall (tự động phát hiện hoặc chọn iptables/ufw/nftables)
banaction = iptables-multiport

# Gửi email thông báo (tùy chọn)
destemail = admin@yourdomain.com
sender = fail2ban@yourdomain.com
action = %(action_mw)s

# Tăng thời gian ban cho tái phạm
bantime.increment = true
bantime.factor = 2
bantime.formula = ban.Time * (1<<(ban.Count if ban.Count<20 else 20)) * banFactor

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 1h
findtime = 10m
```

**Giải thích tham số** :

* `ignoreip`: IP không bị ban (thêm IP của bạn vào đây)
* `bantime`: Thời gian khóa IP
* `findtime`: Khung thời gian đếm số lần thất bại
* `maxretry`: Số lần thử sai tối đa trước khi bị ban

***

#### 4. Tích hợp với UFW (Ubuntu Firewall)

Nếu dùng UFW, thay đổi `banaction` :ini Copy

```ini
[DEFAULT]
banaction = ufw
```

Hoặc cho jail SSH cụ thể:ini Copy

```ini
[sshd]
banaction = ufw
```

***

#### 5. Khởi động lại và kiểm tra

**Áp dụng cấu hình mới**

bash Copy

```bash
sudo systemctl restart fail2ban
```

**Kiểm tra cấu hình**

bash Copy

```bash
sudo fail2ban-client -t
```

**Xem trạng thái jail SSH**

bash Copy

```bash
sudo fail2ban-client status sshd
```

Kết quả mẫu :plain Copy

```
Status for the jail: sshd
|- Filter
|  |- Currently failed: 0
|  |- Total failed: 0
|  `- Journal matches: _SYSTEMD_UNIT=ssh.service + _COMM=sshd
`- Actions
   |- Currently banned: 0
   |- Total banned: 0
   `- Banned IP list:
```

***

#### 6. Quản lý Fail2ban

**Kiểm tra tất cả jail đang chạy**

bash Copy

```bash
sudo fail2ban-client status
```

**Ban IP thủ công**

bash Copy

```bash
sudo fail2ban-client set sshd banip 192.168.1.100
```

**Gỡ ban IP**

bash Copy

```bash
sudo fail2ban-client set sshd unbanip 192.168.1.100
```

**Xem log thời gian thực**

bash Copy

```bash
sudo tail -f /var/log/fail2ban.log
```

***

#### 7. Cấu hình nâng cao

**Bảo vệ thêm dịch vụ khác**

Thêm vào `jail.local` :ini Copy

```ini
# Bảo vệ Apache
[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/error.log
maxretry = 3

# Bảo vệ Nginx
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log
maxretry = 3

# Bảo vệ FTP (ProFTPD)
[proftpd]
enabled = true
port = ftp,ftp-data
filter = proftpd
logpath = /var/log/proftpd/proftpd.log
maxretry = 5

# Bảo vệ Mail (Postfix)
[postfix]
enabled = true
port = smtp,465,submission
filter = postfix
logpath = /var/log/mail.log
maxretry = 3
```

**Cấu hình ban vĩnh viễn**

ini Copy

```ini
bantime = -1
```

***

#### 8. Script tự động cài đặt

Tạo file `install_fail2ban.sh`:bash Copy

```bash
#!/bin/bash

echo "=== CÀI ĐẶT FAIL2BAN CHO UBUNTU SERVER ==="

# Cập nhật hệ thống
sudo apt update && sudo apt upgrade -y

# Cài đặt Fail2ban
sudo apt install fail2ban -y

# Sao lưu file gốc nếu chưa có
if [ ! -f /etc/fail2ban/jail.local ]; then
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
fi

# Cấu hình cơ bản
sudo tee /etc/fail2ban/jail.d/defaults-ubuntu.conf > /dev/null <<EOF
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime = 10m
findtime = 10m
maxretry = 5
banaction = ufw

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 1h
EOF

# Khởi động dịch vụ
sudo systemctl enable fail2ban --now

# Kiểm tra trạng thái
echo ""
echo "=== KIỂM TRA TRẠNG THÁI ==="
sudo systemctl status fail2ban --no-pager

echo ""
echo "=== CẤU HÌNH SSH JAIL ==="
sudo fail2ban-client status sshd

echo ""
echo "✅ Cài đặt Fail2ban hoàn tất!"
echo "📋 Xem log: sudo tail -f /var/log/fail2ban.log"
echo "📊 Xem status: sudo fail2ban-client status sshd"
```

Chạy script:bash Copy

```bash
chmod +x install_fail2ban.sh
sudo ./install_fail2ban.sh
```

***

#### ⚠️ Lưu ý quan trọng

Table

| Vấn đề                  | Giải pháp                                              |
| ----------------------- | ------------------------------------------------------ |
| **Tự khóa mình**        | Thêm IP của bạn vào `ignoreip` trước khi kích hoạt     |
| **Không ban được**      | Kiểm tra `backend` phù hợp với hệ thống (systemd/file) |
| **UFW không hoạt động** | Đổi `banaction` thành `ufw`                            |
| **Ubuntu 24.04+**       | Dùng `nftables` backend thay vì `iptables`             |

***

#### Kiểm tra phiên bản Ubuntu

Table

| Ubuntu    | Fail2ban | Backend mặc định   |
| --------- | -------- | ------------------ |
| 22.04 LTS | 0.11.x   | iptables-multiport |
| 24.04 LTS | 1.0.x    | nftables           |
| 26.04 LTS | 1.1.x    | nftables           |

Theo LinuxCapable , các phiên bản mới dùng `nftables` thay vì `iptables`.Fail2ban giúp giảm đáng kể các cuộc tấn công brute-force vào SSH và các dịch vụ khác! 🛡️
