linux记录用户更新文件,记录Linux系统下所有用户的操作信息

在日常运维中,我们需要清楚服务器上每个用户登录后都做了哪些操作,我们需要记录下每个用户的操作命令。

下面的内容设置可以实现在Linux下所有用户,不管是远程还是本地登陆,在本机的所有操作都会记录下来,并生成包含“用户/IP/时间”的文件存放在指定位置。

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

将上面的内容追加到/etc/profile文件里,source /etc/profile生效后。切换到一个路径后,显示的是全路径,并且终端显示中没有#符号或$符号.

root@elk-node1:[/root]cd /usr/local/src/

root@elk-node1:[/usr/local/src]

上面的显示跟默认的linux终端显示不太习惯。现在要求终端里切换路径后,只显示当前的简介路径,不显示全部路径,并且后面带上#或$符号,那么只需要将上面的第一行PS1参数后面的设置改下:

1)只显示当前简介路径,不显示全路径,显示#号

PS1="[\u@\h \W]"#

2)只显示当前简介路径,不显示全路径,显示$号

PS1="[\u@\h \W]\$"

这里,我选择第(1)种带#号显示,生效后的终端显示内容和linux默认显示的一样,如下:

[root@elk-node1]#cd /usr/local/

[root@elk-node1 local]#pwd                 #显示的只是当前简介路径local,而不是全路径/usr/local

/usr/local

[root@elk-node1 local]#

这样,各个用户的操作记录的日志路径如下:

[root@elk-node1 history]#pwd

/tmp/history

[root@elk-node1 history]#ll

total 4

d-wx------. 2 root root 82 Oct 21 19:44 root

d-wx------. 2 wangshibo wangshibo 4096 Oct 21 20:00 wangshibo

[root@elk-node1 history]#cd wangshibo/

[root@elk-node1 wangshibo]#ll

total 12

-rw-------. 1 wangshibo wangshibo 74 Oct 21 19:11 gateway history.20161021_190706

-rw-------. 1 wangshibo wangshibo 22 Oct 21 19:28 gateway history.20161021_192848

-rw-------. 1 wangshibo wangshibo 23 Oct 21 20:00 gateway history.20161021_200045

[root@elk-node1 wangshibo]#tail -f gateway\ history.20161021_200045

touch test

rm -f test

---------------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------

还有一种方案:

这个方案会在每个用户退出登录 时把用户所执行的每一个命令都发送给日志守护进程rsyslogd,你也可通过配置“/etc/rsyslog.conf”进一步将日志发送给日志服务器

操作如下:

把下面内容添加到/etc/profile文件底部

[root@elk-node1 ~]# vim /etc/profile

........

#设置history格式

export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`who am i 2>/dev/null| \

awk '{print $NF}'|sed -e 's/[()]//g'`] "

#登录时清空当前缓存

echo "" > .bash_history

#记录shell执行的每一条命令

export PROMPT_COMMAND='\

if [ -z "$OLD_PWD" ];then

export OLD_PWD=$PWD;

fi;

if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then

logger -t `whoami`_shell_cmd "[$OLD_PWD]$(history 1)";

fi ;

export LAST_CMD="$(history 1)";

export OLD_PWD=$PWD;'

[root@elk-node1 ~]# sudo /etc/profile

测试:

分别使用wangshibo,guohuihui账号登陆这台服务器进行一些操作。

然后发现/var/log/message日志文件中已经记录了这两个用户的各自操作了~

[root@elk-node2 ~]# tail -20 /var/log/messages

Oct 24 14:16:04 elk-node2 root_shell_cmd: [/root] 321 [2016-10-24 14:16:04] [gateway] tail -100 /var/log/messages

Oct 24 14:19:09 elk-node2 root_shell_cmd: [/root] 322 [2016-10-24 14:18:51] [gateway] vim /etc/profile

Oct 24 14:19:12 elk-node2 su: (to wangshibo) root on pts/0

Oct 24 14:19:25 elk-node2 root_shell_cmd: [/root] 315 [2016-10-24 14:19:23] [gateway] tail -f /var/log/messages

Oct 24 14:19:40 elk-node2 wangshibo_shell_cmd: [/home/wangshibo] 2 [2016-10-24 14:19:40] [gateway] echo "123456" > test

Oct 24 14:19:43 elk-node2 wangshibo_shell_cmd: [/home/wangshibo] 3 [2016-10-24 14:19:43] [gateway] uptime

Oct 24 14:19:45 elk-node2 wangshibo_shell_cmd: [/home/wangshibo] 4 [2016-10-24 14:19:45] [gateway] who

Oct 24 14:19:47 elk-node2 wangshibo_shell_cmd: [/home/wangshibo] 5 [2016-10-24 14:19:47] [gateway] last

Oct 24 14:19:48 elk-node2 root_shell_cmd: [/root] 323 [2016-10-24 14:19:12] [gateway] su - wangshibo

Oct 24 14:19:52 elk-node2 su: (to guohuihui) root on pts/0

Oct 24 14:20:00 elk-node2 guohuihui_shell_cmd: [/usr/local/src] 2 [2016-10-24 14:20:00] [gateway] ls

Oct 24 14:20:03 elk-node2 guohuihui_shell_cmd: [/usr/local/src] 3 [2016-10-24 14:20:03] [gateway] date

Oct 24 14:20:11 elk-node2 guohuihui_shell_cmd: [/usr/local/src] 4 [2016-10-24 14:20:11] [gateway] free -m

Oct 24 14:20:12 elk-node2 root_shell_cmd: [/root] 324 [2016-10-24 14:19:52] [gateway] su - guohuihui

Oct 24 14:20:23 elk-node2 root_shell_cmd: [/root] 316 [2016-10-24 14:20:18] [gateway] tail -f /etc/sudoers

Oct 24 14:20:30 elk-node2 root_shell_cmd: [/root] 317 [2016-10-24 14:20:24] [gateway] tail -f /var/log/messages

Oct 24 14:20:35 elk-node2 root_shell_cmd: [/root] 318 [2016-10-24 14:20:35] [gateway] tail -100 /var/log/messages

Oct 24 14:20:46 elk-node2 su: (to wangshibo) root on pts/0

Oct 24 14:23:42 elk-node2 root_shell_cmd: [/root] 325 [2016-10-24 14:20:46] [gateway] su - wangshibo

Oct 24 14:23:45 elk-node2 root_shell_cmd: [/root] 326 [2016-10-24 14:23:45] [gateway] cat /etc/profile

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值