手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux 命令教程:lsof 入门

[日期:2014-10-27] 来源:Linux中国  作者:Linux [字体:大 中 小]

用户信息

你也可以获取各种用户的信息,以及它们在系统上正干着的事情,包括它们的网络活动、对文件的操作等。

使用-u显示指定用户打开了什么

  1. # lsof -u daniel
  2.  
  3. -- snipped --
  4. Dock155 daniel txt REG 14,22798436823208/usr/lib/libicucore.A.dylib
  5. Dock155 daniel txt REG 14,21580212823126/usr/lib/libobjc.A.dylib
  6. Dock155 daniel txt REG 14,22934184823498/usr/lib/libstdc++.6.0.4.dylib
  7. Dock155 daniel txt REG 14,2132008823505/usr/lib/libgcc_s.1.dylib
  8. Dock155 daniel txt REG 14,2212160823214/usr/lib/libauto.dylib
  9. -- snipped --

使用-u user来显示除指定用户以外的其它所有用户所做的事情

  1. # lsof -u ^daniel
  2.  
  3. -- snipped --
  4. Dock155 jim txt REG 14,22798436823208/usr/lib/libicucore.A.dylib
  5. Dock155 jim txt REG 14,21580212823126/usr/lib/libobjc.A.dylib
  6. Dock155 jim txt REG 14,22934184823498/usr/lib/libstdc++.6.0.4.dylib
  7. Dock155 jim txt REG 14,2132008823505/usr/lib/libgcc_s.1.dylib
  8. Dock155 jim txt REG 14,2212160823214/usr/lib/libauto.dylib
  9. -- snipped --

杀死指定用户所做的一切事情

可以消灭指定用户运行的所有东西,这真不错。

  1. # kill -9 `lsof -t -u daniel`

命令和进程

可以查看指定程序或进程由什么启动,这通常会很有用,而你可以使用lsof通过名称或进程ID过滤来完成这个任务。下面列出了一些选项:

使用-c查看指定的命令正在使用的文件和网络连接

  1. # lsof -c syslog-ng
  2.  
  3. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  4. syslog-ng 7547 root cwd DIR 3,340962/
  5. syslog-ng 7547 root rtd DIR 3,340962/
  6. syslog-ng 7547 root txt REG 3,31135241064970/usr/sbin/syslog-ng
  7. -- snipped --

使用-p查看指定进程ID已打开的内容

  1. # lsof -p 10075
  2.  
  3. -- snipped --
  4. sshd 10068 root mem REG 3,334808850407/lib/libnss_files-2.4.so
  5. sshd 10068 root mem REG 3,334924850409/lib/libnss_nis-2.4.so
  6. sshd 10068 root mem REG 3,326596850405/lib/libnss_compat-2.4.so
  7. sshd 10068 root mem REG 3,3200152509940/usr/lib/libssl.so.0.9.7
  8. sshd 10068 root mem REG 3,346216510014/usr/lib/liblber-2.3
  9. sshd 10068 root mem REG 3,359868850413/lib/libresolv-2.4.so
  10. sshd 10068 root mem REG 3,31197180850396/lib/libc-2.4.so
  11. sshd 10068 root mem REG 3,322168850398/lib/libcrypt-2.4.so
  12. sshd 10068 root mem REG 3,372784850404/lib/libnsl-2.4.so
  13. sshd 10068 root mem REG 3,370632850417/lib/libz.so.1.2.3
  14. sshd 10068 root mem REG 3,39992850416/lib/libutil-2.4.so
  15. -- snipped --

-t选项只返回PID

  1. # lsof -t -c Mail
  2.  
  3. 350

文件和目录

通过查看指定文件或目录,你可以看到系统上所有正与其交互的资源——包括用户、进程等。

显示与指定目录交互的所有一切

  1. # lsof /var/log/messages/
  2.  
  3. COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
  4. syslog-ng 7547 root 4w REG 3,3217309834024/var/log/messages

显示与指定文件交互的所有一切

  1. # lsof /home/daniel/firewall_whitelist.txt

高级用法

与tcpdump类似,当你开始组合查询时,它就显示了它强大的功能。

显示daniel连接到1.1.1.1所做的一切

  1. # lsof -u daniel -i @1.1.1.1
  2.  
  3. bkdr 1893 daniel 3uIPv63456 TCP 10.10.1.10:1234->1.1.1.1:31337(ESTABLISHED)

同时使用-t和-c选项以给进程发送 HUP 信号

  1. # kill -HUP `lsof -t -c sshd`

lsof +L1显示所有打开的链接数小于1的文件

这通常(当不总是)表示某个攻击者正尝试通过删除文件入口来隐藏文件内容。

  1. # lsof +L1
  2.  
  3. (hopefully nothing)

显示某个端口范围的打开的连接

  1. # lsof -i @fw.google.com:2150=2180

结尾

本入门教程只是管窥了lsof功能的一斑,要查看完整参考,运行man lsof命令或查看在线版本。希望本文对你有所助益,也随时欢迎你的评论和指正。

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/108512.htm

linux
相关资讯       Linux命令教程  lsof 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款