1. lsof 简介
2. 输出列
2.1 FD 文件描述符
2.2 Type 文件类型
3. 常用选项
4. 示例
4.1 manpage-Examples
5. 特别用法
5.1 恢复删除的文件
5.2 杀死某个特定用户的所有活动
5.3 找回被删除文件占用的空间
1. lsof 简介
lsof(list open files)列出当前系统打开的文件.
进程打开的文件; 打开文件的进程; 进程打开的端口(TCP,UDP); 等...
由于 Linux 系统一切皆文件, 所以 lsof 打开的文件可以是:
a regular file, 常规文件
a directory, 目录
a block special file, 块特殊文件
a character special file, 字符特殊文件
an executing text reference, 执行文本参考,
a library, 库文件
a stream or a network file (Internet socket, NFS file or UNIX domain socket.) 流或网络文件(网络 socket,NFS 文件, UNIX 域 socket)
A specific file or all the files in a file system may be selected by path.
可用选项: $ lsof -h
详细说明: $ man lsof
2. 输出列
Lsof 每次运行时动态调整输出列的大小, 保证每列的最小大小. 它还保证每列与其前一列至少隔开一个空格.
Lsof 不会为每个进程或文件集生成所有字段, 只会生成可用的字段. 某些字段是互斥的:
文件设备字符和文件主要 / 次要设备号;
文件 inode 编号和协议名称;
文件名和流识别;
文件大小和偏移量.(-s/-o)
这些互斥集合中的一个或另一个成员将出现在字段输出中, 但不会出现在两者中.
$ lsof |Less
COMMAND 命令名(默认长度为 9,+c w 可设置长度, arch Linux 最大 15);
PID 进程 ID(-p 指定 PID);
TID 线程 ID;
TASKCMD 任务名(-K?);
PPID 父进程号(仅在使用 - R 时显示)
PGID 与进程关联的进程组号(仅在使用 - g 时显示)
USER 用户(-u 指定用户);
FD 文件描述符 (详见下表)(-d 排除或包含 FD 字段);
TYPE 与文件关联的节点的类型 (详见下表);
当 n 被指定为 + f 时, NODE-ID(或某些方言的 INODE-ADDR)包含文件节点的唯一标识符(通常是内核 vnode 或 inode 地址, 但有时也是设备和节点号的串联);
DEVICE 指定磁盘的名称;
SIZE/OFF 文件的大小 / 偏移量(-s/-o);
NLINK 包含指定 + L 时的文件链接计数;
NODE 索引节点(文件在磁盘上的标识);
NAME 打开文件的确切名称;
2.1 FD 文件描述符
FD | FD 是文件的文件描述符编号或: | wc -l | |
cwd | current working directory; | 当前工作目录 | 381 |
txt | program text (code and data); | 程序文件或共享库 | 381 |
rtd | root directory; | root 目录 | 381 |
mem | memory-mapped file; | 内存映射文件; | 36728 |
mmap | memory-mapped device; | 内存映射设备; | |
Lnn | library references (AIX); | 库引用(AIX); | |
jld | jail directory (FreeBSD); | jail 目录(FreeBSD); | |
ltx | shared library text (code and data); | 共享库文本(代码和数据); | |
Mxx | hex memory-mapped type number xx. | 十六进制内存映射类型号 xx。 | |
m86 | DOS Merge mapped file; | DOS 合并映射文件; | |
err | information error (see NAME column); | 错误的 FD 信息错误; | |
pd | parent directory; | 父目录; | |
tr | kernel trace file (OpenBSD); | 内核跟踪文件 (OpenBSD); | |
v86 | VP/ix mapped file; | VP/ix 映射文件; | |
DEL | 3743 | ||
Mode | FD 后跟其中一个字符, 描述文件打开的模式: | ||
u | u for read and write access; | 文件处于读取 / 写入模式 | 128:5678 |
r | r for read access; | 文件处于只读模式 | 78:2924 |
w | w for write access; | 文件处于写入模式 | 92:2633 |
‘ ‘ | space if mode unknown and no lock | 未知模式, 且未锁定 | |
- | ’-’ if mode unknown and lock | 未知模式, 并锁定 | |
Lock | 模式字符后跟一个锁定字符, 描述应用于文件的锁定类型: | ||
N | N for a Solaris NFS lock of unknown type; | 未知类型的 Solaris NFS 锁; | |
r | r for read lock on part of the file; | r 用于部分文件的读锁定; | |
R | R for a read lock on the entire file; | R 表示整个文件的读锁定; | |
w | w for a write lock on part of the file; | w 对文件的一部分进行写锁定; | |
W | W for a write lock on the entire file; | W 表示整个文件的写锁定; | |
u | u for a read and write lock of any length; | u 表示任何长度的读写锁; | |
U | U for a lock of unknown type; | U 表示未知类型的锁; | |
x | x for an SCO OpenServer Xenix lock on part of the file; | 部分文件的 SCO OpenServer Xenix 锁; | |
X | X for an SCO OpenServer Xenix lock on the entire file; | 整个文件的 SCO OpenServer Xenix 锁; | |
‘ ‘ | space if there is no lock. | 空格, 没有锁。 |
2.2 Type 文件类型
TYPE | is the type of the node associated with the file | TYPE 是与文件关联的节点的类型 | wc-l |
BLK | for a block special file | 用于块特殊文件 | 1 |
CHR | for a character special file | 用于字符特殊文件 | 780 |
DIR | for a directory | 用于目录 | 770 |
REG | for a regular file | 用于常规文件 | 46350 |
FIFO | for a FIFO special file | 用于 FIFO 特殊文件 | 903 |
IPv4 | for an IPv4 socket | 用于 IPv4 套接字 | 7 |
IPv6 | for an open IPv6 network file - even if its address is IPv4, mapped in an IPv6 address | 用于开放的 IPv6 网络文件 - 即使其地址是 IPv4, 也映射在 IPv6 地址中 | 22 |
sock | for a socket of unknown domain | 用于未知域的套接字 | 217 |
unix | for a UNIX domain socket | unix 套接字的 | 2925 |
a_inode | 1384 | ||
netlink | 134 | ||
unknown | 72 | ||
DEL | for a Linux map file that has been deleted | 已删除的 Linux 映射文件的 | |
LINK | for a symbolic link file | 用于符号链接文件 | |
MPB | for a multiplexed block file | 用于多路复用块文件 | |
MPC | for a multiplexed character file | 用于多路复用字符文件 | |
ax25 | for a Linux AX.25 socket | 用于 Linux AX.25 套接字 | |
inet | for an Internet domain socket | 用于 Internet 域套接字 | |
lla | for a HP-UX link level access file | 用于 HP-UX 链路级访问文件 | |
rte | for an AF_ROUTE socket | 用于 AF_ROUTE 套接字 | |
x.25 | for an HP-UX x.25 socket | 用于 HP-UX x.25 套接字 | |
DOOR | for a VDOOR file | VDOOR 文件的 | |
KQUEUE | for a BSD style kernel event queue file | 用于 BSD 样式的内核事件队列文件 | |
NOFD | for a Linux /proc/<PID>/fd directory that can‘t be opened -- the directory path appears in the NAME column, followed by an error message | 对于无法打开的 Linux/proc/<PID>/fd 目录的 “NOFD - 目录路径出现在 NAME 列中, 后跟一条错误消息 | |
PAS | for a /proc/as file | 用于 / proc/as 文件 | |
PAXV | for a /proc/auxv file | 用于 / proc/auxv 文件 | |
PCRE | for a /proc/cred file | 用于 / proc/cred 文件 | |
PCTL | for a /proc control file | 用于 / proc 控制文件 | |
PCUR | for the current /proc process | 用于当前 / proc 过程 | |
PCWD | for a /proc current working directory | 用于 / proc 当前工作目录 | |
PDIR | for a /proc directory | 作为 / proc 目录 | |
PETY | for a /proc executable type (etype) | 用于 / proc 可执行类型 (etype) | |
PFD | for a /proc file descriptor | 用于 / proc 文件描述符 | |
PFDR | for a /proc file descriptor directory | 用于 / proc 文件描述符目录 | |
PFIL | for an executable /proc file | 表示可执行文件 / proc 文件 | |
PFPR | for a /proc FP register set | 用于 / proc FP 寄存器组 | |
PGD | for a /proc/pagedata file | 用于 / proc/pagedata 文件 | |
PGID | for a /proc group notifier file | 用于 / proc 组通知程序文件 | |
PIPE | for pipes | 管道 | |
PLC | for a /proc/lwpctl file | 表示 / proc/lwpctl 文件 | |
PLDR | for a /proc/lpw directory | 用于 / proc/lpw 目录 | |
PLDT | for a /proc/ldt file | 用于 / proc/ldt 文件 | |
PLPI | for a /proc/lpsinfo file | 用于 / proc/lpsinfo 文件的 | |
PLST | for a /proc/lstatus file | 用于 / proc/lstatus 文件 | |
PLU | for a /proc/lusage file | 用于 / proc/lusage 文件 | |
PLWG | for a /proc/gwindows file | 用于 / proc/gwindows 文件 | |
PLWI | for a /proc/lwpsinfo file | 用于 / proc/lwpsinfo 文件 | |
PLWS | for a /proc/lwpstatus file | 用于 / proc/lwpstatus 文件 | |
PLWU | for a /proc/lwpusage file | 用于 / proc/lwpusage 文件 | |
PLWX | for a /proc/xregs file | 用于 / proc/xregs 文件 | |
PMAP | for a /proc map file (map) | 用于 / proc 映射文件 (map) | |
PMEM | for a /proc memory image file | 用于 / proc 内存映像文件 | |
PNTF | for a /proc process notifier file | 用于 / proc 进程通知程序文件 | |
POBJ | for a /proc/object file | 用于 / proc/object 文件 | |
PODR | for a /proc/object directory | 用于 / proc/object 目录 | |
POLP | for an old format /proc light weight process file | 用于旧格式 / proc 轻量级处理文件 | |
POPF | for an old format /proc PID file | 用于旧格式 / proc PID 文件 | |
POPG | for an old format /proc page data file | 用于旧格式 / proc 页面数据文件 | |
PORT | for a SYSV named pipe | SYSV 命名管道的 | |
PREG | for a /proc register file | 用于 / proc 寄存器文件 | |
PRMP | for a /proc/rmap file | 用于 / proc/rmap 文件 | |
PRTD | for a /proc root directory | 用于 / proc 根目录 | |
PSGA | for a /proc/sigact file | 用于 / proc/sigact 文件 | |
PSIN | for a /proc/psinfo file | 用于 / proc/psinfo 文件 | |
PSTA | for a /proc status file | 用于 / proc 状态文件 | |
PSXSEM | for a POSIX semaphore file | 用于 POSIX 信号量文件 | |
PSXSHM | for a POSIX shared memory file | 用于 POSIX 共享内存文件 | |
PTS | for a /dev/pts file | 用于 / dev/pts 文件 | |
PUSG | for a /proc/usage file | 用于 / proc/usage 文件 | |
PW | for a /proc/watch file | 用于 / proc/watch 文件 | |
PXMP | for a /proc/xmap file | 用于 / proc/xmap 文件 | |
SMT | for a shared memory transport file | 用于共享内存传输文件 | |
STSO | for a stream socket | 用于流套接字 | |
UNNM | for an unnamed type file | 用于未命名的类型文件 | |
XNAM | for an OpenServer Xenix special file of unknown type | 用于未知类型的 OpenServer Xenix 特殊文件 | |
XSEM | for an OpenServer Xenix semaphore file | 用于 OpenServer Xenix 信号量文件 | |
XSD | for an OpenServer Xenix shared data file | 用于 OpenServer Xenix 共享数据文件 | |
or the four type number octets if the corresponding name isn‘t known. | 如果相应的名称未知, 则为四个类型数字八位字节。 |
3. 常用选项
-a And 运算
-b
+c w command 宽度
-c command 命令或程序
+d dir 目录
-d FD 列内容的筛选
-D dir 递归目录
-p PID 进程号
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges. | 括号中的默认值; 逗号分隔的集合项目; 短划线范围。 | |
-?|-h | list help | 列表帮助 |
-v | list version info | 列表版本信息 |
-a | AND selections (OR) | AND 选择(OR) |
-c c | cmd c ^c /c/[bix] | cmd c ^ c / c / [bix] |
+c w | COMMAND width (9) | 命令宽度(9)最大 15? |
-d s | select by FD set | 输出列表中排除或包含的文件描述符 (FD) 列表。 文件描述符在逗号分隔集合 s 中指定 – 例如, ‘‘cwd,1,3‘‘,‘‘^6,^2‘‘.(集合中不应有空格) |
+d s | dir s files | dirs 文件 注:不含符号链接,除非使用 - x 或 - x l 选项可包含 |
+D D | dir D tree *SLOW?* | dir D tree * SLOW?* 递归列出目录下被打开的文件 |
-u s | exclude(^)|select login|UID set s | exclude(^)| |
-i | select IPv[46] files | 选择 IPv[4,6]tcp,udp 等文件 |
-n | no host names | # 不将 IP 地址转换为 hostname, 预设是转换的 |
-P | no port names | # 此参数禁止将 port number 转换为 service name, 预设为转换 |
-l | list UID numbers | 列出 UID 号码, 禁止将 UID 转换为登录名。 |
-t | terse listing | 简洁列表, 只输出 PID |
-s | list file size | 列表文件大小 |
-o | list file offset | 它会将 SIZE/OFF 输出列标题更改为 OFFSET 偏移量 |
-U | select Unix socket | 选择 Unix 套接字 |
+|-e s | exempt s *RISKY* | 免除路径名为 s 的文件系统受到可能阻塞的内核函数调用。 +e 选项免除 stat(2),lstat(2)和大多数 readlink(2)内核函数调用. -e 选项仅免除 stat(2)和 lstat(2)内核函数调用. 可以使用单独的 +|-e 规范指定多个文件系统, 并且每个文件系统可以具有免除或不允许的 readlink(2)调用. |
+|-w | Warnings (+) | 警告(+) |
-p s | exclude(^)|select PIDs | exclude(^)| 选择 PID |
-R | list paRent PID | 列出 PPID |
-g [s] | exclude(^)|select and print process group IDs | exclude(^)| 选择并打印进程组 ID |
+|-L [l] | list (+) suppress (-) link counts < l (0 = all; default = 0) | 启用 (+) 或禁用 (-) 文件链接计数列表, 它们可用 - 例如, 它们不适用于套接字或大多数 FIFO 和管道. 如果指定 + L 且没有跟数字, 则将列出所有链接计数。 指定 - L(默认值)时,不会列出任何链接计数。 当 + L 后跟一个数字时,只列出链接数小于该数字的文件。(没有数字可以跟随 - L) |
4. 示例
COMMAND | PID | USER | FD | TYPE | DEVICE | SIZE/OFF | NODE | NAME |
$ lsof /home/toma/Documents/IO.xls | 查找打开该文件的进程 | |||||||
soffice.b | 20084 | toma | 3uW | REG | 8,23 | 242688 | 1480016 | /home/toma/Documents/IO.xls |
$ lsof /home/toma/Downloads/mov/GoT.mp4 | ||||||||
Thunar | 676 | toma | 25u | REG | 8,23 | 11121302 | 1705712 | /home/toma/Downloads/mov/GoT.mp4 |
vlc | 7975 | toma | 22r | REG | 8,23 | 11121302 | 1705712 | /home/toma/Downloads/mov/GoT.mp4 |
$ lsof -c soffice |grep .xls | 查找该进程打开的 *.xls 文件 | |||||||
soffice.b | 20084 | toma | 3uW | REG | 8,23 | 242688 | 1480016 | /home/toma/Documents/IO.xls |
$ lsof -c vlc |grep .mp4 | 查找该进程打开的 *.mp4 文件 | |||||||
vlc | 7975 | toma | 23r | REG | 8,23 | 11121302 | 1705712 | /home/toma/Downloads/mov/GoT.mp4 |
$ lsof /bin/bash | 查看谁正在使用某个文件,也就是说查找某个文件相关的进程 | |||||||
sh | 528 | toma | txt | REG | 8,22 | 903464 | 1315133 | /usr/bin/bash |
$ lsof /usr/bin/fish | ||||||||
fish | 738 | toma | txt | REG | 8,22 | 1587184 | 1365002 | /usr/bin/fish |
$ lsof /usr/lib/libreoffice/program/soffice.bin | ||||||||
soffice.b | 22284 | toma | txt | REG | 8,22 | 14112 | 1456879 | /usr/lib/libreoffice/program/soffice.bin |
$ lsof -c soffice.b |grep office.bin | ||||||||
soffice.b | 22284 | toma | txt | REG | 8,22 | 14112 | 1456879 | /usr/lib/libreoffice/program/soffice.bin |
$ lsof -c soffice |grep office.bin | ||||||||
soffice.b | 22284 | toma | txt | REG | 8,22 | 14112 | 1456879 | /usr/lib/libreoffice/program/soffice.bin |
$ lsof /dev/sda10 | 列出 /dev/sda10 设备上打开的所有文件 | |||||||
vlc | 29350 | toma | 21r | REG | 8,10 | 8500479 | 9728 | /run/media/toma/TjOe/TDDownload/FROZEN2.mp4 |
- 4.1 manpage-Examples
- For a more extensive set of examples, documented more fully, see the 00QUICKSTART file of the lsof distribution.
有关更全面的示例, 请参阅 lsof 发行版的 00QUICKSTART 文件.
To list all open Internet, x.25 (HP-UX), and UNIX domain files, use:
要列出所有打开的 Internet,x.25(HP-UX)和 UNIX 域文件, 请使用:
- $ lsof -i -U
- To list all open IPv4 network files in use by the process whose PID is 1234, use:
要列出 PID 为 1234 的进程正在使用的所有打开的 IPv4 网络文件, 请使用:
- $ lsof -i 4 -a -p 1234
- Presuming the UNIX dialect supports IPv6, to list only open IPv6 network files, use:
假设 UNIX 方言支持 IPv6, 要仅列出打开的 IPv6 网络文件, 请使用:
- $ lsof -i 6
- To list all files using any protocol on ports 513, 514, or 515 of host wonderland.cc.purdue.edu, use:
要在主机 wonderland.cc.purdue.edu 的端口 513,514 或 515 上使用任何协议列出所有文件, 请使用:
- $ lsof -i @wonderland.cc.purdue.edu:513-515
- To list all files using any protocol on any port of mace.cc.purdue.edu (cc.purdue.edu is the default domain), use:
要在 mace.cc.purdue.edu 的任何端口上使用任何协议列出所有文件(cc.purdue.edu 是默认域), 请使用:
- $ lsof -i @mace
- To list all open files for login name ''abe'', or user ID 1234, or process 456, or process 123, or process 789, use:
要列出登录名 "abe", 或用户 ID 1234, 或流程 456, 流程 123 或流程 789 的所有打开文件, 请使用:
- $ lsof -p 456,123,789 -u 1234,abe
- To send a SIGHUP to the processes that have /u/abe/bar open, use:
要将 SIGHUP 发送到打开 / u / abe / bar 的进程, 请使用:
- $ kill -HUP 'lsof -t /u/abe/bar'
- To find any open file, including an open UNIX domain socket file, with the name /dev/log, use:
要查找任何打开的文件, 包括名为 / dev / log 的打开的 UNIX 域套接字文件, 请使用:
- $ lsof /dev/log
- To find processes with open files on the NFS file system named /nfs/mount/point whose server is inaccessible, and presuming your mount table supplies the device number for /nfs/mount/point, use:
要在名为 / nfs / mount / point 且服务器不可访问的 NFS 文件系统上查找具有打开文件的进程, 并假设您的挂载表提供 / nfs / mount / point 的设备编号, 请使用:
- $ lsof -b /nfs/mount/point
- To do the preceding search with warning messages suppressed, use:
要在禁用警告消息的情况下执行上述搜索, 请使用:
- $ lsof -bw /nfs/mount/point
- To ignore the device cache file, use:
要忽略设备缓存文件, 请使用:
- $ lsof -Di
- To obtain PID and command name field output for each process, file descriptor, file device number, and file inode number for each file of each process, use:
要为每个进程获取 PID 和命令名称字段输出, 文件描述符, 文件设备编号和每个进程的每个文件的文件 inode 编号, 请使用:
- $ lsof -FpcfDi
- To list the files at descriptors 1 and 3 of every process running the lsof command for login ID ''abe'' every 10 seconds, use:
要列出每 10 秒执行登录 ID"abe" 的 lsof 命令的每个进程的描述符 1 和 3 的文件, 请使用:
- $ lsof -c lsof -a -d 1 -d 3 -u abe -r10
- To list the current working directory of processes running a command that is exactly four characters long and has an 'o' or 'O' in character three, use this regular expression form of the -c c option:
要列出运行命令的进程的当前工作目录, 该命令长度正好为四个字符且在字符 3 中具有 "o" 或 "O", 请使用 - c c 选项的此正则表达式形式:
- $ lsof -c /^..o.$/i -a -d cwd
- To find an IP version 4 socket file by its associated numeric dot-form address, use:
要通过其关联的数字点形式地址查找 IP 版本 4 套接字文件, 请使用:
- $ lsof [email protected]
- To find an IP version 6 socket file (when the UNIX dialect supports IPv6) by its associated numeric colon-form address, use:
要通过关联的数字冒号形式地址查找 IP 版本 6 套接字文件(当 UNIX 方言支持 IPv6 时), 请使用:
- $ lsof [email protected][0:1:2:3:4:5:6:7]
- To find an IP version 6 socket file (when the UNIX dialect supports IPv6) by an associated numeric colon-form address that has a run of zeroes in it - e.g., the loop-back address - use:
要通过关联的数字冒号形式地址查找 IP 版本 6 套接字文件(当 UNIX 方言支持 IPv6 时), 其中包含一连串的零 - 例如, 回送地址 - 使用:
- $ lsof [email protected][::1]
- To obtain a repeat mode marker line that contains the current time, use:
要获得包含当前时间的重复模式标记线, 请使用:
- $ lsof -rm====%T====
- To add spaces to the previous marker line, use:
要向前一个标记行添加空格, 请使用:
$ lsof -r "m==== %T ===="
5. 特别用法
5.1 恢复删除的文件
对于许多应用程序, 尤其是日志文件和数据库, 这种恢复删除文件的方法非常有用.
$ lsof |grep .mp4 播放视频文件中... FROZEN2.mp4
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/TDDownload/FROZEN2.mp4
$ lsof |grep .mp4 放入回收站后 显示如下: 从回收站恢复即可
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/.Trash-1001/files/FROZEN2.mp4
$ lsof |grep .mp4 直接删除 (shift + delete), 显示如下, 后面多了(deleted) 标记
vlc 30573 toma 20r REG 8,10 8500479 9728 /run/media/toma/TjOe/TDDownload/FROZEN2.mp4 (deleted)
$ ls -l /proc/30573/fd/20 列出 pid 信息,/proc/30573/fd/20 链接到已删除文件.
lr-x------ 1 /proc/30573/fd/20 -> '/run/media/toma/TjOe/TDDownload/FROZEN2.mp4 (deleted)'
$ cat /proc/30573/fd/20> /home/toma/te.mp4 使用查看命令打开并 重定向到新文件即可
$ ls -l /home/toma/te.mp4 列出恢复的新文件
-rwxrwxrwx 1 toma 8500479 te.mp4
5.2 杀死某个特定用户的所有活动
# kill -9 `lsof -t -u named`
对于服务器或多用户共用的环境比较有用.
5.3 找回被删除文件占用的空间
找到仍被打开, 但已被删除的文件
$ lsof | grep deleted
查看后关闭相应程序或杀死进程即可
- $ ps -ef | grep pid
- $ sudo kill -9 pid
来源: http://www.bubuko.com/infodetail-3100727.html