参考
http://www.thegeekstuff.com/2012/08/lsof-command-examples/
list open file
使用lsof,可以查看到任何打开的文件的信息。
linux中一切可以看做文件,普通文件,管道,网络文件(例如socket),设备等都是文件。
1.lsof
# lsof
列出所有活动进程打开的文件。
解释第一行的几个值:
FD:file descriptor 文件描述符
- cwd – Current Working Directory
- txt – Text file
- mem – Memory mapped file
- mmap – Memory mapped device
- NUMBER – Represent the actual file descriptor. The character after the number i.e ‘1u’, represents the mode in which the file is opened. r for read, w for write, u for read and write.
TYPE, Some of the values of TYPEs are
- REG – Regular File
- DIR – Directory
- FIFO – First In First Out(先进先出是队列)
- CHR – Character special file
DEVICE:指定磁盘名称
NODE: 索引节点,文件在磁盘上的标识
2.lsof 文件名
列出打开了指定文件的进程
# lsof /var/log/syslog
3.lsof -p 进程pid
列出被指定的进程所打开的文件
# lsof -p pid
4. lsof -u username
显示指定用户下打开的文件
5. lsof -i
这个命令和网络相关,显示所有的网络连接
按条件过滤的
- lsof -i tcp #查看协议tcp的连接
- lsof -i udp #udp协议的连接
- lsof -i4 # ipv4
- lsof -i6 # ipv6
- lsof -i -n # 不把ip转化为hostname
- lsof -i :22 # 查看22号端口被哪个进程占用,监听