我在 AWS LightSail 上有一個 Bitnami 伺服器 Ubuntu Xenial。我跟著本教程依國家限制 SSH 連線。此腳本ipfilter.sh
過濾 IP 位址:
#!/bin/bash
# License: WTFPL
# UPPERCASE space-separated country codes to ACCEPT
ALLOW_COUNTRIES="US UK"
LOGDENY_FACILITY="authpriv.notice"
if [ $# -ne 1 ]; then
echo "Usage: `basename $0` " 1>&2
exit 0 # return true in case of config issue
fi
if [[ "`echo $1 | grep ':'`" != "" ]] ; then
COUNTRY=`/usr/bin/geoiplookup6 "$1" | awk -F ": " '{ print $2 }' | awk -F "," '{ print $1 }' | head -n 1`
else
COUNTRY=`/usr/bin/geoiplookup "$1" | awk -F ": " '{ print $2 }' | awk -F "," '{ print $1 }' | head -n 1`
fi
[[ $COUNTRY = "IP Address not found" || $ALLOW_COUNTRIES =~ $COUNTRY ]] && RESPONSE="ALLOW" || RESPONSE="DENY"
if [[ "$RESPONSE" == "ALLOW" ]] ; then
logger -p $LOGDENY_FACILITY "$RESPONSE sshd connection from $1 ($COUNTRY)"
exit 0
else
logger -p $LOGDENY_FACILITY "$RESPONSE sshd connection from $1 ($COUNTRY)"
exit 1
fi
我將其設定為:
sudo -y apt-get install geoip-bin geoip-database
sudo echo "sshd: ALL" > /etc/hosts.deny
sudo echo "vsftpd: ALL" >> /etc/hosts.deny
sudo echo "sshd: ALL: spawn /path/to/file/ipfilter.sh %a" > /etc/hosts.allow
我使用 重新啟動了 SSH 守護程式sudo systemctl restart ssh.service
。我嘗試從另一個國家建立連接並檢查文件/var/log/syslog
和/var/log/auth.log
,但它們沒有顯示此事件。該檔案/etc/rsyslog.conf
不顯示 authpriv 設施的日誌記錄位置。
如何檢查此 IP 過濾器是否正常運作?