lsof (list open files)是一個(gè)列出當(dāng)前系統(tǒng)打開(kāi)文件的工具城榛。在linux系統(tǒng)環(huán)境下沪铭,任何事物都可以以文件形式存在,通過(guò)文件不僅可以訪問(wèn)常規(guī)的數(shù)據(jù)除抛,還可以訪問(wèn)網(wǎng)絡(luò)連接和硬件薯酝。
適應(yīng)條件:lsof訪問(wèn)的是核心文件和各種文件半沽,所以必須以root用戶的身份運(yùn)行才能充分發(fā)揮其功能。
lsof [選項(xiàng)] [絕對(duì)路徑的文件名]
顯示示例
[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
每行顯示一個(gè)打開(kāi)的文件吴菠,默認(rèn)如果后面不跟任何東西者填,將打開(kāi)系統(tǒng)打開(kāi)的所有文件
COMMAND :進(jìn)程名稱
PID:進(jìn)程標(biāo)識(shí)符
USER:進(jìn)程所有者
FD:文件描述符,應(yīng)用程序通過(guò)文件描述符識(shí)別到該文件橄务。如cwd幔托、txt等
TYPE:文件類型,如DIR,REG
DEVICE:指定磁盤名稱
SIZE:文件大小
NODE:索引節(jié)點(diǎn)(文件在磁盤上的標(biāo)識(shí))
NAME:打開(kāi)文件的確切名稱
補(bǔ)充:FD列中的文件描述cwd值表示應(yīng)用程序的當(dāng)前工作目錄,這是該程序啟動(dòng)的目錄重挑,除非它本身對(duì)這個(gè)目錄進(jìn)行更改嗓化。txt類型的是程序代碼,如應(yīng)用程序二進(jìn)制文件本身或者共享庫(kù)谬哀。其
次數(shù)值表示應(yīng)用程序的文件描述符刺覆,這是打開(kāi)文件時(shí)一個(gè)返回的一個(gè)整數(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表示該文件被打開(kāi)處于讀取\寫(xiě)入模式史煎,而不是只讀或只寫(xiě)模式谦屑;
r 只讀 ; w 只寫(xiě) 篇梭;W表示該應(yīng)用程序具有對(duì)整個(gè)文件的寫(xiě)鎖(確保每次只能打開(kāi)一次應(yīng)用程序?qū)嵗?/p>
初始打開(kāi)每個(gè)應(yīng)用程序時(shí)氢橙,都具有三個(gè)文件描述符,從0到2恬偷,分別表示標(biāo)準(zhǔn)輸入悍手、輸出和錯(cuò)誤流。因此袍患,大多數(shù)應(yīng)用程序
所打開(kāi)的FD都是從3開(kāi)始
TYPE:REG坦康、DIR、CHR诡延、BLK滞欠、UNIX、FIFO肆良、IPV4
(2)
查看端口現(xiàn)在運(yùn)行的情況
ls -i:port? #某個(gè)端口
ls -i:port1-port2 #
ls -i:1-1024????? #查看端口1-1024運(yùn)行情況
(3)恢復(fù)刪除文件
當(dāng)系統(tǒng)中的某個(gè)文件被意外刪除了筛璧,只要這個(gè)時(shí)候系統(tǒng)中有進(jìn)程正在訪問(wèn)這個(gè)文件,那么可以通過(guò)lsof 從/proc目錄下恢復(fù)文件的內(nèi)容
假如/var/log/messages文件被刪了惹恃,恢復(fù)這個(gè)文件的方法:
首先使用lsof 查看當(dāng)前是否有進(jìn)程打開(kāi)/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)打開(kāi)文件的文件描述符為1座舍,同時(shí)發(fā)現(xiàn)/var/log/messages已經(jīng)被刪除了。
因此可以通過(guò)/var/log/messages文件描述符來(lái)查看文件信息陨帆。
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
最后通過(guò)重定向的方法恢復(fù)被刪除的/var/log/messages
cat /pro/5925/fd/1 >/var/log/messages