lsof命令

lsof [options] filename

常用的参数列表:
lsof  filename 显示打开指定文件的所有进程
lsof -a and与 输出所有参数都符合的结果
lsof -c string   显示COMMAND列中包含指定字符的进程所有打开的文件
lsof -u username  显示所属user进程打开的文件
lsof -g gid 显示归属gid的进程情况
lsof +d /DIR/ 显示目录下被进程打开的文件
lsof +D /DIR/ 同上,但是会搜索目录下的所有目录,时间相对较长
lsof -d 文件号 显示指定文件描述符的进程
lsof -p 进程号 列出指定进程号所打开的文件
lsof -n 不将IP转换为hostname,缺省是不加上-n参数
lsof -N 列出使用NFS的文件
lsof -i 列出符合条件的进程。(4、6、协议、:端口、 @ip )
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
            46 --> IPv4 or IPv6
            protocol --> TCP or UDP
            hostname --> Internet host name
            hostaddr --> IPv4地址
            service --> /etc/service中的 service name (可以不只一个)
            port --> 端口号 (可以不只一个)

例如: 查看22端口现在运行的情况

lsof -i :22
COMMAND  PID USER   FD   TYPE DEVICE SIZE NODE NAME
sshd    1409 root    3u  IPv6   5678       TCP *:ssh (LISTEN)

查看所属root用户进程所打开的文件类型为txt的文件:

lsof -a -u root -d txt
COMMAND    PID USER  FD      TYPE DEVICE    SIZE    NODE NAME
init       1    root txt       REG    3,3   38432 1763452 /sbin/init
mingetty  1632 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1633 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1634 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1635 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1636 root txt       REG    3,3   14366 1763337 /sbin/mingetty
mingetty  1637 root txt       REG    3,3   14366 1763337 /sbin/mingetty
kdm        1638 root txt       REG    3,3  132548 1428194 /usr/bin/kdm
X          1670 root txt       REG    3,3 1716396 1428336 /usr/bin/Xorg
kdm        1671 root txt       REG    3,3  132548 1428194 /usr/bin/kdm
startkde  2427 root txt       REG    3,3  645408 1544195 /bin/bash
... ...

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注