程式扎記: [Linux 文章收集] 10 lsof Command Examples in Linux

標籤

2015年9月24日 星期四

[Linux 文章收集] 10 lsof Command Examples in Linux

Source From Here 
Preface 
This is our on-going series of Linux commands and in this article we are going to review lsof command with practical examples. lsof meaning ‘LiSOpen Files’ is used to find out which files are open by which process. As we all know Linux/Unix considers everything as a files (pipes, sockets, directories, devices etc).One of the reason to use lsof command is when a disk cannot be unmounted as it says the files are being used. With the help of this command we can easily identify the files which are in use. 

1. List all Open Files with lsof Command 
In the below example, it will show 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 it’s values are self-explanatory. However, we’ll review FD & TYPE columns more precisely. FD – stands for File descriptor and may seen 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 it’s 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

















3. Find Processes running on Specific Port 
To find out all the running process of specific port, just use the following command with option -i. The below example will list all running process of port 22. 










4. List Only IPv4 & IPv6 Open Files 
In below example shows only IPv4 and IPv6 network files open with separate commands. 

























5. List Open Files of TCP Port ranges 1-1024 
To list all the running process of open files of TCP Port ranges from 1-1024. 
 

6. Exclude User with ‘^’ Character 
Here, we have excluded root user. You can exclude a particular user using ‘^’ with command as shown above. 
 

7. Find Out who’s Looking What Files and Commands? 
Below example shows user tecmint is using command like ping and /etc directory . 
 

8. List all Network Connections 
The following command with option ‘-i’ shows the list of all network connections ‘LISTENING & ESTABLISHED’. 
 

9. Search by PID 
The below example only shows whose PID is 1. 
 

10. Kill all Activity of Particular User 
Sometimes you may have to kill all the processes for a specific user. Below command will kills all the processes of tecmint user. 

// -t: specifies that lsof should produce terse output with process identifiers only and no header - e.g., so that the output may be
// piped to kill(1).

# kill -9 `lsof -t -u tecmint`


Supplement 
15 Linux lsof Command Examples (Identify Open Files)

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!