您好,登录后才能下订单哦!
小编给大家分享一下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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。