linux lsof命令


2.lsof列出进程所打开的文件名

回到顶部
fuser是通过文件名找出占用该文件或者文件系统的进程,而lsof命令则可以查看进程打开的文件或者使用的文件系统。
[root@initroot ~]# lsof [-aUu] [+d]
选项与参数:
-a :多项数据需要同时成立才显示出结果时! -U :仅列出 Unix like 系统的 socket 文件类型; -u :后面接 username,列出该使用者相关进程所开启的文件; +d :后面接目录,亦即找出某个目录底下已经被开启的文件!
列出目前系统上面所有已经被开启的文件与装置:
[root@initroot ~]# lsof
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND    PID  TID            USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
systemd      1                 root  cwd       DIR                8,1      4096          2 /
systemd      1                 root  rtd       DIR                8,1      4096          2 /
systemd      1                 root  txt       REG                8,1   1595792    1049085 /lib/systemd/systemd
systemd      1                 root  mem       REG                8,1   1700792    1053812 /lib/x86_64-linux-gnu/libm-2.27.so
systemd      1                 root  mem       REG                8,1    121016    1049149 /lib/x86_64-linux-gnu/libudev.so.1.6.9
systemd      1                 root  mem       REG                8,1     84032    1053790 /lib/x86_64-linux-gnu/libgpg-error.so.0.22.0
systemd      1                 root  mem       REG                8,1     43304    1053801 /lib/x86_64-linux-gnu/libjson-c.so.3.0.1
systemd      1                 root  mem       REG                8,1     34872     532281 /usr/lib/x86_64-linux-gnu/libargon2.so.0
...(省略)...
如果不加任何参数,lsof默认会将目前系统上面已经开启的文件全部列出来。可以看到目前系统中打开的文件非常多。可以注意到,第一个文件 systemd 执行的 地方就是根目录,而根目录所在的 inode 也有显示出来了!
仅列出关于 root 的所有进程开启的 socket 文件
[root@initroot ~]# lsof -u root -a -U
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND    PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
systemd      1 root   14u  unix 0xffff8c980d33e400      0t0  14487 /run/systemd/notify type=DGRAM
systemd      1 root   15u  unix 0xffff8c980d33e800      0t0  14488 type=DGRAM
systemd      1 root   16u  unix 0xffff8c980d33f400      0t0  14489 type=DGRAM
systemd      1 root   17u  unix 0xffff8c980d33e000      0t0  14490 /run/systemd/private type=STREAM
systemd      1 root   18u  unix 0xffff8c980dc44400      0t0  24013 /run/systemd/journal/stdout type=STREAM
systemd      1 root   19u  unix 0xffff8c980ddb3c00      0t0  24265 /run/systemd/journal/stdout type=STREAM
systemd      1 root   20u  unix 0xffff8c981625c800      0t0  24329 /run/systemd/journal/stdout type=STREAM
systemd      1 root   21u  unix 0xffff8c981625d800      0t0  24330 /run/systemd/journal/stdout type=STREAM
systemd      1 root   23u  unix 0xffff8c981833b400      0t0  23801 /run/systemd/journal/stdout type=STREAM
systemd      1 root   26u  unix 0xffff8c980d33f000      0t0  14502 /run/systemd/journal/stdout type=STREAM
systemd      1 root   27u  unix 0xffff8c980d33fc00      0t0  14504 /run/systemd/journal/socket type=DGRAM
systemd      1 root   32u  unix 0xffff8c980c8d3800      0t0  14543 /run/systemd/coredump type=SEQPACKET
systemd      1 root   33u  unix 0xffff8c980c8d3c00      0t0  14545 /run/systemd/fsck.progress type=STREAM
systemd      1 root   34u  unix 0xffff8c980c8d2000      0t0  14547 /run/udev/control type=SEQPACKET
systemd      1 root   35u  unix 0xffff8c980c8d3400      0t0  14550 /run/systemd/journal/dev-log type=DGRAM
systemd      1 root   37u  unix 0xffff8c980c8d2400      0t0  14559 /run/lvm/lvmpolld.socket type=STREAM
systemd      1 root   38u  unix 0xffff8c980c8d2c00      0t0  14563 /run/systemd/journal/syslog type=DGRAM
systemd      1 root   44u  unix 0xffff8c9816219000      0t0  14569 /run/lvm/lvmetad.socket type=STREAM
systemd      1 root   46u  unix 0xffff8c980ddb2800      0t0  15354 type=DGRAM
systemd      1 root   48u  unix 0xffff8c980dfbf800      0t0  16711 /run/systemd/journal/stdout type=STREAM
...(省略)...
注意到那个 -a 吧!如果你分别输入 lsof -u root 及 lsof -U ,会有啥信息?
使用 lsof -u root -U 及 lsof -u root -a -U ,呵呵!都不同啦!-a 的用途就是在解决同时需要两个项目都成立时啊! ^_^
列出目前系统上面所有的被启动的设备:
[root@initroot ~]# lsof +d /dev
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND    PID            USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd      1            root    0u   CHR    1,3      0t0    6 /dev/null
systemd      1            root    1u   CHR    1,3      0t0    6 /dev/null
systemd      1            root    2u   CHR    1,3      0t0    6 /dev/null
systemd      1            root    3w   CHR   1,11      0t0   12 /dev/kmsg
systemd      1            root   24r   CHR 10,235      0t0  402 /dev/autofs
systemd      1            root   70u   CHR  10,62      0t0    4 /dev/rfkill
kdevtmpfs   15            root  cwd    DIR    0,6     4080    2 /dev
kdevtmpfs   15            root  rtd    DIR    0,6     4080    2 /dev
systemd-j  377            root    0r   CHR    1,3      0t0    6 /dev/null
systemd-j  377            root    1w   CHR    1,3      0t0    6 /dev/null
systemd-j  377            root    2w   CHR    1,3      0t0    6 /dev/null
systemd-j  377            root    7w   CHR   1,11      0t0   12 /dev/kmsg
systemd-j  377            root    9u   CHR   1,11      0t0   12 /dev/kmsg
lvmetad    405            root    0r   CHR    1,3      0t0    6 /dev/null
systemd-u  407            root    0r   CHR    1,3      0t0    6 /dev/null
systemd-r  580 systemd-resolve    0r   CHR    1,3      0t0    6 /dev/null
dbus-daem  586      messagebus    0u   CHR    1,3      0t0    6 /dev/null
            
# 看吧!因为装置都在 /dev 里面嘛!所以啰,使用搜寻目录即可啊!
秀出属于 root 的 bash 进程所开启的文件
[root@initroot ~]# lsof -u root | grep bash
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
bash      6632 root  cwd       DIR                8,1     4096    2621441 /root
bash      6632 root  rtd       DIR                8,1     4096          2 /
bash      6632 root  txt       REG                8,1  1113504    4456451 /bin/bash
bash      6632 root  mem       REG                8,1    47568    1053839 /lib/x86_64-linux-gnu/libnss_files-2.27.so
bash      6632 root  mem       REG                8,1    97176    1053833 /lib/x86_64-linux-gnu/libnsl-2.27.so
bash      6632 root  mem       REG                8,1    47576    1053850 /lib/x86_64-linux-gnu/libnss_nis-2.27.so
bash      6632 root  mem       REG                8,1    39744    1053835 /lib/x86_64-linux-gnu/libnss_compat-2.27.so
bash      6632 root  mem       REG                8,1  4795856     531033 /usr/lib/locale/locale-archive
bash      6632 root  mem       REG                8,1  2030544    1053749 /lib/x86_64-linux-gnu/libc-2.27.so
bash      6632 root  mem       REG                8,1    14560    1053772 /lib/x86_64-linux-gnu/libdl-2.27.so
bash      6632 root  mem       REG                8,1   170784    1053907 /lib/x86_64-linux-gnu/libtinfo.so.5.9
bash      6632 root  mem       REG                8,1   170960    1053721 /lib/x86_64-linux-gnu/ld-2.27.so
bash      6632 root  mem       REG                8,1    26376     790981 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
bash      6632 root    0u      CHR              136,0      0t0          3 /dev/pts/0
bash      6632 root    1u      CHR              136,0      0t0          3 /dev/pts/0
bash      6632 root    2u      CHR              136,0      0t0          3 /dev/pts/0
bash      6632 root  255u      CHR              136,0      0t0          3 /dev/pts/0
这个指令可以找出您想要知道的某个进程是否有启用哪些信息!

initroot编辑整理,转载请注明www.initroot.com

100次点赞 100次阅读