rob*_*les 39 linux system-monitoring
我正在尝试找到一种可靠的方法来查找我机器上的哪个进程正在更改配置文件(/etc/hosts具体来说)。
我知道我可以lsof /etc/hosts用来找出当前打开文件的进程,但这无济于事,因为该进程显然是打开文件,写入文件,然后再次关闭它。
我还查看了lsof重复选项 (-r),但它似乎只以每秒一次的速度运行,这可能永远不会捕获正在进行的写入。
我知道有几个用于监视文件系统更改的工具,但在这种情况下,我想知道哪个进程负责,这意味着在行动中捕获它。
use*_*517 57
您可以使用审计来发现这一点。如果尚未可用,请为您的发行版安装并启用审核。
在 /etc/hosts 上设置审计监视
/sbin/auditctl -w /etc/hosts -p war -k hosts-file
-w watch /etc/hosts
-p warx watch for write, attribute change, execute or read events
-k hosts-file is a search key.
Run Code Online (Sandbox Code Playgroud)
等到 hosts 文件更改,然后使用 ausearch 查看记录的内容
/sbin/ausearch -f /etc/hosts | more
Run Code Online (Sandbox Code Playgroud)
你会得到大量的输出,例如
> time->Wed Oct 12 09:34:07 2011 type=PATH
> msg=audit(1318408447.180:870): item=0 name="/etc/hosts" inode=2211062
> dev=fd:00 mode=0100644 ouid=0 ogid=0 rdev=00:00
> obj=system_u:object_r:etc_t:s0 type=CWD msg=audit(1318408447.180:870):
> cwd="/home/iain" type=SYSCALL msg=audit(1318408447.180:870):
> arch=c000003e syscall=2 success=yes exit=0 a0=7fff73641c4f a1=941
> a2=1b6 a3=3e7075310c items=1 **ppid=7259** **pid=7294** au id=1001 uid=0 gid=0
> euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=pts0 ses=123
> comm="touch" **exe="/bin/touch"** subj=user_u:system_r:unconfined_t:s0
> key="hosts-file"
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我使用 touch 命令来更改文件 timstamp,它的 pid 是 7294,它的 ppid 是 7259(我的外壳)。