Tháng Năm 23, 2026Hướng dẫn
Cách Cài Đặt và Cấu Hình Fail2ban trên Linux
Bảo vệ VPS khỏi tấn công brute-force bằng cách cài Fail2ban với jail SSH và Nginx.

Fail2ban theo dõi file log và tạm thời chặn địa chỉ IP có hành vi độc hại — ví dụ đăng nhập SSH thất bại liên tục. Đây là một trong những công cụ bảo mật đầu tiên nên cài trên VPS Hiddence mới.
Cài đặt Fail2ban
bash
# Ubuntu / Debian
sudo apt update
sudo apt install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
# CentOS / RHEL / Alma / Rocky
sudo yum install epel-release -y
sudo yum install fail2ban -y
sudo systemctl enable fail2ban
sudo systemctl start fail2banBảo vệ SSH
Tạo file cấu hình local (không bao giờ sửa trực tiếp jail.conf):
bash
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 5
bantime = 3600
findtime = 600
sudo systemctl restart fail2banBảo vệ Nginx (tùy chọn)
Chặn IP gây quá nhiều lỗi 404 hoặc lỗi xác thực:
bash
[nginx-http-auth]
enabled = true
port = http,https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 5
[nginx-noscript]
enabled = true
port = http,https
filter = nginx-noscript
logpath = /var/log/nginx/access.log
maxretry = 6Kiểm tra IP bị chặn
bash
sudo fail2ban-client status
sudo fail2ban-client status sshd
# Unban an IP if needed:
sudo fail2ban-client set sshd unbanip 1.2.3.4Thực hành tốt nhất
- Dùng khóa SSH thay vì mật khẩu để giảm bề mặt tấn công
- Đổi cổng SSH mặc định chỉ kèm theo quy tắc firewall
- Thêm IP văn phòng vào ignoreip của fail2ban nếu cần
- Theo dõi /var/log/fail2ban.log thường xuyên
- Kết hợp Fail2ban với UFW hoặc Firewalld