Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 找出在UNIX中哪个用户在什么时间访问了特定文件_Linux_Bash_Unix - Fatal编程技术网

Linux 找出在UNIX中哪个用户在什么时间访问了特定文件

Linux 找出在UNIX中哪个用户在什么时间访问了特定文件,linux,bash,unix,Linux,Bash,Unix,是否有人可以建议我使用任何命令来查看哪个用户在UNIX中何时访问特定文件。我知道history命令列出了以前触发的命令,但它不包括“谁”触发的命令以及在什么时间触发的命令。您可以使用stat查找上次访问文件的时间。这仅在文件系统存储inode的atime时才可能实现。但这并不能告诉您是谁访问了该文件 您可以使用lsof列出当前使用文件的进程。但是,如果您的用户没有足够的权限,则可能看不到其他用户的进程(如果您是root用户,则可以看到所有进程) 通常,history的输出是从执行用户的历史文件生

是否有人可以建议我使用任何命令来查看哪个用户在UNIX中何时访问特定文件。我知道history命令列出了以前触发的命令,但它不包括“谁”触发的命令以及在什么时间触发的命令。

您可以使用
stat
查找上次访问文件的时间。这仅在文件系统存储inode的
atime
时才可能实现。但这并不能告诉您是谁访问了该文件

您可以使用
lsof
列出当前使用文件的进程。但是,如果您的用户没有足够的权限,则可能看不到其他用户的进程(如果您是root用户,则可以看到所有进程)

通常,
history
的输出是从执行用户的历史文件生成的。因此,您可以假设由
history
打印的命令都由同一用户执行。在某些shell中,您可以设置一个选项,以便将执行时间与命令一起存储。然后,您还可以通过
历史记录
获得这一次。这可能取决于您使用的shell


您可以阅读
stat
lsof
bash
zsh
(或者
ksh
?)的手册页来了解更多信息。

您可以在
~/.bashrc
中添加以下几行,以便现在
history
命令 以
[2016-05-11 14:04:33]
格式记录命令。以下命令适用于所有打开的交互式终端

export HISTFILESIZE=100000000     
export HISTSIZE=100000000

# First two are optional, they need to be changed only if the default 500
# lines history logging needs to be changed

export HISTTIMEFORMAT="[$USER %F %T] "
HISTCONTROL=ignoredups:erasedups
shopt -s histappend
PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"

对原始文件进行修改以存储
$USER

对特定文件使用Linux
auditd

示例

假设我有一个文件(让它成为
$HOME/an_important_file.txt
),我想查看对它的所有访问。首先为it设置审核规则:

$ sudo auditctl -w $PWD/an_important_file.txt -p warx -k watch_an_important_file
并检查了审核日志:

$ sudo ausearch -k watch_an_important_file
----
time->Thu May 12 10:54:16 2016
type=CONFIG_CHANGE msg=audit(1463039656.913:278): auid=500 ses=1 subj=unconfined_u:unconfined_r:auditctl_t:s0-s0:c0.c1023 op="add rule" key="watch_an_important_file" list=4 res=1
$ sudo ausearch -k watch_an_important_file
----
time->Thu May 12 10:54:16 2016
type=CONFIG_CHANGE msg=audit(1463039656.913:278): auid=500 ses=1 subj=unconfined_u:unconfined_r:auditctl_t:s0-s0:c0.c1023 op="add rule" key="watch_an_important_file" list=4 res=1
----
time->Thu May 12 10:56:42 2016
type=PATH msg=audit(1463039802.788:291): item=1 name=(null) inode=535849 dev=fd:02 mode=0100664 ouid=500 ogid=500 rdev=00:00 obj=unconfined_u:object_r:user_home_t:s0 nametype=NORMAL
type=PATH msg=audit(1463039802.788:291): item=0 name="/home/Sergey.Kurenkov/" inode=524289 dev=fd:02 mode=040700 ouid=500 ogid=500 rdev=00:00 obj=unconfined_u:object_r:user_home_dir_t:s0 nametype=PARENT
type=CWD msg=audit(1463039802.788:291):  cwd="/usr"
type=SYSCALL msg=audit(1463039802.788:291): arch=c000003e syscall=2 success=yes exit=3 a0=7fff6d986060 a1=941 a2=1b6 a3=3149b8f14c items=2 ppid=4852 pid=10022 auid=500 uid=500 gid=500 euid=500 suid=500 fsuid=500 egid=500 sgid=500 fsgid=500 tty=pts1 ses=1 comm="touch" exe="/bin/touch" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="watch_an_important_file"
然后我用touch修改了这个文件(
$touch$HOME/an\u important\u file.txt
)。我正在再次检查审核日志:

$ sudo ausearch -k watch_an_important_file
----
time->Thu May 12 10:54:16 2016
type=CONFIG_CHANGE msg=audit(1463039656.913:278): auid=500 ses=1 subj=unconfined_u:unconfined_r:auditctl_t:s0-s0:c0.c1023 op="add rule" key="watch_an_important_file" list=4 res=1
$ sudo ausearch -k watch_an_important_file
----
time->Thu May 12 10:54:16 2016
type=CONFIG_CHANGE msg=audit(1463039656.913:278): auid=500 ses=1 subj=unconfined_u:unconfined_r:auditctl_t:s0-s0:c0.c1023 op="add rule" key="watch_an_important_file" list=4 res=1
----
time->Thu May 12 10:56:42 2016
type=PATH msg=audit(1463039802.788:291): item=1 name=(null) inode=535849 dev=fd:02 mode=0100664 ouid=500 ogid=500 rdev=00:00 obj=unconfined_u:object_r:user_home_t:s0 nametype=NORMAL
type=PATH msg=audit(1463039802.788:291): item=0 name="/home/Sergey.Kurenkov/" inode=524289 dev=fd:02 mode=040700 ouid=500 ogid=500 rdev=00:00 obj=unconfined_u:object_r:user_home_dir_t:s0 nametype=PARENT
type=CWD msg=audit(1463039802.788:291):  cwd="/usr"
type=SYSCALL msg=audit(1463039802.788:291): arch=c000003e syscall=2 success=yes exit=3 a0=7fff6d986060 a1=941 a2=1b6 a3=3149b8f14c items=2 ppid=4852 pid=10022 auid=500 uid=500 gid=500 euid=500 suid=500 fsuid=500 egid=500 sgid=500 fsgid=500 tty=pts1 ses=1 comm="touch" exe="/bin/touch" subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 key="watch_an_important_file"

您可以通过
HISTTIMEFORMAT
环境变量记录历史时间。请参见《人类历史》。另请参见@RuslanOsmanov您是否有
历史记录的手册页
?我使用的是ArchLinux,但我没有。对我来说,这是一个内置的
bash
zsh
@Lucas,
人类历史
为我打开
bash\u内置的(1)
手册页。这可以放在
/etc/bash.bashrc
中,以便它适用于所有用户。当然,最好是