lsof(list open files)是一个查看当前系统文件的工具。在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件。如传输控制协议 (TCP) 和用户数据报协议 (UDP) 套接字等,系统在后台都为该应用程序分配了一个文件描述符,该文件描述符提供了大量关于这个应用程序本身的信息。
lsof打开的文件可以是:
- 普通文件
- 目录
- 网络文件系统的文件
- 字符或设备文件
- (函数)共享库
- 管道,命名管道
- 符号链接
- 网络文件(例如:NFS file、网络socket,unix域名socket)
- 还有其它类型的文件,等等
命令参数
-a 列出打开文件存在的进程
-c<进程名> 列出指定进程所打开的文件
-g 列出GID号进程详情
-d<文件号> 列出占用该文件号的进程
+d<目录> 列出目录下被打开的文件
+D<目录> 递归列出目录下被打开的文件
-n<目录> 列出使用NFS的文件
-i<条件> 列出符合条件的进程。(4、6、协议、:端口、 @ip )
-p<进程号> 列出指定进程号所打开的文件
-u 列出UID号进程详情
-h 显示帮助信息
-v 显示版本信息
说明:
1.查看某个目录的进程打开
[root@y-3c-a018 fwj]# lsof +D /opt/kf5/
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
superviso 7867 root 15w REG 8,3 98336 2675942 /opt/kf5/log/robot_offline_info.log
superviso 7867 root 18w REG 8,3 4464 2681975 /opt/kf5/log/robot_new_info.log
superviso 7867 root 21w REG 8,3 24828 947215 /opt/kf5/log/im.log
superviso 7867 root 24w REG 8,3 293 2675940 /opt/kf5/log/robot_pending_info.log
superviso 7867 root 27w REG 8,3 294 2675938 /opt/kf5/log/robot_open_info.log
python3 7869 root 3w REG 8,3 671328 2675937 /opt/kf5/log/kf5.log
python3 7870 root 3w REG 8,3 671328 2675937 /opt/kf5/log/kf5.log
python3 7873 root 3w REG 8,3 671328 2675937 /opt/kf5/log/kf5.log
python3 7875 root 3w REG 8,3 671328 2675937 /opt/kf5/log/kf5.log
python3 7876 root 3w REG 8,3 671328 2675937 /opt/kf5/log/kf5.log
python3 7877 root 3w REG 8,3 671328 2675937 /opt/kf5/log/kf5.log
python3 7878 root 3w REG 8,3 671328 2675937 /opt/kf5/log/kf5.log
lsof输出各列信息的意义如下:
COMMAND:进程的名称
PID:进程标识符
PPID:父进程标识符(需要指定-R参数)
USER:进程所有者
PGID:进程所属组
FD:文件描述符,应用程序通过文件描述符识别该文件。如cwd、txt等:
(1)cwd:表示current work dirctory,即:应用程序的当前工作目录,这是该应用程序启动的目录,除非它本身对这个目录进行更改
(2)txt :该类型的文件是程序代码,如应用程序二进制文件本身或共享库,如上列表中显示的 /sbin/init 程序
(3)lnn:library references (AIX);
(4)er:FD information error (see NAME column);
(5)jld:jail directory (FreeBSD);
(6)ltx:shared library text (code and data);
(7)mxx :hex memory-mapped type number xx.
(8)m86:DOS Merge mapped file;
(9)mem:memory-mapped file;
(10)mmap:memory-mapped device;
(11)pd:parent directory;
(12)rtd:root directory;
(13)tr:kernel trace file (OpenBSD);
(14)v86 VP/ix mapped file;
(15)0:表示标准输入
(16)1:表示标准输出
(17)2:表示标准错误
一般在标准输出、标准错误、标准输入后还跟着文件状态模式:r、w、u等
(1)u:表示该文件被打开并处于读取/写入模式
(2)r:表示该文件被打开并处于只读模式
(3)w:表示该文件被打开并处于
(4)空格:表示该文件的状态模式为unknow,且没有锁定
(5)-:表示该文件的状态模式为unknow,且被锁定
同时在文件状态模式后面,还跟着相关的锁
(1)N:for a Solaris NFS lock of unknown type;
(2)r:for read lock on part of the file;
(3)R:for a read lock on the entire file;
(4)w:for a write lock on part of the file;(文件的部分写锁)
(5)W:for a write lock on the entire file;(整个文件的写锁)
(6)u:for a read and write lock of any length;
(7)U:for a lock of unknown type;
(8)x:for an SCO OpenServer Xenix lock on part of the file;
(9)X:for an SCO OpenServer Xenix lock on the entire file;
(10)space:if there is no lock.
TYPE列有以下常见取值:
REG:一般文件
DIR:目录
CHR:字符设备
BLK:块设备
FIFO:命名管道
PIPE:管道
IPV4:ipv4套接字
unix:unix域套接字
使用
1.列出所有打开的文件:
lsof
备注: 如果不加任何参数,就会打开所有被打开的文件,建议加上一下参数来具体定位
2.查看谁正在使用某个文件
lsof /filepath/file
3.递归查看某个目录的文件信息
lsof +D /filepath/filepath2/
备注: 使用了+D,对应目录下的所有子目录和文件都会被列出
4.比使用+D选项,遍历查看某个目录的所有文件信息 的方法
lsof | grep ‘/filepath/filepath2/’
5.列出某个用户打开的文件信息
lsof -u username
备注: -u 选项,u其实是user的缩写
6.列出某个程序所打开的文件信息
lsof -c mysql
备注: -c 选项将会列出所有以mysql开头的程序的文件,其实你也可以写成 lsof | grep mysql, 但是第一种方法明显比第二种方法要少打几个字符了
7.列出多个程序多打开的文件信息
lsof -c mysql -c apache
8.列出某个用户以及某个程序所打开的文件信息
lsof -u test -c mysql
9.列出除了某个用户外的被打开的文件信息
lsof -u ^root
备注:^这个符号在用户名之前,将会把是root用户打开的进程不让显示
10.通过某个进程号显示该进行打开的文件
lsof -p 1
11.列出多个进程号对应的文件信息
lsof -p 123,456,789
12.列出除了某个进程号,其他进程号所打开的文件信息
lsof -p ^1
13 .列出所有的网络连接
lsof -i
14.列出所有tcp 网络连接信息
lsof -i tcp
15.列出所有udp网络连接信息
lsof -i udp
16.列出谁在使用某个端口
lsof -i :3306
17.列出谁在使用某个特定的udp端口
lsof -i udp:55
特定的tcp端口
lsof -i tcp:80
18.列出某个用户的所有活跃的网络端口
lsof -a -u test -i
19.列出所有网络文件系统
lsof -N
20.域名socket文件
lsof -u
21.某个用户组所打开的文件信息
lsof -g 5555
22.根据文件描述列出对应的文件信息
lsof -d description(like 2)
23.根据文件描述范围列出文件信息
lsof -d 2-3
几个疑问
1.FD 那一栏15w 是什么意思?
如图所示 15 代表该进程当前的文件描述符,可以通过 cat /proc/7867/fd/15去查看当前进程调用的 FD 信息,通常 Linux 的文件描述符从3开始 0、1、2 代表输入 输出还有错误输出。
2.DEVICE 这一栏,8,3 是什么意思 我还不清楚,不过 man 看了下 lsof 的手册
DEVICE contains the device numbers, separated by commas, for a character special, block special, regular, directory or NFS file;
or ``memory'' for a memory file system node under Tru64 UNIX;
or the address of the private data area of a Solaris socket stream;
or a kernel reference address that identifies the file (The kernel reference address may be used for FIFO's, for example.);
or the base address or device name of a Linux AX.25 socket device.
Usually only the lower thirty two bits of Tru64 UNIX kernel addresses are displayed.
cat /proc/devices 发现
Block devices:
259 blkext
8 sd
9 md
65 sd
66 sd
67 sd
68 sd
69 sd
70 sd
71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
253 device-mapper
254 mdp
我猜这个8,3 应该是表示这是一个块设备,3表示该设备的编号,通过 fdisk 查看 /dev/sda3 编号是3
进入/dev 目录 ll 查看,果然是这样。
[root@y-3c-a018 dev]# ll | grep sda3
brw-rw---- 1 root disk 8, 3 Aug 12 10:54 sda3