您好,登錄后才能下訂單哦!
小編給大家分享一下Linux中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="http://www.rsyslog.com"] 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
對于許多應用程序,尤其是日志文件和數據庫,這種恢復刪除文件的方法非常有用。
以上是“Linux中lsof命令怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。