lsof (list open files)是一個列出當(dāng)前系統(tǒng)打開文件的工具。在linux系統(tǒng)環(huán)境下,任何事物都可以以文件形式存在,通過文件不僅可以訪問常規(guī)的數(shù)據(jù),還可以訪問網(wǎng)絡(luò)連接和硬件。
適應(yīng)條件:lsof訪問的是核心文件和各種文件,所以必須以root用戶的身份運行才能充分發(fā)揮其功能。
lsof [選項] [絕對路徑的文件名]
顯示示例
[root@localhost ~]# lsof /usr/sbin/httpd
COMMAND? PID? USER? FD? TYPE DEVICE SIZE/OFF? NODE NAME
httpd6279root txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6281apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6282apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6283apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6284apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6285apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6286apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6287apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6288apache txt? ? REG8,2344112415135/usr/sbin/httpd
httpd6546apache txt? ? REG8,2344112415135/usr/sbin/httpd
每行顯示一個打開的文件,默認如果后面不跟任何東西,將打開系統(tǒng)打開的所有文件
COMMAND :進程名稱
PID:進程標(biāo)識符
USER:進程所有者
FD:文件描述符,應(yīng)用程序通過文件描述符識別到該文件。如cwd、txt等
TYPE:文件類型,如DIR,REG
DEVICE:指定磁盤名稱
SIZE:文件大小
NODE:索引節(jié)點(文件在磁盤上的標(biāo)識)
NAME:打開文件的確切名稱
補充:FD列中的文件描述cwd值表示應(yīng)用程序的當(dāng)前工作目錄,這是該程序啟動的目錄,除非它本身對這個目錄進行更改。txt類型的是程序代碼,如應(yīng)用程序二進制文件本身或者共享庫。其
次數(shù)值表示應(yīng)用程序的文件描述符,這是打開文件時一個返回的一個整數(shù)。
lsof6660root0uCHR136,00t03/dev/pts/0lsof6660root1uCHR136,00t03/dev/pts/0lsof6660root2uCHR136,00t03/dev/pts/0lsof6660root? ? 3r? ? ? DIR0,301/proc
lsof6660root? ? 4r? ? ? DIR0,3036358/proc/6660/fd
lsof6660root? ? 5wFIFO0,80t036363pipe
lsof6660root? ? 6r? ? FIFO0,80t036364pipe
lsof6661root? cwd? ? ? DIR8,24096130562/root
lsof6661root? rtd? ? ? DIR8,240962/lsof6661root? txt? ? ? REG8,2154356415242/usr/sbin/lsof
lsof6661root? mem? ? ? REG8,21907156914957/lib/libc-2.12.so
lsof6661root? mem? ? ? REG8,217892914963/lib/libdl-2.12.so
lsof6661root? mem? ? ? REG8,2141080914950/lib/ld-2.12.so
lsof6661root? mem? ? ? REG8,2120780915040/lib/libselinux.so.1lsof6661root? mem? ? ? REG8,299154448395123/usr/lib/locale/locale-archive
lsof6661root? ? 4r? ? FIFO0,80t036363pipe
lsof6661root? ? 7wFIFO0,80t036364pipe
其中u表示該文件被打開處于讀取\寫入模式,而不是只讀或只寫模式;
r 只讀 ; w 只寫 ;W表示該應(yīng)用程序具有對整個文件的寫鎖(確保每次只能打開一次應(yīng)用程序?qū)嵗?/p>
初始打開每個應(yīng)用程序時,都具有三個文件描述符,從0到2,分別表示標(biāo)準(zhǔn)輸入、輸出和錯誤流。因此,大多數(shù)應(yīng)用程序
所打開的FD都是從3開始
TYPE:REG、DIR、CHR、BLK、UNIX、FIFO、IPV4
(2)
查看端口現(xiàn)在運行的情況
ls -i:port? #某個端口
ls -i:port1-port2 #
ls -i:1-1024????? #查看端口1-1024運行情況
(3)恢復(fù)刪除文件
當(dāng)系統(tǒng)中的某個文件被意外刪除了,只要這個時候系統(tǒng)中有進程正在訪問這個文件,那么可以通過lsof 從/proc目錄下恢復(fù)文件的內(nèi)容
假如/var/log/messages文件被刪了,恢復(fù)這個文件的方法:
首先使用lsof 查看當(dāng)前是否有進程打開/var/log/messages文件,
#lsof |grep /var/log/messages
[root@localhost ~]# rm /var/log/messages
rm:是否刪除普通文件 "/var/log/messages"?y
[root@localhost ~]# lsof |grep /var/log/messages
rsyslogd? 5925????? root??? 1w????? REG??????? 8,2???? 4369???? 266184 /var/log/messages (deleted)
從上面的信息可以看到PID 5925(syslogd)打開文件的文件描述符為1,同時發(fā)現(xiàn)/var/log/messages已經(jīng)被刪除了。
因此可以通過/var/log/messages文件描述符來查看文件信息。
cat/pro/5925/fd/1[root@localhost~]#cat/proc/5925/fd/1May1208:04:11localhost kernel: hpet1: lost3rtc interrupts
May1208:04:11localhost kernel: hpet1: lost6rtc interrupts
May1208:04:11localhost kernel: hpet1: lost1rtc interrupts
May1209:25:33localhost kernel: usb2-2.1: USB disconnect, device number10May1209:25:33localhost kernel: eth0: link down
May1209:25:33localhost kernel: usb2-2.1: new full speed USB device number11using uhci_hcd
May1209:25:33localhost kernel: usb2-2.1: New USB device found, idVendor=0e0f, idProduct=0008May1209:25:33localhost kernel: usb2-2.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3May1209:25:33localhost kernel: usb2-2.1: Product: Virtual Bluetooth Adapter
May1209:25:33localhost kernel: usb2-2.1: Manufacturer: VMware
May1209:25:33localhost kernel: usb2-2.1: SerialNumber:000650268328May1209:25:33localhost kernel: usb2-2.1: configuration #1chosen from1choice
最后通過重定向的方法恢復(fù)被刪除的/var/log/messages
cat /pro/5925/fd/1 >/var/log/messages