每日Linux命令(4):lsof
lsof(list open files)是一個列出當前系統開啟檔案的工具。在linux環境下,任何事物都以檔案的形式存在,透過檔案不僅僅可以訪問常規資料,還可以訪問網路連線和硬體。所以如傳輸控制協議 (TCP) 和使用者資料包協議 (UDP) 套接字等,系統在後臺都為該應用程式分配了一個檔案描述符,無論這個檔案的本質如何,該檔案描述符為應用程式與基礎作業系統之間的互動提供了通用介面。因為應用程式開啟檔案的描述符列表提供了大量關於這個應用程式本身的資訊,因此透過lsof工具能夠檢視這個列表對系統監測以及排錯將是很有幫助的。lsof只能以root許可權執行。
1、命令格式
lsof(選項)
2、選項引數
-a:列出開啟檔案存在的程式;
-c<程式名>:列出指定程式所開啟的檔案;
-g:列出GID號程式詳情;
-d<檔案號>:列出佔用該檔案號的程式;
+d<目錄>:列出目錄下被開啟的檔案;
+D<目錄>:遞迴列出目錄下被開啟的檔案;
-n<目錄>:列出使用NFS的檔案;
-i<條件>:列出符合條件的程式。(4、6、協議、:埠、 @ip )
-p<程式號>:列出指定程式號所開啟的檔案;
-u:列出UID號程式詳情;
-h:顯示幫助資訊;
-v:顯示版本資訊。
3、使用示例(以CentOS 7版本為例)
示例一:顯示Linux系統當前已開啟的所有檔案列表(前40項)
[root@wjq ~]# lsof | head -40
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID TID
USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 253,0 4096 64 /
systemd 1 root rtd
DIR 253,0 4096 64 /
systemd 1 root txt
REG 253,0 1482128 4221 /usr/lib/systemd/systemd
systemd 1 root mem
REG 253,0 20040
33683824 /usr/lib64/libuuid.so.1.3.0
systemd 1 root mem
REG 253,0 256960
33895489 /usr/lib64/libblkid.so.1.1.0
systemd 1 root mem
REG 253,0 90664
33683808 /usr/lib64/libz.so.1.2.7
systemd 1 root mem
REG 253,0 157424
33683835 /usr/lib64/liblzma.so.5.2.2
systemd 1 root
mem REG 253,0 23968
33683981 /usr/lib64/libcap-ng.so.0.0.0
systemd 1 root mem
REG 253,0 19888
33684065 /usr/lib64/libattr.so.1.1.0
systemd 1 root mem
REG 253,0 19344
33620992 /usr/lib64/libdl-2.17.so
systemd 1 root mem
REG 253,0 398264
33683764 /usr/lib64/libpcre.so.1.2.0
systemd 1 root mem
REG 253,0
2116736 33620986
/usr/lib64/libc-2.17.so
systemd 1 root mem
REG 253,0 143352
33621012 /usr/lib64/libpthread-2.17.so
systemd 1 root mem
REG 253,0 88720
33554508 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
systemd 1 root mem
REG 253,0 43928
33621016 /usr/lib64/librt-2.17.so
systemd 1 root mem
REG 253,0 265008
33895601 /usr/lib64/libmount.so.1.1.0
systemd 1 root mem
REG 253,0 91792
33858785 /usr/lib64/libkmod.so.2.2.10
systemd 1 root mem
REG 253,0 122936
33683983 /usr/lib64/libaudit.so.1.0.0
systemd 1 root mem
REG 253,0 61672
33941999 /usr/lib64/libpam.so.0.83.1
systemd 1 root mem
REG 253,0 20024
33684068 /usr/lib64/libcap.so.2.22
systemd 1 root mem
REG 253,0 155744
33683773 /usr/lib64/libselinux.so.1
systemd 1 root mem
REG 253,0 155064
33620979 /usr/lib64/ld-2.17.so
systemd 1 root 0u
CHR 1,3 0t0 4787 /dev/null
systemd 1 root 1u
CHR 1,3 0t0 4787 /dev/null
systemd 1 root 2u
CHR 1,3 0t0
4787 /dev/null
systemd 1 root 3u
a_inode 0,9 0 4783 [timerfd]
systemd 1 root 4u
a_inode 0,9 0 4783 [eventpoll]
systemd 1 root 5u
a_inode 0,9 0 4783 [signalfd]
systemd 1 root 6r
DIR 0,20 0 6749 /sys/fs/cgroup/systemd
systemd 1 root 7u
a_inode 0,9 0 4783 [timerfd]
systemd 1 root 8u
netlink
0t0 11369 KOBJECT_UEVENT
systemd 1 root 9r
REG 0,3 0 7007 /proc/1/mountinfo
systemd 1 root 10r
a_inode 0,9 0 4783 inotify
systemd 1 root 11r
REG 0,3 0 4026532019 /proc/swaps
systemd 1 root 12u
unix 0xffff880035cef000 0t0
11376 /run/systemd/private
systemd 1 root 14r
a_inode 0,9 0 4783 inotify
systemd 1 root 19u
netlink
0t0 11381 AUDIT
systemd 1 root 20u
FIFO 0,18 0t0
11405 /run/systemd/initctl/fifo
systemd 1 root 21u
unix 0xffff880035cefc00
0t0 11413
/run/lvm/lvmetad.socket
說明:
每行顯示一個開啟的檔案,若不指定條件預設將顯示所有程式開啟的所有檔案。
lsof輸出各列資訊的意義如下:
COMMAND:程式的名稱 PID:程式識別符號
USER:程式所有者
FD:檔案描述符,應用程式透過檔案描述符識別該檔案。如cwd、txt等
(1)cwd:表示current work dirctory,即:應用程式的當前工作目錄,這是該應用程式啟動的目錄,除非它本身對這個目錄進行更改
(2)txt :該型別的檔案是程式程式碼,如應用程式二進位制檔案本身或共享庫,如上列表中顯示的 /sbin/init 程式
(3)Lnn:library
references (AIX);
(4)er:FD
information error (see NAME column);
(5)jld:jail
directory (FreeBSD);
(6)ltx:shared
library text (code and data);
(7)Mxx :hex memory-mapped type number xx.
(8)m86:DOS
Merge mapped file;
(9)mem:memory-mapped
file;
(10)mmap:memory-mapped device;
(11)pd:parent
directory;
(12)rtd:root directory;
(13)tr:kernel
trace file (OpenBSD);
(14)v86 VP/ix mapped
file;
(15)0:表示標準輸出
(16)1:表示標準輸入
(17)2:表示標準錯誤
一般在標準輸出、標準錯誤、標準輸入後還跟著檔案狀態模式:r、w、u等
(1)u:表示該檔案被開啟並處於讀取/寫入模式
(2)r:表示該檔案被開啟並處於只讀模式
(3)w:表示該檔案被開啟並處於
(4)空格:表示該檔案的狀態模式為unknow,且沒有鎖定
(5)-:表示該檔案的狀態模式為unknow,且被鎖定
同時在檔案狀態模式後面,還跟著相關的鎖
(1)N:for
a Solaris NFS lock of unknown type;
(2)r:for
read lock on part of the file;
(3)R:for
a read lock on the entire file;
(4)w:for
a write lock on part of the file;(檔案的部分寫鎖)
(5)W:for
a write lock on the entire file;(整個檔案的寫鎖)
(6)u:for
a read and write lock of any length;
(7)U:for
a lock of unknown type;
(8)x:for
an SCO OpenServer Xenix lock on part of the file;
(9)X:for
an SCO OpenServer Xenix lock on the entire file;
(10)space:if there is no lock.
TYPE:檔案型別,如DIR、REG等
(1)DIR:表示目錄
(2)CHR:表示字元型別
(3)BLK:塊裝置型別
(4)UNIX: UNIX 域套接字
(5)FIFO:先進先出 (FIFO) 佇列
(6)IPv4:網際協議 (IP) 套接字
DEVICE:指定磁碟的名稱
SIZE:檔案的大小
NODE:索引節點(檔案在磁碟上的標識)
NAME:開啟檔案的確切名稱
示例二:查詢誰在使用檔案系統
解除安裝檔案系統時,如果該檔案系統中有任何開啟的檔案,操作通常將會失敗。那麼透過lsof可以找出那些程式在使用當前要解除安裝的檔案系統,如下:
[root@wjq ~]# lsof /home
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
tnslsnr 3780 oracle cwd
DIR 253,2 4096
71 /home/oracle
bash 27386 root
cwd DIR 253,2
4096 71 /home/oracle
vim 27442 root
cwd DIR 253,2
4096 71 /home/oracle
vim 27442 root
3u REG 253,2 4096
114 /home/oracle/.1.txt.swp
在這個示例中,使用者root正在其/home/oracle目錄中進行一些操作。一個 bash是例項正在執行,並且它當前的目錄為/home/oracle,另一個則顯示的是vim正在編輯/home/oracle下的檔案1.txt。要成功地解除安裝/home/oracle,應該在通知使用者以確保情況正常之後,中止這些程式。 這個示例說明了應用程式的當前工作目錄非常重要,因為它仍保持著檔案資源,並且可以防止檔案系統被解除安裝。這就是為什麼大部分守護程式(後臺程式)將它們的目錄更改為根目錄、或服務特定的目錄(如 sendmail 示例中的 /var/spool/mqueue)的原因,以避免該守護程式阻止解除安裝不相關的檔案系統。
示例三:檢視誰在使用某一個檔案,也就是查詢某一個檔案相關的程式
[root@wjq ~]# lsof /bin/bash
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be incomplete.
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE NAME
ksmtuned 660 root txt
REG 253,0 960392 85563 /usr/bin/bash
mysqld_sa 994 mysql txt REG
253,0 960392 85563 /usr/bin/bash
bash 27094 root txt
REG 253,0 960392 85563 /usr/bin/bash
bash 27386 root txt
REG 253,0 960392 85563 /usr/bin/bash
示例四:遞迴的查詢某一目錄的檔案資訊
[root@wjq ~]# lsof +D /etc/
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE NAME
systemd-u 507 root
mem REG 253,0
7289802 67164582
/etc/udev/hwdb.bin
systemd-u 507 root
6r REG 253,0
7289802 67164582 /etc/udev/hwdb.bin
avahi-dae 632 avahi cwd
DIR 253,0 71 102480993 /etc/avahi
avahi-dae 632 avahi rtd
DIR 253,0 71 102480993 /etc/avahi
cupsd 931 root
10r REG 253,0
2392 69280429 /etc/passwd
gnome-ses 1459 gdm mem
REG 253,0 395 102699532 /etc/dconf/db/distro
gnome-ses 1459 gdm mem
REG 253,0 104 102656452 /etc/dconf/db/site
gnome-ses 1459 gdm mem
REG 253,0 104 101248845 /etc/dconf/db/local
gnome-ses 1459 gdm mem
REG 253,0 104 100950876 /etc/dconf/db/gdm
at-spi-bu 1466 gdm mem
REG 253,0 395 102699532 /etc/dconf/db/distro
at-spi-bu 1466 gdm mem
REG 253,0 104 102656452 /etc/dconf/db/site
at-spi-bu 1466 gdm mem
REG 253,0 104 101248845 /etc/dconf/db/local
at-spi-bu 1466 gdm mem
REG 253,0 104 100950876 /etc/dconf/db/gdm
gnome-set 1482 gdm mem
REG 253,0 395 102699532 /etc/dconf/db/distro
gnome-set 1482 gdm mem
REG 253,0 104 102656452 /etc/dconf/db/site
gnome-set 1482 gdm mem
REG 253,0 104 101248845 /etc/dconf/db/local
gnome-set 1482 gdm mem
REG 253,0 104 100950876 /etc/dconf/db/gdm
gnome-set 1482 gdm 14r
REG 253,0 65536
34100937 /etc/pki/nssdb/cert8.db
gnome-set 1482 gdm 16r
REG 253,0 16384
34100939 /etc/pki/nssdb/key3.db
gnome-she 1513 gdm mem
REG 253,0 395 102699532 /etc/dconf/db/distro
gnome-she 1513 gdm mem
REG 253,0 104 102656452 /etc/dconf/db/site
gnome-she 1513 gdm mem
REG 253,0 104 101248845 /etc/dconf/db/local
gnome-she 1513 gdm mem
REG 253,0 104 100950876 /etc/dconf/db/gdm
ibus-dcon 1548 gdm mem
REG 253,0 395 102699532 /etc/dconf/db/distro
ibus-dcon 1548 gdm mem
REG 253,0 104 102656452 /etc/dconf/db/site
ibus-dcon 1548 gdm mem
REG 253,0 104 101248845 /etc/dconf/db/local
ibus-dcon 1548 gdm mem
REG 253,0 104 100950876 /etc/dconf/db/gdm
mission-c 1557 gdm mem
REG 253,0 395 102699532 /etc/dconf/db/distro
mission-c 1557 gdm mem
REG 253,0 104 102656452 /etc/dconf/db/site
mission-c 1557 gdm mem
REG 253,0 104 101248845 /etc/dconf/db/local
mission-c 1557 gdm mem
REG 253,0 104 100950876 /etc/dconf/db/gdm
goa-daemo 1580 gdm mem
REG 253,0 395 102699532 /etc/dconf/db/distro
goa-daemo 1580 gdm mem
REG 253,0 104 102656452 /etc/dconf/db/site
goa-daemo 1580 gdm mem
REG 253,0 104 101248845 /etc/dconf/db/local
goa-daemo 1580 gdm mem
REG 253,0 104
100950876 /etc/dconf/db/gdm
示例五:檢視某個使用者開啟的檔案資訊
[root@wjq ~]# lsof -u oracle |
head -40
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID USER
FD TYPE DEVICE
SIZE/OFF NODE NAME
oracle 1832 oracle cwd
DIR 253,0 164
33584428 /u01/app/oracle/product/11.2.0/dbhome_1/dbs
oracle 1832 oracle rtd
DIR 253,0 4096 64 /
oracle 1832 oracle txt REG 253,0 239520824 104141161
/u01/app/oracle/product/11.2.0/dbhome_1/bin/oracle
oracle 1832 oracle mem
REG 0,17 16777216
25163 /dev/shm/ora_seiang11g_131073_0
oracle 1832 oracle mem
REG 0,17 0
25167 /dev/shm/ora_seiang11g_163842_0
oracle 1832 oracle mem
REG 0,17 0
25168 /dev/shm/ora_seiang11g_163842_1
oracle 1832 oracle mem
REG 0,17 0
25169 /dev/shm/ora_seiang11g_163842_2
oracle 1832 oracle mem
REG 0,17 0
25170 /dev/shm/ora_seiang11g_163842_3
oracle 1832 oracle mem
REG 0,17 0
25171 /dev/shm/ora_seiang11g_163842_4
oracle 1832 oracle mem
REG 0,17 0
25172 /dev/shm/ora_seiang11g_163842_5
oracle 1832 oracle mem
REG 0,17 0
25173 /dev/shm/ora_seiang11g_163842_6
oracle 1832 oracle mem
REG 0,17 0
25174 /dev/shm/ora_seiang11g_163842_7
oracle 27596 oracle mem
CHR 1,5 4789 /dev/zero
oracle 27596 oracle mem
REG 253,0 61752
33621004 /usr/lib64/libnss_files-2.17.so
oracle 27596 oracle mem
REG 253,0 88720
33554508 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
oracle 27596 oracle mem
REG 253,0 50712
33858575 /usr/lib64/libnuma.so.1
oracle 27596 oracle mem
REG 253,0 2116736
33620986 /usr/lib64/libc-2.17.so
oracle 27596 oracle mem
REG 253,0 113152
33620996 /usr/lib64/libnsl-2.17.so
oracle 27596 oracle mem
REG 253,0 143352
33621012 /usr/lib64/libpthread-2.17.so
oracle 27596 oracle mem REG 253,0 1141456
33620994 /usr/lib64/libm-2.17.so
oracle 27596 oracle mem
REG 253,0 19344
33620992 /usr/lib64/libdl-2.17.so
oracle 27596 oracle mem
REG 253,0 6264
33858589 /usr/lib64/libaio.so.1.0.1
……(此處省略N行)
oracle 27596 oracle mem
REG 253,0 156012
72091029 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libocrutl11.so
oracle 27596 oracle mem
REG 253,0 3354840
72091028 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libocrb11.so
oracle 27596 oracle mem
REG 253,0 1612720
72091027 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libocr11.so
oracle 27596 oracle mem
REG 253,0 12787
69872407 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libskgxn2.so
oracle 27596 oracle mem
REG 253,0 17647384
72091023 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libhasgen11.so
oracle 27596 oracle mem
REG 253,0 161796
69872194 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libdbcfg11.so
oracle 27596 oracle mem
REG 253,0 241443
72091025 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libclsra11.so
oracle 27596 oracle mem
REG 253,0 7996693
72151264 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libnnz11.so
oracle 27596 oracle mem
REG 253,0 43928
33621016 /usr/lib64/librt-2.17.so
oracle 27596 oracle mem
REG 253,0 1133264
71108630 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libskgxp11.so
oracle 27596 oracle mem
REG 253,0 720412
72151276 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libcell11.so
oracle 27596 oracle mem
REG 253,0 155064
33620979 /usr/lib64/ld-2.17.so
oracle 27596 oracle DEL
REG 0,10 1628455 /[aio]
oracle 27596 oracle mem
REG 253,0 12291
70011741 /u01/app/oracle/product/11.2.0/dbhome_1/lib/libodmd11.so
oracle 27596 oracle 0r
CHR 1,3 0t0
4787 /dev/null
oracle 27596 oracle 1w
CHR 1,3 0t0
4787 /dev/null
oracle 27596 oracle 2w
CHR 1,3 0t0
4787 /dev/null
oracle 27596 oracle 3r
CHR 1,3 0t0
4787 /dev/null
--列出某個使用者外所開啟的檔案資訊
[root@wjq ~]# lsof -u ^root
說明:^這個符號在使用者名稱之前,將會把是root使用者開啟的程式不讓顯示
示例六:列出某個程式所開啟的檔案資訊
[root@wjq ~]# lsof -c mysql
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF
NODE NAME
mysqld_sa 994 mysql cwd
DIR 253,0 4096 64 /
mysqld_sa 994 mysql rtd
DIR 253,0 4096 64 /
mysqld_sa 994 mysql txt
REG 253,0 960392
85563 /usr/bin/bash
mysqld_sa 994 mysql mem
REG 253,0
106070960 85554
/usr/lib/locale/locale-archive
……(此處省略N行)
mysqld 1178 mysql
19u REG 253,0
532 48713024
/var/lib/mysql/mysql/user.MYD
mysqld 1178 mysql 20u
REG 253,0 5120
48713017 /var/lib/mysql/mysql/db.MYI
mysqld 1178 mysql 21u
REG 253,0 880
48713018 /var/lib/mysql/mysql/db.MYD
mysqld 1178 mysql 22u
REG 253,0 5120
50288030 /var/lib/mysql/mysql/proxies_priv.MYI
mysqld 1178 mysql 23u
REG 253,0 1386
50288031 /var/lib/mysql/mysql/proxies_priv.MYD
mysqld 1178 mysql 24u
REG 253,0 4096
48713035 /var/lib/mysql/mysql/tables_priv.MYI
mysqld 1178 mysql 25u
REG 253,0 0
48713036 /var/lib/mysql/mysql/tables_priv.MYD
mysqld 1178 mysql 26u
REG 253,0 4096
48713038 /var/lib/mysql/mysql/columns_priv.MYI
mysqld 1178 mysql 27u
REG 253,0 0
48713039 /var/lib/mysql/mysql/columns_priv.MYD
mysqld 1178 mysql 28u
REG 253,0 4096
50288015 /var/lib/mysql/mysql/procs_priv.MYI
mysqld 1178 mysql 29u
REG 253,0 0
50288016 /var/lib/mysql/mysql/procs_priv.MYD
mysqld 1178 mysql 30u
REG 253,0 1024
48713032 /var/lib/mysql/mysql/servers.MYI
mysqld 1178 mysql 31u
REG 253,0 0
48713033 /var/lib/mysql/mysql/servers.MYD
mysqld 1178 mysql 32u
REG 253,0 2048
50288024 /var/lib/mysql/mysql/event.MYI
mysqld 1178 mysql 33u
REG 253,0 0
50288025 /var/lib/mysql/mysql/event.MYD
--列出多個程式所開啟的檔案資訊
[root@wjq ~]# lsof -c mysql -c sshd
--列出某個程式某個使用者所開啟的檔案資訊
[root@wjq ~]# lsof -c sshd -u oracle
示例七:列出多個程式號對應的檔案資訊
[root@wjq ~]# lsof -p 2,3,9
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID USER FD
TYPE DEVICE SIZE/OFF NODE NAME
kthreadd 2 root cwd
DIR 253,0 4096
64 /
kthreadd 2 root rtd
DIR 253,0 4096
64 /
kthreadd 2 root txt
unknown /proc/2/exe
ksoftirqd 3 root cwd
DIR 253,0 4096
64 /
ksoftirqd 3 root rtd
DIR 253,0 4096
64 /
ksoftirqd 3 root txt
unknown
/proc/3/exe
rcu_sched 9 root cwd
DIR 253,0 4096
64 /
rcu_sched 9 root rtd
DIR 253,0 4096
64 /
rcu_sched 9 root txt
unknown
/proc/9/exe
--列出除了某個程式號外的其他程式開啟檔案的資訊
[root@wjq ~]# lsof -p ^2
示例八:列出所有的網路連線
[root@wjq ~]# lsof -i
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root
38u IPv6 14043
0t0 TCP *:sunrpc (LISTEN)
systemd 1 root
39u IPv4 14044
0t0 TCP *:sunrpc (LISTEN)
avahi-dae 632 avahi
12u IPv4 16000
0t0 UDP *:mdns
avahi-dae 632 avahi
13u IPv4 16001
0t0 UDP *:59037
cupsd 931 root
11u IPv6 20259
0t0 TCP localhost:ipp (LISTEN)
cupsd 931 root
12u IPv4 20260
0t0 TCP localhost:ipp (LISTEN)
httpd 940 root
4u IPv6 20914
0t0 TCP *:http (LISTEN)
sshd 978 root
3u IPv4 19298
0t0 TCP *:ssh (LISTEN)
sshd 978 root
4u IPv6 19307
0t0 TCP *:ssh (LISTEN)
mysqld 1178 mysql
14u IPv4 20988
0t0 TCP *:mysql (LISTEN)
master 1239 root
13u IPv4 20739
0t0 TCP localhost:smtp (LISTEN)
master 1239 root
14u IPv6 20740
0t0 TCP localhost:smtp (LISTEN)
httpd 1330 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1331 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1332 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1333 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1334 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
dnsmasq 1386 nobody 3u
IPv4 21368 0t0
UDP *:bootps
dnsmasq 1386 nobody 5u
IPv4 21375 0t0
UDP wjq.comsys.com:domain
dnsmasq 1386 nobody 6u
IPv4 21376 0t0
TCP wjq.comsys.com:domain (LISTEN)
oracle 1832 oracle 9u
IPv6 25267 0t0
UDP localhost:41804
oracle 1832 oracle 12u
IPv4 44942 0t0
TCP wjq.comsys.com:58605->wjq.comsys.com:ncube-lm (ESTABLISHED)
oracle 1852 oracle 14u
IPv6 30917 0t0
UDP *:56442
oracle 1860 oracle 14u
IPv6 25837 0t0
UDP *:44721
oracle 1864 oracle 9u
IPv6 25506 0t0
UDP localhost:18001
oracle 1864 oracle 10u
IPv6 25509 0t0
TCP *:28327 (LISTEN)
oracle 1866 oracle 9u
IPv6 25501 0t0
UDP localhost:17685
tnslsnr 3780 oracle 8u
IPv6 44829 0t0
TCP *:ncube-lm (LISTEN)
tnslsnr 3780 oracle 13u
IPv6 44943 0t0
TCP wjq.comsys.com:ncube-lm->wjq.comsys.com:58605 (ESTABLISHED)
sshd 27091 root
3u IPv4 1620474 0t0
TCP wjq.comsys.com:ssh->10.1.1.123:60667 (ESTABLISHED)
--列出所有tcp和udp的網路連線資訊
[root@wjq ~]# lsof -i udp(tcp)
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
avahi-dae 632 avahi
12u IPv4 16000
0t0 UDP *:mdns
avahi-dae 632 avahi
13u IPv4 16001
0t0 UDP *:59037
dnsmasq 1386 nobody 3u
IPv4 21368 0t0
UDP *:bootps
dnsmasq 1386 nobody 5u
IPv4 21375 0t0
UDP wjq.comsys.com:domain
oracle 1832 oracle 9u
IPv6 25267 0t0
UDP localhost:41804
oracle 1852 oracle 14u
IPv6 30917 0t0
UDP *:56442
oracle 1860 oracle 14u
IPv6 25837 0t0
UDP *:44721
oracle 1864 oracle 9u
IPv6 25506 0t0
UDP localhost:18001
oracle 1866 oracle 9u
IPv6 25501 0t0
UDP localhost:17685
--列出誰在使用某一個埠
[root@wjq ~]# lsof -i:3306
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
[root@wjq ~]#
[root@wjq ~]# lsof
-i:80
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
httpd 940 root
4u IPv6 20914
0t0 TCP *:http (LISTEN)
httpd 1330 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1331 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1332 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1333 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1334 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
--檢視誰在使用某一個特定的udp埠
[root@wjq ~]# lsof -i udp:56442
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
oracle 1852 oracle 14u
IPv6 30917 0t0
UDP *:56442
[root@wjq ~]# lsof -i tcp:80
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
httpd 940 root
4u IPv6 20914
0t0 TCP *:http (LISTEN)
httpd 1330 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1331 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1332 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1333 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
httpd 1334 apache 4u
IPv6 20914 0t0
TCP *:http (LISTEN)
--檢視與10.1.1.46的連線情況
[root@wjq ~]# lsof -i @10.1.1.46
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE NAME
oracle 1832 oracle 12u
IPv4 44942 0t0
TCP wjq.comsys.com:58605->wjq.comsys.com:ncube-lm (ESTABLISHED)
tnslsnr 3780 oracle 13u
IPv6 44943 0t0
TCP wjq.comsys.com:ncube-lm->wjq.comsys.com:58605 (ESTABLISHED)
sshd 27091 root
3u IPv4 1620474 0t0
TCP wjq.comsys.com:ssh->10.1.1.123:60667 (ESTABLISHED)
--檢視與10.1.1.46的58605埠的檔案開啟情況
[root@wjq ~]# lsof -i
@10.1.1.46:58605
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
oracle 1832 oracle 12u
IPv4 44942 0t0
TCP wjq.comsys.com:58605->wjq.comsys.com:ncube-lm (ESTABLISHED)
tnslsnr 3780 oracle 13u IPv6
44943 0t0 TCP wjq.comsys.com:ncube-lm->wjq.comsys.com:58605
(ESTABLISHED)
--列出某一使用者的所有活躍網路埠
[root@wjq ~]# lsof -a -i -u
oracle
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
oracle 1832 oracle 9u
IPv6 25267 0t0
UDP localhost:41804
oracle 1832 oracle 12u
IPv4 44942 0t0
TCP wjq.comsys.com:58605->wjq.comsys.com:ncube-lm (ESTABLISHED)
oracle 1852 oracle 14u
IPv6 30917 0t0
UDP *:56442
oracle 1860 oracle 14u
IPv6 25837 0t0
UDP *:44721
oracle 1864 oracle 9u
IPv6 25506 0t0
UDP localhost:18001
oracle 1864 oracle 10u
IPv6 25509 0t0
TCP *:28327 (LISTEN)
oracle 1866 oracle 9u
IPv6 25501 0t0
UDP localhost:17685
tnslsnr 3780 oracle 8u IPv6
44829 0t0 TCP *:ncube-lm (LISTEN)
tnslsnr 3780 oracle 13u IPv6
44943 0t0 TCP
wjq.comsys.com:ncube-lm->wjq.comsys.com:58605 (ESTABLISHED)
示例九:檢視某個使用者組所開啟的檔案資訊
[root@wjq ~]# id
uid=0(root) gid=0(root) groups=0(root)
[root@wjq ~]#
[root@wjq ~]# lsof
-g 0 | head -10
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID PGID USER FD
TYPE DEVICE SIZE/OFF NODE NAME
kthreadd 2 0 root
cwd DIR 253,0
4096 64 /
kthreadd 2 0 root
rtd DIR 253,0
4096 64 /
kthreadd 2 0 root
txt unknown /proc/2/exe
ksoftirqd 3 0 root
cwd DIR 253,0
4096 64 /
ksoftirqd 3 0 root
rtd DIR 253,0
4096 64 /
ksoftirqd 3 0 root
txt unknown /proc/3/exe
migration 7 0 root
cwd DIR 253,0
4096 64 /
migration 7 0 root
rtd DIR 253,0
4096 64 /
migration 7 0 root
txt unknown /proc/7/exe
示例十:顯示COMMAND列中以scsi和sh開頭的所有開啟檔案
[root@wjq ~]# lsof -c scsi -c sh
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
COMMAND PID USER FD
TYPE DEVICE SIZE/OFF NODE NAME
scsi_eh_0 271 root cwd DIR
253,0 4096 64 /
scsi_eh_0 271 root rtd DIR
253,0 4096 64 /
scsi_eh_0 271 root txt unknown /proc/271/exe
scsi_tmf_ 273 root cwd DIR
253,0 4096 64 /
scsi_tmf_ 273 root rtd DIR
253,0 4096 64 /
scsi_tmf_ 273 root txt unknown /proc/273/exe
scsi_eh_1 274 root cwd DIR
253,0 4096 64 /
scsi_eh_1 274 root rtd DIR
253,0 4096 64 /
scsi_eh_1 274 root txt unknown /proc/274/exe
scsi_tmf_ 276 root cwd DIR
253,0 4096 64 /
scsi_tmf_ 276 root rtd DIR
253,0 4096 64 /
scsi_tmf_ 276 root txt unknown /proc/276/exe
scsi_eh_2 290 root cwd DIR
253,0 4096 64 /
scsi_eh_2 290 root rtd DIR
253,0 4096 64 /
scsi_eh_2 290 root txt unknown /proc/290/exe
scsi_tmf_ 291 root cwd DIR
253,0 4096 64 /
scsi_tmf_ 291 root rtd DIR
253,0 4096 64 /
scsi_tmf_ 291 root txt unknown /proc/291/exe
示例十一:每隔三秒顯示一次
[root@wjq ~]# lsof -i:3306 -r 3
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
[root@wjq ~]# lsof
-i:3306 +r 3
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
COMMAND PID USER
FD TYPE DEVICE SIZE/OFF NODE
NAME
mysqld 1178 mysql 14u
IPv4 20988 0t0
TCP *:mysql (LISTEN)
=======
說明:
-r:每隔三秒顯示一次,直到ctrl+c退出
+r:每隔三秒顯示一次,直到lsof中沒有資料了或ctrl+c退出
示例十二:恢復刪除的檔案
當Linux計算機受到入侵時,常見的情況是日誌檔案被刪除,以掩蓋攻擊者的蹤跡。管理錯誤也可能導致意外刪除重要的檔案,比如在清理舊日誌時,意外地刪除了資料庫的活動事務日誌。有時可以透過lsof來恢復這些檔案。
當程式開啟了某個檔案時,只要該程式保持開啟該檔案,即使將其刪除,它依然存在於磁碟中。這意味著,程式並不知道檔案已經被刪除,它仍然可以向開啟該檔案時提供給它的檔案描述符進行讀取和寫入。除了該程式之外,這個檔案是不可見的,因為已經刪除了其相應的目錄索引節點。
在/proc目錄下,其中包含了反映核心和程式樹的各種檔案。/proc目錄掛載的是在記憶體中所對映的一塊區域,所以這些檔案和目錄並不存在於磁碟中,因此當我們對這些檔案進行讀取和寫入時,實際上是在從記憶體中獲取相關資訊。大多數與lsof相關的資訊都儲存於以程式的 PID 命名的目錄中,即 /proc/1234 中包含的是 PID 為 1234 的程式的資訊。每個程式目錄中存在著各種檔案,它們可以使得應用程式簡單地瞭解程式的記憶體空間、檔案描述符列表、指向磁碟上的檔案的符號連結和其他系統資訊。lsof 程式使用該資訊和其他關於核心內部狀態的資訊來產生其輸出。所以lsof 可以顯示程式的檔案描述符和相關的檔名等資訊。也就是我們透過訪問程式的檔案描述符可以找到該檔案的相關資訊。
當系統中的某個檔案被意外地刪除了,只要這個時候系統中還有程式正在訪問該檔案,那麼我們就可以透過lsof從/proc目錄下恢復該檔案的內容。
假如由於誤操作將/var/log/messages檔案刪除掉了,那麼這時要將/var/log/messages檔案恢復的方法如下:
首先使用lsof來檢視當前是否有程式開啟/var/logmessages檔案,如下:
[root@wjq ~]# lsof | grep
/var/log/messages
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
abrt-watc 650 root 4r
REG 253,0 53823
48711749 /var/log/messages
rsyslogd 932 root 3w
REG 253,0 53823
48711749 /var/log/messages
in:imjour 932 965
root 3w REG 253,0 53823
48711749 /var/log/messages
rs:main 932 966
root 3w REG 253,0 53823
48711749 /var/log/messages
從上面的資訊可以看到 PID 932(syslogd)開啟檔案的檔案描述符為 3。同時還可以看到/var/log/messages已經標記被刪除了。因此我們可以在 /proc/932/fd/3 (fd下的每個以數字命名的檔案表示程式對應的檔案描述符)中檢視相應的資訊,如下:
[root@wjq fd]# head -n 10 /proc/932/fd/3
Oct 29 03:39:01 wjq rsyslogd: [origin software="rsyslogd"
swVersion="7.4.7" x-pid="932"
x-info=""] rsyslogd was HUPed
Oct 29 03:39:01 wjq logrotate: ALERT exited abnormally with [1]
Oct 29 03:40:01 wjq systemd: Started Session 596 of user root.
Oct 29 03:40:01 wjq systemd: Starting Session 596 of user root.
Oct 29 03:50:01 wjq systemd: Started Session 597 of user root.
Oct 29 03:50:01 wjq systemd: Starting Session 597 of user root.
Oct 29 04:00:01 wjq systemd: Started Session 598 of user root.
Oct 29 04:00:01 wjq systemd: Starting Session 598 of user root.
Oct 29 04:01:01 wjq systemd: Started Session 599 of user root.
Oct 29 04:01:01 wjq systemd: Starting Session 599 of user root.
如果可以透過檔案描述符檢視相應的資料,那麼就可以使用 I/O 重定向將其複製到檔案中,如:
[root@wjq fd]# cat /proc/932/fd/3
> /var/log/messages
[root@wjq fd]# lsof | grep /var/log/messages
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/42/gvfs
Output information may be
incomplete.
rsyslogd 932 root
3w REG 253,0 53950
48711749 /var/log/messages
in:imjour 932 965
root 3w REG 253,0 53950
48711749 /var/log/messages
rs:main 932 966
root 3w REG 253,0
53950 48711749 /var/log/messages
對於許多應用程式,尤其是日誌檔案和資料庫,這種恢復刪除檔案的方法非常有用。
作者:SEian.G(苦練七十二變,笑對八十一難)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31015730/viewspace-2146635/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- linux每日命令(39):lsof命令Linux
- Linux命令--lsofLinux
- Linux lsof命令解析Linux
- linux沒有lsof命令Linux
- linux lsof命令詳解Linux
- Linux lsof 命令簡介Linux
- Linux-lsof命令詳解Linux
- lsof 命令
- lsof命令
- 每天一個 Linux 命令(51): lsof 命令Linux
- linux下檢視埠命令lsofLinux
- Linux技術——lsof命令詳解Linux
- Linux 有用的命令之 - lsof(轉)Linux
- lsof命令簡介
- lsof命令詳解
- lsof命令小結
- linux每日命令(32):gzip命令Linux
- linux每日命令(29):chown命令Linux
- linux每日命令(18):whereis命令Linux
- linux每日命令(36):wc命令Linux
- linux每日命令(35):grep命令Linux
- linux每日命令(37):top命令Linux
- linux每日命令(5):mkdir命令Linux
- linux每日命令(2):cd命令Linux
- linux每日命令(10):touch命令Linux
- linux每日命令(12):nl命令Linux
- linux每日命令(11):cat命令Linux
- Linux lsof命令如何使用?其作用是什麼?Linux
- lsof命令功能說明
- linux每日命令(38):iostat命令LinuxiOS
- 每日Linux命令(5):lsblkLinux
- 每日Linux命令(2):vmstatLinux
- 每日Linux命令(3):sarLinux
- lsof命令學習筆記筆記
- 【系統】lsof命令簡介
- 【系統】lsof 命令簡介
- linux每日命令(20):find命令概覽Linux
- Linux lsof詳解Linux