CentOS中的lsof


1.安装

lsof的安装命令如下

yum install lsof

lsof执行安装

2.简介

lsoflists openfiles

用法:

lsof usage

lsof: status error on usage: No such file or directory

lsof 4.87
latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/

latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ

latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man

usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]

[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]

[[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [-Z [Z]] [–] [names]

Use the ``-h’’ option to get more help information.

lsof usage

lsof -h			#利用help来查看

lsof 4.87

latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/

latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ

latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man

usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [-Z [Z]] [–] [names]
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges.
-?|-h list help -a AND selections (OR) -b avoid kernel blocks
-c c cmd c ^c /c/[bix] +c w COMMAND width (9) +d s dir s files
-d s select by FD set +D D dir D tree SLOW? +|-e s exempt s RISKY
-i select IPv[46] files -K list tasKs (threads) -l list UID numbers
-n no host names -N select NFS files -o list file offset
-O no overhead RISKY -P no port names -R list paRent PID
-s list file size -t terse listing -T disable TCP/TPI info
-U select Unix socket -v list version info -V verbose search
+|-w Warnings (+) -X skip TCP&UDP* files -Z Z context [Z]
– end option scan
+f|-f +filesystem or -file names +|-f[gG] flaGs
-F [f] select fields; -F? for help
+|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0)
+m [m] use|create mount supplement
+|-M portMap registration (-) -o o o 0t offset digits (8)
-p s exclude(^)|select PIDs -S [t] t second stat timeout (15)
-T qs TCP/TPI Q,St (s) info
-g [s] exclude(^)|select and print process group IDs
-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
+|-r [t[m]] repeat every t seconds (15); + until no files, - forever.
An optional suffix to t is m; m must separate t from and
is an strftime(3) format for the marker line.
-s p:s exclude(^)|select protocol (p = TCP|UDP) states by name(s).
-u s exclude(^)|select login|UID set s
-x [fl] cross over +d|+D File systems or symbolic Links
names select named files or files on named file systems
Anyone can list all files; /dev warnings disabled; kernel ID check disabled.

lsof -h

3.使用方法

默认 : 没有选项,lsof列出活跃进程的所有打开文件

组合 : 可以将选项组合到一起,如-abc,但要当心哪些选项需要参数

-a : 结果进行“与”运算(而不是“或”)

-l : 在输出显示用户ID而不是用户名

-h : 获得帮助

-t : 仅获取进程ID

-U : 获取UNIX套接口地址

-F : 格式化输出结果,用于其它命令。可以通过多种方式格式化,如-F pcfn(用于进程id、命令名、文件描述符、文件名,并以空终止)

4.常用命令

(1)显示所有连接

lsof -i

(2)显示端口占用情况

lsof -i:端口号

(3)找出监听端口

lsof  -i -sTCP:LISTEN

(4)查看指定进程ID已打开的内容

lsof  -p 这里是pid

参考网址:Linux 命令神器:lsof

5.输出信息简单解读

一般lsof之后会输出哪些信息呢

​ COMMAND: 进程的名称

  PID:进程标识符

  PPID:父进程标识符(需要指定-R参数)

  USER:进程所有者

  PGID:进程所属组

  FD:文件描述符,应用程序通过文件描述符识别该文件。

  TYPE:文件类型。如 DIR、REG等

  DEVICE:指定磁盘的名称

  SIZE:文件的大小

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

  NAME:打开文件的确切名称

FD:

  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. 空格:表示该文件的状态模式为 unknown ,且没有锁定
  5. -: 表示该文件的状态模式为 unknown ,且被锁定

  在文件状态模式后面,还跟着相关的锁

  1. N:for a Solaris NFS lock of unknown type
  2. r: for a 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 OpentServer Xenix lock on the entire file
  10. space : if there is no lock

参考网页——每天一个linux命令(38)–lsof 之FD文件描述符


文章作者: milu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 milu !
  目录