linux用户登录日志与命令执行日志
vi /etc/profile 在末尾增加代码
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 histtimeformat="[$dt][${user}][${user_ip}]" export histfile="/var/log/history/${logname}/${user}@${user_ip}_$dt" chmod 600 /var/log/history/${logname}/*history* 2>/dev/null
执行 source /etc/profile 使其生效
之后不同用户会在 /var/log/history
以用户名为目录名的目录
进入对应目录后会有用户登录时间ip为名字的文件
内容为用户执行命令的日志信息
# cd /var/log/history/ # ll total 4 d-wx------ 2 root root 4096 nov 7 15:37 root # cd root # ll total 24 -rw------- 1 root root 800 nov 6 17:26 root@192.168.1.1_20181106_15:09:56 -rw------- 1 root root 4411 nov 6 18:55 root@192.168.1.1_20181106_17:40:31 -rw------- 1 root root 7 nov 7 15:35 root@192.168.1.1_20181107_15:33:23 -rw------- 1 root root 86 nov 7 15:37 root@192.168.1.1_20181107_15:35:27 -rw------- 1 root root 124 nov 7 15:37 root@192.168.1.1_20181107_15:37:18 # cat root@192.168.1.1_20181107_15\:37\:18 ll ps aux logout
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论