lsof命令详解

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

适应条件:lsof访问的是核心文件和各种文件,所以必须以root用户的身份运行才能充分发挥其功能。

lsof [选项] [绝对路径的文件名]

显示示例

[root@localhost ~]# lsof /usr/sbin/httpd

COMMAND  PID  USER  FD  TYPE DEVICE SIZE/OFF  NODE NAME

httpd6279root txt    REG8,2344112415135/usr/sbin/httpd

httpd6281apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6282apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6283apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6284apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6285apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6286apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6287apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6288apache txt    REG8,2344112415135/usr/sbin/httpd

httpd6546apache txt    REG8,2344112415135/usr/sbin/httpd

每行显示一个打开的文件,默认如果后面不跟任何东西,将打开系统打开的所有文件

COMMAND :进程名称

PID:进程标识符

USER:进程所有者

FD:文件描述符,应用程序通过文件描述符识别到该文件。如cwd、txt等

TYPE:文件类型,如DIR,REG

DEVICE:指定磁盘名称

SIZE:文件大小

NODE:索引节点(文件在磁盘上的标识)

NAME:打开文件的确切名称

补充:FD列中的文件描述cwd值表示应用程序的当前工作目录,这是该程序启动的目录,除非它本身对这个目录进行更改。txt类型的是程序代码,如应用程序二进制文件本身或者共享库。其

次数值表示应用程序的文件描述符,这是打开文件时一个返回的一个整数。

lsof6660root0uCHR136,00t03/dev/pts/0lsof6660root1uCHR136,00t03/dev/pts/0lsof6660root2uCHR136,00t03/dev/pts/0lsof6660root    3r      DIR0,301/proc

lsof6660root    4r      DIR0,3036358/proc/6660/fd

lsof6660root    5wFIFO0,80t036363pipe

lsof6660root    6r    FIFO0,80t036364pipe

lsof6661root  cwd      DIR8,24096130562/root

lsof6661root  rtd      DIR8,240962/lsof6661root  txt      REG8,2154356415242/usr/sbin/lsof

lsof6661root  mem      REG8,21907156914957/lib/libc-2.12.so

lsof6661root  mem      REG8,217892914963/lib/libdl-2.12.so

lsof6661root  mem      REG8,2141080914950/lib/ld-2.12.so

lsof6661root  mem      REG8,2120780915040/lib/libselinux.so.1lsof6661root  mem      REG8,299154448395123/usr/lib/locale/locale-archive

lsof6661root    4r    FIFO0,80t036363pipe

lsof6661root    7wFIFO0,80t036364pipe

其中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/1May1208:04:11localhost kernel: hpet1: lost3rtc interrupts

May1208:04:11localhost kernel: hpet1: lost6rtc interrupts

May1208:04:11localhost kernel: hpet1: lost1rtc interrupts

May1209:25:33localhost kernel: usb2-2.1: USB disconnect, device number10May1209:25:33localhost kernel: eth0: link down

May1209:25:33localhost kernel: usb2-2.1: new full speed USB device number11using uhci_hcd

May1209:25:33localhost kernel: usb2-2.1: New USB device found, idVendor=0e0f, idProduct=0008May1209:25:33localhost kernel: usb2-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3May1209:25:33localhost kernel: usb2-2.1: Product: Virtual Bluetooth Adapter

May1209:25:33localhost kernel: usb2-2.1: Manufacturer: VMware

May1209:25:33localhost kernel: usb2-2.1: SerialNumber:000650268328May1209:25:33localhost kernel: usb2-2.1: configuration #1chosen from1choice

最后通过重定向的方法恢复被删除的/var/log/messages

cat /pro/5925/fd/1 >/var/log/messages

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容