Let's Go!

lsof 快速起步,查看打开文件

上一篇 / 下一篇  2011-10-19 09:59:51 / 个人分类:系统相关&实用技巧




Abstract:
通过大量的示例介绍使用lsof的方法.

文档出处:http://io.expert.com.br/~salvatti/tutoriais/seguranca/site/www.aei.ca/pmatilus/pub/lsof-quickstart.txt



Contents

    查看对某个文件的使用情况
    查看对文件系统的使用
        查找打开,但是不能连接的文件
    无法卸载
    查看监听socket
    查看某个网络连接
    识别 Netstat 连接
    查找针对某个命令打开的文件
    查看某个用户的操作
    更多信息
    Bibliography






查看对某个文件的使用情况

查看哪些进程对某个文件进行了调用:

$ lsof /etc/passwd 1

查看对文件系统的使用

/tmp目录被垃圾文件塞满了, 但是, 用ls 又看不到太大文件, 谁干的?

$ lsof /tmp

查找打开,但是不能连接的文件

一个进程打开一个文件, 然后将其设为 unlinked 状态, 则此文件资源仍能被进程使用, 但是其访问路径已经被删除了. 因此, 使用ls不能将其列出. 只有当进程结束时, 才能释放文件占用的资源

查找unlinked 文件, 选项 +L, 作用: 列出打开文件的连接数

$lsof +L

指定连接数的上限 $lsof +L1

同时指定文件系统, 则需要使用 -a(AND) 选项

$ lsof -a +L1 /home

无法卸载

查看谁令mount的分区无法卸载

$ lsof <file_system_name>

查看监听socket
查看网络服务

$ lsof -i

查看某个网络连接

$ lsof -i@aaa.bbb.ccc

$ lsof -iTCP@aaa.bbb.ccc:ftp-data *指定协议*

$ lsof -i4 *指定IP版本*

$ lsof -i6

识别 Netstat 连接
例如: netstat -p -t -n 的输出为:

Proto Recv-Q Send-Q Local Address Foreign Address State

tcp 0 0 218.56.203.246:52634 202.109.72.72:7000 ESTABLISHED

则可以: tony@tony:~$ lsof -iTCP@202.109.72.72:7000

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME

xchat 4505 tony 12u IPv4 5775 TCP 218.56.203.246:52634->202.109.72.72:afs3-fileserver (ESTABLISHED)

查找针对某个命令打开的文件

使用命令的PID

$ lsof -p <PID>

使用命令名称

$ lsof -c <first_characters_of_command_name_that_interest_you>

$ lsof -c sendmail

查看谁在使用设备文件

$ lsof /dev/hda6
查看某个用户的操作

$ id -u tony

1000

$ lsof -u1000 or $ lsof -utony

$ lsof -u^tony * 则是取反的意思*

更多信息
更多信息参阅 lsof 的联机手册losf(1).

Bibliography

1
    http://io.expert.com.br/~salvatti/tutoriais/seguranca/site/www.aei.ca/pmatilus/pub/lsof-quickstart.txt;

About this document ...
lsof 快速起步

This document was generated using the LaTeX2HTML translator Version 2002-2-1 (1.71)

Copyright ? 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
Copyright ? 1997, 1998, 1999, Ross Moore, Mathematics Department, Macquarie University, Sydney.

The command line arguments were:
latex2html lsof.tex -split 1 -local_icons

The translation was initiated by tony on 2005-12-18

转自:http://www.debsir.org/doc/inthedebianway/lsof/lsof.html



TAG:

 

评分:0

我来说两句

Open Toolbar