Skip to content

Instantly share code, notes, and snippets.

@zoonderkins
Last active January 12, 2025 07:27
Show Gist options
  • Save zoonderkins/27ae8e34439e5274f394821d505fbcf1 to your computer and use it in GitHub Desktop.
Save zoonderkins/27ae8e34439e5274f394821d505fbcf1 to your computer and use it in GitHub Desktop.
debian 12 and ubuntu install fail2ban script
#!/bin/bash
CHECK_OS(){
if [[ -f /etc/redhat-release ]]; then
release="centos"
elif grep -q -E -i "debian" /etc/issue; then
release="debian"
elif grep -q -E -i "ubuntu" /etc/issue; then
release="ubuntu"
elif grep -q -E -i "centos|red hat|redhat" /etc/issue; then
release="centos"
elif grep -q -E -i "debian" /proc/version; then
release="debian"
elif grep -q -E -i "ubuntu" /proc/version; then
release="ubuntu"
elif grep -q -E -i "centos|red hat|redhat" /proc/version; then
release="centos"
fi
}
GET_SETTING_FAIL2BAN_INFO(){
read -p "允许SSH登陆失败次数,默认10: " BLOCKING_THRESHOLD
[[ -z "${BLOCKING_THRESHOLD}" ]] && BLOCKING_THRESHOLD=10
read -p "SSH登陆失败次数超过${BLOCKING_THRESHOLD}次时,封禁时长(h),默认8760: " BLOCKING_TIME_H
[[ -z "${BLOCKING_TIME_H}" ]] && BLOCKING_TIME_H=8760
BLOCKING_TIME_S=$((BLOCKING_TIME_H * 3600))
}
INSTALL_FAIL2BAN(){
if [ ! -e /etc/fail2ban/jail.local ]; then
CHECK_OS
case "${release}" in
centos)
GET_SETTING_FAIL2BAN_INFO
yum -y install epel-release
yum -y install fail2ban
;;
debian|ubuntu)
GET_SETTING_FAIL2BAN_INFO
apt-get -y install fail2ban
;;
*)
echo "请使用CentOS,Debian,Ubuntu系统."
exit 1
;;
esac
else
echo "fail2ban已经安装了."
exit 0
fi
}
REMOVE_FAIL2BAN(){
if [ -e /etc/fail2ban/jail.local ]; then
CHECK_OS
case "${release}" in
centos)
systemctl stop fail2ban
yum -y remove fail2ban
rm -rf /etc/fail2ban/jail.local
;;
debian|ubuntu)
systemctl stop fail2ban
apt-get -y remove fail2ban
rm -rf /etc/fail2ban/jail.local
;;
esac
else
echo "fail2ban尚未安装."
exit 0
fi
}
SETTING_FAIL2BAN(){
CHECK_OS
case "${release}" in
centos|debian|ubuntu)
cat > /etc/fail2ban/jail.local <<EOF
[DEFAULT]
backend = systemd
ignoreip = 127.0.0.1
bantime = 86400
maxretry = ${BLOCKING_THRESHOLD}
findtime = 1800
[ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
logpath = $( [[ "$release" == "centos" ]] && echo "/var/log/secure" || echo "/var/log/auth.log" )
maxretry = ${BLOCKING_THRESHOLD}
findtime = 3600
bantime = ${BLOCKING_TIME_S}
EOF
systemctl restart fail2ban
systemctl enable fail2ban
systemctl restart sshd
;;
esac
}
VIEW_RUN_LOG(){
CHECK_OS
case "${release}" in
centos)
tail -f /var/log/secure
;;
debian|ubuntu)
tail -f /var/log/auth.log
;;
esac
}
case "${1}" in
install)
INSTALL_FAIL2BAN
SETTING_FAIL2BAN
;;
uninstall)
REMOVE_FAIL2BAN
;;
status)
echo -e "\033[41;37m【进程】\033[0m"
ps aux | grep fail2ban
echo
echo -e "\033[41;37m【状态】\033[0m"
fail2ban-client ping
echo
echo -e "\033[41;37m【Service】\033[0m"
service fail2ban status
;;
blocklist|bl)
[ -e /etc/fail2ban/jail.local ] && fail2ban-client status ssh-iptables || echo "fail2ban尚未安装."
;;
unlock|ul)
[ -e /etc/fail2ban/jail.local ] || { echo "fail2ban尚未安装."; exit 0; }
if [[ -z "${2}" ]]; then
read -p "请输入需要解封的IP: " UNLOCK_IP
[[ -z "${UNLOCK_IP}" ]] && { echo "不允许空值,请重试."; exit 1; }
fail2ban-client set ssh-iptables unbanip "${UNLOCK_IP}"
else
fail2ban-client set ssh-iptables unbanip "${2}"
fi
;;
more)
echo "【参考文章】
https://www.fail2ban.org
https://linux.cn/article-5067-1.html
【更多命令】
fail2ban-client -h"
;;
runlog)
VIEW_RUN_LOG
;;
start)
systemctl start fail2ban
;;
stop)
systemctl stop fail2ban
;;
restart)
systemctl restart fail2ban
;;
*)
echo "bash fail2ban.sh {install|uninstall|runlog|more}"
echo "bash fail2ban.sh {start|stop|restart|status}"
echo "bash fail2ban.sh {blocklist|unlock}"
;;
esac
#END

Install

chmod +x fail2ban.sh

./fail2ban.sh install && ./fail2ban.sh start

## Fail2ban failed to start on debian 12 issue

Solution: https://github.com/fail2ban/fail2ban/issues/3292#issuecomment-1932489993
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment