按时按登录IP记录Linux所有用户操作日志的方法
在/etc/profile配置文件的末尾加入以下脚本代码就可以实现:
PS1="`whoami`@`hostname`:"'[$PWD]' history 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 /tmp/history ] then mkdir /tmp/history chmod 777 /tmp/history fi if [ ! -d /tmp/history/${LOGNAME} ] then mkdir /tmp/history/${LOGNAME} chmod 300 /tmp/history/${LOGNAME} fi export HISTSIZE=4096 DT=`date +"%Y%m%d_%H%M%S"` export HISTFILE="/tmp/history/${LOGNAME}/${USER_IP}_history.$DT" chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null
通过上面的代码可以看出来,在系统的/tmp新建个history目录(这个目录可以自定义),在目录中记录了所有的登陆过系统的用户和IP地址,这也是监测系统安全的方法之一。
本文作者:香菜哥哥
本文链接:https://www.cnblogs.com/yizhipanghu/p/14211669.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步