如何检测 Linux 上文件所有权的变化?

Col*_*ill 4 linux diagnostic file-permissions

我继承了一堆 Linux (Ubuntu Precise) 服务器,并且目前经常遇到将文件夹所有权更改为“root”的问题。我们运行 puppet,它将所有权更改为它应该是的用户,但稍后会发生其他事情将其更改回来。

我目前每 30 秒记录一次文件的权限,以尝试缩小时间以查看日志中是否有任何内容等。这是一个大型繁忙的服务器,因此没有更多信息就不容易在日志中找到任何内容。

Linux 中有没有办法在文件/文件夹所有权发生变化时捕获并检测负责的进程?

Pra*_*era 10

我认为您可以对特定文件/目录使用审核,也可以根据您的要求编写自定义规则

        auditctl -w <path to the file you need to monitor> -p war -k test

        Where -w is for specifying file path
        -p is for permission access (read,write,execute and attribute change)
        -k key name,you can give name you can use to filter audit rule
Run Code Online (Sandbox Code Playgroud)

然后你可以使用搜索它

        ausearch -ts today -k test
Run Code Online (Sandbox Code Playgroud)

例如我使用这个,创建这个文件 /tmp/test 然后写入一些随机数据

       auditctl -w /tmp/test -p warx -k test
Run Code Online (Sandbox Code Playgroud)

然后执行这个命令

       ausearch -ts today -k test

      --ts for start date
      -k is for key string
Run Code Online (Sandbox Code Playgroud)

所以这个输出

  type=SYSCALL msg=audit(1407949301.821:63216): arch=c000003e syscall=191 success=no
  exit=-61 a0=eacca0 a1=3600005db7 a2=7fff15265180 a3=84 items=1 ppid=2384 pid=16921
  auid=0 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=10096
  comm="vim" exe="/usr/bin/vim" key="test"
Run Code Online (Sandbox Code Playgroud)

因此,如果您检查输出的最后一行,它将显示执行的命令是 vim 并且 uid=0 是 root

如果您想让这些更改在重启后保持不变,请在 /etc/audit/audit.rules 中添加这样的条目

  -w /tmp/test -p warx -k test
Run Code Online (Sandbox Code Playgroud)

并确保 auditd 服务已启动并正在运行

  service auditd status 
Run Code Online (Sandbox Code Playgroud)

有关更多信息,您可以参考http://www.cyberciti.biz/tips/linux-audit-files-to-see-who-made-changes-to-a-file.html