1、如果常用电脑出口,有公网IP,将IP写入白名单。
echo "sshd:218.xx.xxx.37:allow" >> /etc/hosts.allow
tail -1 /etc/hosts.allow
2、配置脚本,输入密码错误三次以上,加入 hosts.deny, 简单防止暴力破解。
vim /usr/local/scripts/denyssh.sh
#!/bin/bash
a=$(date +%b" "%e)
sshlogfile=/var/log/secure
sshdenylist=/tmp/sshdenylist.txt
hostsdenyfile=/etc/hosts.deny
grep "$a" $sshlogfile | awk '/Failed/{print $(NF-3)}' | sort -rn | uniq -c > $sshdenylist
while read line
do
count=$(echo $line | awk '{print $1}')
DenyIP=$(echo $line | awk '{print $2}')
if grep ":${DenyIP}:" $hostsdenyfile &> /dev/null;then
continue
elif [ $count -ge 8 ];then
echo "sshd:${DenyIP}:deny" >> $hostsdenyfile
fi
done < $sshdenylist
配置crontab定时任务
crontab -e
*/5 * * * * /bin/bash /usr/local/scripts/denyssh.sh &> /dev/null
0 9 1 * * /bin/sed -i '/^#/!d' /etc/hosts.deny &> /dev/null
3、记录每个用户操作命令记录, 并按日期存档。
vim /usr/local/scripts/jilu.sh
#Record history operation
USER_IP=`who -u am i 2>/dev/null |awk '{print $NF}' |sed -e 's/[()]//g'`
LOGNAME=`who -u am i |awk '{print $1}'`
HISTDIR=/usr/share/.history
if [ -z $USER_IP ]
then
USER_IP=`hostname`
fi
if [ ! -d $HISTDIR ]
then
mkdir -p $HISTDIR
chmod 777 $HISTDIR
fi
if [ ! -d $HISTDIR/${LOGNAME} ]
then
mkdir -p $HISTDIR/${LOGNAME}
chmod 300 $HISTDIR/${LOGNAME}
fi
export HISTSIZE=4000
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"
export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"
chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null
按时间记录每个用户操作的命令。
上传文件jilu.sh到root目录,并执行如下命令。
cat >> /etc/bashrc < jilu.sh
查看是否成功
grep -A 20 "#Record history operation" /etc/bashrc
rm -f jilu.sh
4、修改ssh的默认
若ssh使用22默认端口则修改
sed -i 's/#Port 22$/Port 22022/' /etc/ssh/sshd_config
grep "Port" /etc/ssh/sshd_config
service sshd restart
注意修改后,不要断开当前xshell连接,打开一个“全新”的窗口去连接新的ssh端口,查看是否成功。