linux命令之lsof


一、基本介紹

(1)lsof命令用於查看你進程開打的文件,打開文件的進程,進程打開的端口(TCP、UDP).

(2)找回/恢復刪除的文件.


二、lsof語法

lsof(選項) filename
 
-a:列出打開文件存在的進程;
-c<進程名>:列出指定進程所打開的文件; 
-g:列出GID號進程詳情; 
-d<文件號>:列出佔用該文件號的進程; 
+d<目錄>:列出目錄下被打開的文件; 
+D<目錄>:遞歸列出目錄下被打開的文件; 
-n<目錄>:列出使用NFS的文件; 
-i<條件>:列出符合條件的進程。(4、6、協議、:端口、 @ip ) 
-p<進程號>:列出指定進程號所打開的文件; 
-u:列出UID號進程詳情; 
-h:顯示幫助信息; 
-v:顯示版本信息。

實例:

lsof abc.txt 顯示開啓文件abc.txt的進程
lsof -c abc 顯示abc進程現在打開的文件
lsof -c -p 1234 列出進程號爲1234的進程所打開的文件
lsof -g gid 顯示歸屬gid的進程情況
lsof +d /usr/local/ 顯示目錄下被進程開啓的文件
lsof +D /usr/local/ 同上,但是會搜索目錄下的目錄,時間較長
lsof -d 4 顯示使用fd爲4的進程
lsof -i 用以顯示符合條件的進程情況
lsof -i[46] [protocol][@hostname|hostaddr][:service|port]
--> IPv4 or IPv6
  protocol --> TCP or UDP
  hostname --> Internet host name
  hostaddr --> IPv4地址
  service --> /etc/service中的 service name (可以不止一個)
  port --> 端口號 (可以不止一個)


三、詳解lsof輸出結果

[root@node1 ~]# cd /mnt/
[root@node1 mnt]# lsof /mnt/
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    1523 root  cwd    DIR   11,1     4096 1856 /mnt
lsof    1669 root  cwd    DIR   11,1     4096 1856 /mnt
lsof    1670 root  cwd    DIR   11,1     4096 1856 /mnt

COMMAND:進程的名稱 

PID:進程標識符

USER:進程所有者

FD:文件描述符,應用程序通過文件描述符識別該文件。如cwd、txt等 

TYPE:文件類型,如DIR、REG等

DEVICE:指定磁盤的名稱

SIZE:文件的大小

NODE:索引節點(文件在磁盤上的標識)

NAME:打開文件的確切名稱


文件描述符列表: 

cwd:表示current work dirctory,即:應用程序的當前工作目錄,這是該應用程序啓動的目錄,除非它本身對這個目錄進行更改。
txt:該類型的文件是程序代碼,如應用程序二進制文件本身或共享庫,如上列表中顯示的 /sbin/init 程序。
lnn:library references (AIX); 
er:FD information error (see NAME column); 
jld:jail directory (FreeBSD); 
ltx:shared library text (code and data); 
mxx :hex memory-mapped type number xx. 
m86:DOS Merge mapped file; 
mem:memory-mapped file; 
mmap:memory-mapped device; 
pd:parent directory; 
rtd:root directory; 
tr:kernel trace file (OpenBSD); 
v86  VP/ix mapped file; 
0:表示標準輸出 
1:表示標準輸入 
2:表示標準錯誤


一般在標準輸出、標準錯誤、標準輸入後還跟着文件狀態模式: 
u:表示該文件被打開並處於讀取/寫入模式。 
r:表示該文件被打開並處於只讀模式。 
w:表示該文件被打開並處於。 
空格:表示該文件的狀態模式爲unknow,且沒有鎖定。 
-:表示該文件的狀態模式爲unknow,且被鎖定。 

 
同時在文件狀態模式後面,還跟着相關的鎖: 
N:for a Solaris NFS lock of unknown type; 
r:for read lock on part of the file; 
R:for a read lock on the entire file; 
w:for a write lock on part of the file;(文件的部分寫鎖) 
W:for a write lock on the entire file;(整個文件的寫鎖) 
u:for a read and write lock of any length; 
U:for a lock of unknown type; 
x:for an SCO OpenServer Xenix lock on part      of the file; 
X:for an SCO OpenServer Xenix lock on the      entire file; 
space:if there is no lock.

 

文件類型:

DIR:表示目錄。 
CHR:表示字符類型。 
BLK:塊設備類型。 
UNIX: UNIX 域套接字。 
FIFO:先進先出 (FIFO) 隊列。 
IPv4:網際協議 (IP) 套接字。 
DEVICE:指定磁盤的名稱



四、對刪除的文件進行恢復(該文件刪除前已被進程打開)

[root@node1 ~]# lsof /var/log/messages
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
rsyslogd 1252 root    4w   REG  253,0   201170 261273 /var/log/messages
tail     1550 root    3r   REG  253,0   201170 261273 /var/log/messages
[root@node1 ~]# rm -rf  /var/log/messages 
[root@node1 ~]# 
[root@node1 ~]# lsof /var/log/messages
lsof: status error on /var/log/messages: No such file or directory
lsof 4.82
 latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
 latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
 latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
 usage: [-?abhlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
 [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Use the ``-h'' option to get more help information.


[root@node1 ~]# lsof | grep /var/log/messages
rsyslogd  1252    root    4w      REG              253,0   201170     261273 /var/log/messages (deleted)
tail      1550    root    3r      REG              253,0   201170     261273 /var/log/messages (deleted)

[root@node1 ~]# cat /proc/1252/fd/4 >aaa.txt 
[root@node1 ~]# cat /proc/1550/fd/3 >bbb.txt

[root@node1 ~]# ls -lrt 
total 420
-rw-r--r--. 1 root root   3161 Jun 18 19:58 install.log.syslog
-rw-r--r--. 1 root root   9913 Jun 18 19:59 install.log
-rw-------. 1 root root   1116 Jun 18 19:59 anaconda-ks.cfg
-rw-r--r--  1 root root 201170 Jul  6 20:17 aaa.txt
-rw-r--r--  1 root root 201365 Jul  6 20:18 bbb.txt
[root@node1 ~]# diff aaa.txt bbb.txt 
[root@node1 ~]#

[root@node1 ~]# cp aaa.txt  /var/log/messages 
到此文件恢復完成!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章