最近发现LOSF 命令在系统管理方面特别有用,把我搜集的资料总结如下
 

安装

发个安装lsof的步骤

Steps to Install lsof on AIX
1) Obtain the fileset. It’s in the AIX 5L V5.3 Expansion Pack which shipped with your AIX system. Supposedly, you can call IBM and they will ship you the CDs if you have support with them. Otherwise, try downloading it from their website. I had to create an IBM login to get the file I needed.

I personally installed this using the CDs, so those are the instructions I’m giving.

2) Insert CD, “AIX Expansion Pack”

3) Run SMIT

AIX# smit

4) Navigate to Install Software

Software Installation and Maintenance > Install and Update Software > Install Software

5) Specify your device as your CD (i.e. /dev/cd0)

6) Scroll to SOFTWARE to install. Press F4 or esc+4.

7) Find lsof.base and lsof.man.en_US. Select these using F7 or esc+7

8) Hit enter a few times and it should install in /usr/sbin/lsof.

9) Test the command as root:

AIX# lsof


1、当在lsof后边没有跟任何参数时,该命令将会列出当前系统中被所有进程打开的所有文件
#lsof|nl     #nl命令打印出行号

2、下边这几个命令指出打开某文件的进程

#lsof `which httpd`    #那个进程在使用apache的可执行文件
#lsof /etc/passwd      #那个进程在占用/etc/passwd
#lsof /dev/hda6        #那个进程在占用hda6
#lsof /dev/cdrom       #那个进程在占用光驱

3、下边将会打印出占用httpd可执行文件的进程的进程号(仅仅是进程号,在编写shell脚本是有用)
#lsof -t `which httpd`

4、显示出那些文件被以k打头的进程名的进程打开,以bash打头,和以init打头:
#lsof -c k
#lsof -c bash
#lsof -c init

5、显示出那些文件被以courier打头的进程打开,但是并不属于用户‘zahn’
#lsof -c courier -u ^zahn

6、显示被zahn和apache打开的文件
#lsof -u apache,zahn

7、显示那些文件被pid为30297的进程打开:
#lsof +p 30297

8、显示所有在/tmp文件夹中打开的instance和文件的进程。但是symbol文件并不在列
#lsof -D /tmp

9、显示所有打开的端口
#lsof -i
10、显示所有打开80端口的进程
#lsof -i :80

11、显示所有打开的端口和UNIX domain文件:
#lsof -i -U

12、显示那些进程打开了到www.akadia.com的UDP的123(ntp)端口的链接:
#lsof-iUDP@www.akadia.com:123

13、列出Internet, x.25 (HP-UX), and UNIX domain 文件:
#lsof -i -U

14、列出指定进程打开的IPv4文件:
#lsof -i 4 -a -p 1234  (-a 参数被视为 AND )

15、列出指定进程打开的IPv4文件:
#lsof -i 6

16、列出使用 /dev/log的文件:
#lsof /dev/log

17、获取每个进程的PID,命令名。进程每个文件的文件描述符,文件设备号,文件inode :
#lsof –FpcfDi

18、获取工作目录的第3个字符是'o' or 'O'的进程:
#lsof -c /^..o.$/i -a -d cwd  (-a 参数被视为 AND )

19、获取和指定IP有关的socket 文件:
#lsof-i@172.19.148.26

20、Unlinked的打开文件:
#lsof +L1

21、查找监听的socket
#lsof –i
#lsof-i@aaa.bbb.ccc
#lsof-iTCP@aaa.bbb.ccc:ftp-data

22、查找指定进程号或进程名使用的文件
lsof -p <PID>

23、远程登陆侦测
#lsof /dev/pts/0

24、NFS文件
$ lsof –N

25、指定用户打开的文件

lsof -u ^root