inotify - 如何找出哪个用户修改了文件?

发布于 2024-11-27 18:08:14 字数 238 浏览 6 评论 0原文

我正在寻找有关如何找出哪个用户修改了特定文件的指南。虽然 inotify 非常适合在触摸特定文件时收到通知,但我如何确定哪个用户修改了该文件?我可以考虑使用 lsof ,但我担心它可能不像我想要的那样“实时”和/或者它可能对资源造成太大的负担。我所说的实时是指,如果用户只是在文件上执行 touch 命令,那么当我在文件上运行 lsof 时,可能无法获取该命令。 >lsof

I'm looking for guidance on how to find out which user has modified a particular file. While inotify is great to get notification when a particular file is touched, how do I figure out which user has modified that file? I can think of using lsof but I'm afraid that it may not be as "realtime" as I want and/or it might be too much of a tax on resources. By realtime, I mean that if a user simply executes a touch command on a file, by the time I run lsof on file, it may not be picked up by lsof.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的心爱 2024-12-04 18:08:14

您可以使用审计守护进程

sudo apt-get install auditd

选择要监控的文件

touch /tmp/myfile

添加审计用于写入和属性更改(-p wa):

sudo auditctl -w /tmp/myfile -p wa -k my-file-changed

文件被某个用户触摸:

touch /tmp/myfile

检查审核日志:

sudo ausearch -k my-file-changed | tail -1

您可以在以下位置看到运行该命令的用户的UID输出

type=SYSCALL msg=audit(1313055675.066:57): arch=c000003e syscall=2
成功=是退出=3 a0=7ffffb6744dd a1=941 a2=1b6 a3=7ffffb673bb0
items=1 ppid=3428 pid=4793 auid=4294967295 uid=1000 gid=1000 euid=1000
suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts1
ses=4294967295 comm="touch" exe="/bin/touch" key="my-file-changed"

有关使用详细信息,请参阅 手册页 或此 示例指南

You can use audit deamon:

sudo apt-get install auditd

Choose a file to monitor

touch /tmp/myfile

Add audit for write and attribute change (-p wa):

sudo auditctl -w /tmp/myfile -p wa -k my-file-changed

The file is touched by some user:

touch /tmp/myfile

Check audit logs:

sudo ausearch -k my-file-changed | tail -1

You can see the UID of the user who run the command in the output

type=SYSCALL msg=audit(1313055675.066:57): arch=c000003e syscall=2
success=yes exit=3 a0=7ffffb6744dd a1=941 a2=1b6 a3=7ffffb673bb0
items=1 ppid=3428 pid=4793 auid=4294967295 uid=1000 gid=1000 euid=1000
suid=1000 fsuid=1000 egid=1000 sgid=1000 fsgid=1000 tty=pts1
ses=4294967295 comm="touch" exe="/bin/touch" key="my-file-changed"

For details of usage see man pages or this sample guide.

允世 2024-12-04 18:08:14

如果您在前面的命令中添加 -i 选项,您将以更易于理解的格式获得输出。您将在服务器中将 uid 转换为真实用户名。

ausearch -k my-file-changed -i | tail -1

If you add -i option in the earlier command, you will get output in more human readable format. You will get the uid converted to the real username in the server.

ausearch -k my-file-changed -i | tail -1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文