How to Use ‘lsof’ Command to Check Open Files in Linux
This is our ongoing series ofLinux commandsand in this article, we are going to reviewlsofcommand with practical examples.lsofmeaning‘LiSt Open Files’is used to find out which files are open by whichLinux process.
As we all know Linux/Unix considers everything as a file (pipes, sockets, directories, devices, etc). One of the reasons to use the lsof command is when a disk cannot be unmounted as it says the files are being used. With the help of lsof command, we can easily identify the files which are in use.
10 Linux lsof Command Examples
Table of Contents
1. List All Open Files with lsof Command
In the below example, it will show a long listing of open files some of them are extracted for better understanding which displays the columns like Command, PID, USER, FD, TYPE, etc.
Sections and their values are self-explanatory. However, we’ll review FD & TYPE columns more precisely.
FD – stands for a File descriptor and may see some of the values as:
cwd current working directory
rtd root directory
txt program text (code and data)
mem memory-mapped file
Also in FD column numbers like 1u is actual file descriptor and followed by u,r,w of its mode as:
r for read access.
w for write access.
u for read and write access.
TYPE – of files and it’s identification.
DIR – Directory
REG – Regular file
CHR – Character special file.
FIFO – First In First Out
2. List User Specific Opened Files
The below command will display the list of all opened files of user tecmint.
# lsof -u tecmint
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 1838 tecmint cwd DIR 253,0 4096 2 /
sshd 1838 tecmint rtd DIR 253,0 4096 2 /
sshd 1838 tecmint txt REG 253,0 532336 188129 /usr/sbin/sshd
sshd 1838 tecmint mem REG 253,0 19784 190237 /lib/libdl-2.12.so
sshd 1838 tecmint mem REG 253,0 122436 190247 /lib/libselinux.so.1
sshd 1838 tecmint mem REG 253,0 255968 190256 /lib/libgssapi_krb5.so.2.2
sshd 1838 tecmint mem REG 253,0 874580 190255 /lib/libkrb5.so.3.3
3. Find Processes Running on Specific Port
To find out all the running Linux processes of a specific port, just use the following command with option -i. The below example will list all the running processes of port 22.
The below example only shows whose PID is 1 [One].
# lsof -p 1
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 253,0 4096 2 /
init 1 root rtd DIR 253,0 4096 2 /
init 1 root txt REG 253,0 145180 147164 /sbin/init
init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so
init 1 root mem REG 253,0 142472 189970 /lib/ld-2.12.so
10. Kill all Activity of Particular User
Sometimes you may have to kill all the processes for a specific user. The below command will kill all the processes of the tecmint user.
# kill -9 `lsof -t -u tecmint`
Note: Here, it’s not possible to give examples of all available options, this guide is only to show how lsof command can be used. You may refer man page of lsof command to know more about it. Please share it if you find this article is useful through our comment box below.
No comments:
Post a Comment