识别进程打开的文件
在这一步中,你将学习如何使用 lsof 命令识别与特定进程关联的打开文件。
首先,我们需要找到正在运行的进程的进程 ID(PID)。你可以使用 ps 命令来完成这一操作:
sudo ps -ef | grep nginx
示例输出:
root 825 1 0 14:32 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 826 825 0 14:32 ? 00:00:00 nginx: worker process
www-data 827 825 0 14:32 ? 00:00:00 nginx: worker process
在这个例子中,nginx 进程的 PID 是 825。
接下来,你可以使用 lsof 命令列出与该进程关联的所有打开文件:
sudo lsof -p 825
示例输出:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 825 root cwd DIR 253,0 4096 1048576 /usr/sbin
nginx 825 root rtd DIR 253,0 4096 2 /
nginx 825 root txt REG 253,0 977528 1048577 /usr/sbin/nginx
nginx 825 root mem REG 253,0 2067688 1048578 /usr/lib/x86_64-linux-gnu/libc-2.35.so
nginx 825 root mem REG 253,0 169032 1048579 /usr/lib/x86_64-linux-gnu/ld-2.35.so
nginx 825 root 0u CHR 136,0 0t0 3 /dev/pts/0
nginx 825 root 1u CHR 136,0 0t0 3 /dev/pts/0
nginx 825 root 2u CHR 136,0 0t0 3 /dev/pts/0
这个输出显示了与 nginx 进程关联的所有打开文件,包括可执行文件、共享库以及标准输入/输出/错误文件描述符。
你还可以使用 lsof 命令查找特定用户打开的文件。例如,查找 labex 用户拥有的所有打开文件:
sudo lsof -u labex
这对于排查问题或了解系统中特定用户的活动非常有用。
在下一步中,你将学习如何使用 lsof 命令定位网络连接。