Skip to content

Instantly share code, notes, and snippets.

@hannesbe
Last active February 28, 2020 16:00
Show Gist options
  • Save hannesbe/893476e23e637701fd88 to your computer and use it in GitHub Desktop.
Save hannesbe/893476e23e637701fd88 to your computer and use it in GitHub Desktop.
Install script fail2ban with action to csf | CentOS/EL 7
#!/bin/sh
#
# Shell script to install & configure fail2ban with firewalld actions
# Just one command to install & configure in a few seconds.
# CentOS/EL7+ required.
#
# Run this command to execute the script in one go
# curl -sSL https://gist.github.com/hannesbe/893476e23e637701fd88/raw/fail2ban-csf-install.sh | bash -s
#
set -e
N=$(tput sgr0) ; BD=$(tput bold) ; GR=$(tput setaf 2; tput bold) ; YE=$(tput setaf 3) ; R=$(tput setaf 1)
OK="[$GR OK $N]\n" ; FL="[$R FAILED $N]\n"
[[ $EUID -eq 0 ]] ||
( printf "You must be a root user $FL\n" 2>&1 ; exit 1 )
printf ""$BD"\nInstalling & configuring "$YE"fail2ban for csf with sshd filter enabled$N\n"
printf ""$BD"\nDownloading & installing packages$N\n" &&
yum install epel-release fail2ban fail2ban-systemd -y -q &&
printf " $OK" || ( printf " $FL" ; exit 2 )
printf ""$BD"\nDownloading jail config$N\n"
CONFIG=/etc/fail2ban/jail.local
[[ -f "$CONFIG" ]] &&
( printf "$YE$CONFIG$N already exists, renaming yours to $YE$CONFIG.save$N " ;
mv $CONFIG $CONFIG.save && printf " $OK" || ( printf " $FL" ; exit 3 ) )
curl -fsSL https://gist.github.com/hannesbe/52e080c0b25f24959fc7/raw/jail.local -o $CONFIG &&
printf "Saved config to $YE$CONFIG$N $OK" || ( printf " $FL" ; exit 3 )
printf ""$BD"\nDownloading csf jail config$N\n" &&
CONFIG=/etc/fail2ban/jail.d/csf.conf &&
curl -fsSL https://gist.github.com/hannesbe/52e080c0b25f24959fc7/raw/jail.local_csf -o $CONFIG &&
printf "Saved config to $YE$CONFIG3$N $OK" || ( printf " $FL" ; exit 3 )
printf ""$BD"\nDownloading action config$N\n"
ACTION=/etc/fail2ban/action.d/csf.conf
[[ -f "$ACTION" ]] &&
( printf "$YE$ACTION$N already exists, renaming yours to $YE$ACTION.save$N " ;
mv $ACTION $ACTION.save && printf " $OK" || ( printf " $FL" ; exit 3 ) )
curl -fsSL https://gist.github.com/hannesbe/09c1254a0b74aefc4b47/raw/csf.conf -o $ACTION &&
printf "Saved config to $YE$ACTION$N $OK" || ( printf " $FL" ; exit 3 )
printf ""$BD"\nEnabling & starting daemon$N\n" &&
systemctl start fail2ban &&
systemctl enable fail2ban &&
printf " $OK" || ( printf " $FL" ; exit 4 )
@hannesbe
Copy link
Author

Run this command to execute the script in one go :
curl -sSL https://gist.github.com/hannesbe/893476e23e637701fd88/raw/fail2ban-csf-install.sh | bash -s

@yumminova
Copy link

getting error that curl could not write body ((

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment