找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 279|回复: 2

Linux命令之lsof命令 [list open files]

[复制链接]

146

主题

17

回帖

550

积分

超级版主

积分
550
发表于 2024-1-7 17:59:36 | 显示全部楼层 |阅读模式

lsof (list open files)是一个列出当前系统打开文件的工具。在linux系统环境下,任何事物都可以以文件形式存在,通过文件不仅可以访问常规的数据,还可以访问网络连接和硬件。

适应条件:lsof访问的是核心文件和各种文件,所以必须以root用户的身份运行才能充分发挥其功能。
lsof [选项] [绝对路径的文件名]
显示示例
  • [root@localhost ~]# lsof /usr/sbin/httpd
  • COMMAND  PID   USER  FD   TYPE DEVICE SIZE/OFF   NODE NAME
  • httpd   6279   root txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6281 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6282 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6283 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6284 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6285 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6286 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6287 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6288 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd
  • httpd   6546 apache txt    REG    8,2   344112 415135 /usr/sbin/httpd


146

主题

17

回帖

550

积分

超级版主

积分
550
 楼主| 发表于 2024-1-7 17:59:50 | 显示全部楼层
每行显示一个打开的文件,默认如果后面不跟任何东西,将打开系统打开的所有文件
COMMAND :进程名称
PID:进程标识符
USER:进程所有者
FD:文件描述符,应用程序通过文件描述符识别到该文件。如cwd、txt等
TYPE:文件类型,如DIR,REG
DEVICE:指定磁盘名称
SIZE:文件大小
NODE:索引节点(文件在磁盘上的标识)
NAME:打开文件的确切名称
补充:FD列中的文件描述cwd值表示应用程序的当前工作目录,这是该程序启动的目录,除非它本身对这个目录进行更改。txt类型的是程序代码,如应用程序二进制文件本身或者共享库。其
次数值表示应用程序的文件描述符,这是打开文件时一个返回的一个整数。

    lsof      6660      root    0u      CHR      136,0      0t0          3 /dev/pts/0
    lsof      6660      root    1u      CHR      136,0      0t0          3 /dev/pts/0
    lsof      6660      root    2u      CHR      136,0      0t0          3 /dev/pts/0
    lsof      6660      root    3r      DIR        0,3        0          1 /proc
    lsof      6660      root    4r      DIR        0,3        0      36358 /proc/6660/fd
    lsof      6660      root    5w     FIFO        0,8      0t0      36363 pipe
    lsof      6660      root    6r     FIFO        0,8      0t0      36364 pipe
    lsof      6661      root  cwd       DIR        8,2     4096     130562 /root
    lsof      6661      root  rtd       DIR        8,2     4096          2 /
    lsof      6661      root  txt       REG        8,2   154356     415242 /usr/sbin/lsof
    lsof      6661      root  mem       REG        8,2  1907156     914957 /lib/libc-2.12.so
    lsof      6661      root  mem       REG        8,2    17892     914963 /lib/libdl-2.12.so
    lsof      6661      root  mem       REG        8,2   141080     914950 /lib/ld-2.12.so
    lsof      6661      root  mem       REG        8,2   120780     915040 /lib/libselinux.so.1
    lsof      6661      root  mem       REG        8,2 99154448     395123 /usr/lib/locale/locale-archive
    lsof      6661      root    4r     FIFO        0,8      0t0      36363 pipe
    lsof      6661      root    7w     FIFO        0,8      0t0      36364 pipe

146

主题

17

回帖

550

积分

超级版主

积分
550
 楼主| 发表于 2024-1-7 18:00:19 | 显示全部楼层
其中u表示该文件被打开处于读取\写入模式,而不是只读或只写模式;
    r 只读 ; w 只写 ;W表示该应用程序具有对整个文件的写锁(确保每次只能打开一次应用程序实例)
初始打开每个应用程序时,都具有三个文件描述符,从0到2,分别表示标准输入、输出和错误流。因此,大多数应用程序
所打开的FD都是从3开始
TYPE:REG、DIR、CHR、BLK、UNIX、FIFO、IPV4
(2)查看端口现在运行的情况
ls -i:port  #某个端口
ls -i:port1-port2 #
ls -i:1-1024      #查看端口1-1024运行情况
(3)恢复删除文件
当系统中的某个文件被意外删除了,只要这个时候系统中有进程正在访问这个文件,那么可以通过lsof 从/proc目录下恢复文件的内容
假如/var/log/messages文件被删了,恢复这个文件的方法:
首先使用lsof 查看当前是否有进程打开/var/log/messages文件,
#lsof |grep /var/log/messages
[root@localhost ~]# rm /var/log/messages
rm:是否删除普通文件 "/var/log/messages"?y
[root@localhost ~]# lsof |grep /var/log/messages
rsyslogd  5925      root    1w      REG        8,2     4369     266184 /var/log/messages (deleted)
从上面的信息可以看到PID 5925(syslogd)打开文件的文件描述符为1,同时发现/var/log/messages已经被删除了。
因此可以通过/var/log/messages文件描述符来查看文件信息。

    cat /pro/5925/fd/1
    [root@localhost ~]# cat /proc/5925/fd/1
    May 12 08:04:11 localhost kernel: hpet1: lost 3 rtc interrupts
    May 12 08:04:11 localhost kernel: hpet1: lost 6 rtc interrupts
    May 12 08:04:11 localhost kernel: hpet1: lost 1 rtc interrupts
    May 12 09:25:33 localhost kernel: usb 2-2.1: USB disconnect, device number 10
    May 12 09:25:33 localhost kernel: eth0: link down
    May 12 09:25:33 localhost kernel: usb 2-2.1: new full speed USB device number 11 using uhci_hcd
    May 12 09:25:33 localhost kernel: usb 2-2.1: New USB device found, idVendor=0e0f, idProduct=0008
    May 12 09:25:33 localhost kernel: usb 2-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    May 12 09:25:33 localhost kernel: usb 2-2.1: Product: Virtual Bluetooth Adapter
    May 12 09:25:33 localhost kernel: usb 2-2.1: Manufacturer: VMware
    May 12 09:25:33 localhost kernel: usb 2-2.1: SerialNumber: 000650268328
    May 12 09:25:33 localhost kernel: usb 2-2.1: configuration #1 chosen from 1 choice

最后通过重定向的方法恢复被删除的/var/log/messages
cat /pro/5925/fd/1 >/var/log/messages
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|333cm ( 京ICP备16037542号 )

GMT+8, 2024-5-21 01:08 , Processed in 0.085370 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表