linux常用命令 / 未分类 · 2013年10月28日

每日一命令之 lsof

lsof(list open files)是一个列出当前系统打开文件的工具
常用的参数列表:
lsof filename 显示打开指定文件的所有进程
lsof -a 表示两个参数都必须满足时才显示结果
lsof -c string 显示COMMAND列中包含指定字符的进程所有打开的文件
lsof -u username 显示所属user进程打开的文件
lsof -g gid 显示归属gid的进程情况
lsof +d /DIR/ 显示目录下被进程打开的文件
lsof +D /DIR/ 同上,但是会搜索目录下的所有目录,时间相对较长
lsof -d FD 显示指定文件描述符的进程
lsof -n 不将IP转换为hostname,缺省是不加上-n参数
lsof -i 用以显示符合条件的进程情况
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 –> 端口号 (可以不只一个)
*******************************************
用法例子
某个进程占用未释放空间
[root@91 var]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 2.9G 2.5G 286M 90% /var
[root@hddh_web_91 var]# du -sh /var/
119M /var/
[root@91 var]# lsof |grep del
sendmail 369 smmsp 4uW REG 8,5 1189068800 203225 /var/spool/clientmqueue/dfr0S4k2CD000369 (deleted)
[root@91 var]# kill -9 369
[root@91 var]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 2.9G 1.4G 1.4G 49% /var
***********************
重点的-i
-i [i] This option selects the listing of files any of whose Internet address matches the address specified in i. If
no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.
If -i4 or -i6 is specified with no following address, only files of the indicated IP version, IPv4 or IPv6, are
displayed. (An IPv6 specification may be used only if the dialects supports IPv6, as indicated by ‘‘[46]’’ and
‘‘IPv[46]’’ in lsof’s -h or -? output.) Sequentially specifying -i4, followed by -i6 is the same as specifying
-i, and vice-versa. Specifying -i4, or -i6 after -i is the same as specifying -i4 or -i6 by itself.
Multiple addresses (up to a limit of 100) may be specified with multiple -i options. (A port number or service
name range is counted as one address.) They are joined in a single ORed set before participating in AND option
selection.
An Internet address is specified in the form (Items in square brackets are optional.):
[46][protocol][@hostname|hostaddr][:service|port]
查看22端口现在运行的情况
lsof -i :22
http://blog.csdn.net/guoguo1980/article/details/2324454
搞定
参考资料