syslogd日志的一些作用

启用日志功能,并配置日志记录。

操作步骤

Linux系统默认启用以下类型日志:

  • 系统日志(默认)/var/log/messages

  • cron日志(默认)/var/log/cron

  • 安全日志(默认)/var/log/secure

注意:部分系统可能使用syslog-ng日志,配置文件为:/etc/syslog-ng/syslog-ng.conf。

您可以根据需求配置详细日志。

 记录所有用户的登录和操作日志

    通过脚本代码实现记录所有用户的登录操作日志,防止出现安全事件后无据可查。

操作步骤

    1. 运行 [root@xxx /]# vim /etc/profile打开配置文件。

       

    2. 在配置文件中输入以下内容:

history 
USER=`whoami` 
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e's/[()]//g'` 
if [ "$USER_IP" = "" ]; then USER_IP=`hostname` 
fi
if [ ! -d /var/log/history ]; then mkdir /var/log/history
chmod 777 /var/log/history
fi 
if [ ! -d /var/log/history/${LOGNAME} ]; then mkdir /var/log/history/${LOGNAME} 
chmod 300 /var/log/history/${LOGNAME} 
fi 
export HISTSIZE=4096 
DT=`date +"%Y%m%d_%H:%M:%S"` 
export HISTFILE="/var/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT" 
chmod 600 /var/log/history/${LOGNAME}/*history* 2>/dev/null

 

  1. 运行 [root@xxx /]# source /etc/profile 加载配置生效。
    

      

    注意:/var/log/history 是记录日志的存放位置,可以自定义。

        通过上述步骤,可以在 /var/log/history 目录下以每个用户为名新建一个文件夹,每次用户退出后都会产生以用户名、登录IP、时间的日志文件,包含此用户本次的所有操作(root用户除外)。

 

zabbix利用脚本监控用户登录情况,

#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

[[ $(grep -iE 'ubuntu|debin|elementary' /etc/issue) ]] \
     && logs='/var/log/auth.log' \
     || logs='/var/log/secure'

## $1: Accepted OR Failed

#portarray=( $(awk "/$1/ {print \$(NF-3)}" $logs |sort |uniq) )
count=$2
count=${count:=0}
portarray=( $(grep 'sshd' $logs |awk "/$1/ {print \$(NF-3)}" $logs |sort |uniq -c |awk "{if (\$1 >= $count) print \$NF }") )

length=${#portarray[@]}

function zabbix_json(){
    printf "{\n"
    printf '\t'"\"data\":["
    for ((i=0;i<$length;i++))
    do
        printf '\n\t\t{'
        printf "\"{#ACCESS_IP}\":\"${portarray[$i]}\"}"
        if [ $i -lt $[$length-1] ];then
            printf ','
        fi
    done
    printf "\n\t]\n"
    printf "}\n"
}

function check(){
    awk "/$1/ {print \$(NF-3)}" $logs |grep $2 |sort |uniq -c |awk '{print $1}'
}

[[ $1 = check ]] \
    && $* \
    || zabbix_json

 

posted @ 2021-09-08 16:00  Suixin随心  阅读(92)  评论(0编辑  收藏  举报