通过登入IP记录Linux所有用户登录所操作的日志

来源:互联网 发布:谷歌去马赛克软件 编辑:程序博客网 时间:2024/06/05 06:23

对于Linux用户操作记录一般通过命令history来查看历史记录,但是如果在由于误操作而删除了重要的数据的情况下,history命令就不会有什么作用了。那么依然要存有历史操作记录应该如何来实现呢?其实我们可以通过登陆IP地址来记录所有用户登录所操作的历史操作!

具体操作就是在/etc/profile.d/里添加配置文件的末尾加入以下脚本代码来实现:

#sudo vi /etc/profile.d/user_all_history.sh

粘贴:

# HistoryUSER=`whoami`USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`if [ "$USER_IP" = "" ]; thenUSER_IP=`hostname`fiif [ ! -d /usr/local/history ]; thenmkdir /usr/local/historychmod 777 /usr/local/historyfiif [ ! -d /usr/local/history/${LOGNAME} ]; thenmkdir /usr/local/history/${LOGNAME}chmod 300 /usr/local/history/${LOGNAME}fiexport HISTSIZE=10000DT=`date +"%Y-%m-%d_%H:%M:%S"`export HISTFILE="/usr/local/history/${LOGNAME}/${USER}@${USER_IP}_history.$DT"chmod 600 /usr/local/history/${LOGNAME}/*history* 2>/dev/null
[root@server ~]# source /etc/profile.d/user_all_history.sh
[root@server ~]# logout

此时需要退出系统再重新登录,在/usr/local/history/目录下才有记录

[root@server ~]# ll /usr/local/history/root/总用量 12-rw------- 1 root root  77 1011 09:09 root@192.168.1.23_history.2012-10-11_09:09:12-rw------- 1 root root 529 1011 09:11 root@192.168.1.23_history.2012-10-11_09:09:16-rw------- 1 root root 187 1011 09:12 root@192.168.1.23_history.2012-10-11_09:11:26

如果想只有root可见可以:

#sudo chmod 400 /etc/profile.d/user_all_history.sh
0 0
原创粉丝点击