免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 663 | 回复: 0
打印 上一主题 下一主题

lsof的用法[转] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-10 16:17 |只看该作者 |倒序浏览

lsof是list open files的缩写。
1。当你umount设备或者目录/mnt/cdrom的时候,提示device is busy。这时候可以用lsof查看这个目录(目录事实上也是文件)谁在使用
# lsof /mnt/cdrom
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 13562 root cwd DIR 8,1 4096 105171 /mnt/cdrom
lsof 13857 root cwd DIR 8,1 4096 105171 /mnt/cdrom
lsof 13858 root cwd DIR 8,1 4096 105171 /mnt/cdrom
解释一下输出的几个字段的意思:
FD:File Descriptor number of the file就是文件描述符。cwd表示current working directory也就是当前工作目录。
TYPE表示the type of the node associated with the file,也就是这个文件相关的node的类型。DIR表示目录directory。
其他的可以自己man lsof去查看OUTPUT字段的解释。
2。如果一个文件被删除,但是有进程之前打开了它,并且这个进程一直没有终止,那么可以用lsof把它找回来。比如删除了/var/log/secure文件之后:
# lsof |grep /var/log/secure
syslogd 2574 root 2w REG 253,0 1099 525125 /var/log/secure (deleted)
可以看到进程号为2574的syslogd进程仍然打开了/var/log/secure这个文件,这个文件所显示状态是deleted,已经被删除。现在FD文件描述符是2w。它的意思是文件描述符是2,状态是w写。
我们可以根据这个在/proc/2574/fd/2找到被删除的/var/log/secure的内容。
# cat /proc/2574/fd/2
Aug 19 10:15:34 as4u3 sshd[1953]: Received signal 15; terminating.
Aug 19 15:39:43 as4u3 sshd[2069]: Server listening on :: port 22.
Aug 19 15:39:43 as4u3 sshd[2069]: error: Bind to port 22 on 0.0.0.0 failed: Addr ess already in use.
Aug 19 15:42:37 as4u3 sshd[2563]: Accepted password for root from ::ffff:192.168 .2.8 port 4890 ssh2
Aug 19 16:02:04 as4u3 sshd[3531]: Accepted password for root from ::ffff:192.168 .2.8 port 4935 ssh2
Aug 19 16:25:52 as4u3 sshd[2069]: Received signal 15; terminating.
Aug 29 23:16:21 as4u3 sshd[1953]: Server listening on :: port 22.
Aug 29 23:16:21 as4u3 sshd[1953]: error: Bind to port 22 on 0.0.0.0 failed: Addr ess already in use.
Aug 29 23:16:30 as4u3 sshd[1961]: Accepted password for root from ::ffff:192.168 .2.8 port 2604 ssh2
Aug 29 23:39:44 as4u3 sshd[1953]: Received signal 15; terminating.
Aug 29 23:41:36 as4u3 sshd[2669]: Server listening on :: port 22.
Aug 29 23:41:36 as4u3 sshd[2669]: error: Bind to port 22 on 0.0.0.0 failed: Addr ess already in use.
Aug 29 23:46:13 as4u3 sshd[3356]: Accepted password for root from ::ffff:192.168 .2.8 port 3127 ssh2
因此在误删log,或者被人黑掉删除日志,又没有重启syslogd进程的情况下,可以找回日志文件。所以注意这样一个问题,发现服务器被人黑掉,先不要急着操作,特别是不要急着reboot。第一步先拔掉网线,然后查看进程,查看日志。
3。查看网络连接或者什么程序在监听端口,这个跟netstat比较类似。例:查看25端口是什么程序在监听。
# lsof -i :22
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sshd 2669 root 3u IPv6 5722 TCP *:ssh (LISTEN)
sshd 3356 root 3u IPv6 7242 TCP 192.168.2.10:ssh->192.168.2.8:3127 (ESTABLISHED)
# netstat -anp|grep 22
tcp 0 0 :::22 :::* LISTEN 2669/sshd
tcp 0 52 ::ffff:192.168.2.10:22 ::ffff:192.168.2.8:3127 ESTABLISHED 3356/0
这样就一目了然,如果还不够清楚,就根据pid号1959,用ps去查看
# ps -ef|grep 2669
root 2669 1 0 23:41 ? 00:00:00 /usr/sbin/sshd
root 3356 2669 0 23:46 ? 00:00:01 sshd: root@pts/0
root 3433 3358 0 23:54 pts/0 00:00:00 grep 2669
相似的用法还所有lsof -i @192.168.1.10


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/38214/showart_297500.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP