Skip to content

[ubuntu] fail2ban 防止 ssh 暴力嘗試密碼

Published: at 05:00 AM

架好主機後,通常會開啟 ssh 遠端登入,但過一陣子看系統紀錄後,會發現有許多在惡意嘗試登入的使用者

因此,安裝 fail2ban 可以防止 ssh 暴力嘗試


  1. 安裝 fail2ban sudo apt-get install fail2ban

  2. 查看目前 ssh 阻擋設定,/etc/fail2ban/jail.conf,並複製一份為 jail.local 覆寫你想做的設定,如下:

    [code]

    [ssh]

    enabled = true # 開啟 ssh 阻擋 port = ssh # ssh port filter = sshd # 套用 ssh 的 filter(定義在filter.d/sshd) logpath = /var/log/auth.log # log檔位置 maxretry = 3 # 最多嘗試密碼次數 banaction = iptables-multiport # 使用 iptables 防火牆阻擋,定義在(action.d/iptables-multiport.conf) bantime = 85400 # 錯誤三次之後會被阻擋一天 [/code]

  3. 開啟 fail2ban 服務 sudo service fail2ban start

  4. 完成!!