File used by another process? Play Lsof!

On my latest Linux lesson I have played as teacher, someone asked me about the use of Lsof and how to take advantage of it.

As Man say, Lsof is a command line utility that lists information about files opened by processes, were an open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file (Internet socket, NFS file or UNIX domain socket). A specific file or all the files in a file system may be selected by path.

Do not forget everything is a file on Linux ( pipes, sockets, directories, devices, etc.)!!

LSOF OUTPUT SINTAX

Running Lsoft without parameters, show a list of all open files belonging to all active processes

Output sintax is pretty easy and list, from left to right, the command running, the process identifier, the process running user, a few field explained below and, finally, the file used.

FD column stands for File descriptor and could be:

  • cwd current working directory
  • rtd root directory
  • txt program text (code and data)
  • mem memory-mapped file

Or could be a combo char\numbers like 1u as a 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 column stand for files type, as name suggest, and could be:

  • DIR – Directory
  • REG – Regular file
  • CHR – Character special file.
  • FIFO – First In First Out

USING LSOF

You can simply list processes which opened a specific file, by providing the file name as arguments of lsof command.

Above example, lists file /var/log/messages used by rsyslogd process (PID 1469).

You can also list all the files opened by a specific process using ‘-p’ option and the process ID. It will be helpful some times to get more information about a specific process:

Another way to do that using the process name:

You may also need to know which files are opened by a specific user:

LSOF FOR SYSADMIN

The above examples are usually helpful for both standard users and sysadmin, but as a sysadmin you also need to know specifically command in order to work with mount-point, NFS and network socket.

To know which files are open in a specific mount-point you can pass it as argument to Lsof:

where /app is actually a mount-point. This command works same with a simply folders.

Using option -N lists all files opened from a NFS share.

that works better in a combo with -u option (stand for user).

Instead of netstat, it is possible to use Lsof for listing all process belong a specific port\socket:

or binded on a specific hostname:

When you work with socket or NFS file, for example, repeat mode is nice option to use.
You have to specify -r option followed by delay time.

this run Lsof every 1 second. To interrupt it, press “Ctrl+c”.

Finally. if you need to use a Lsof in conjunction with other command, you have to terse output with option -t to suppress error message and other info.

Lsof it’s an incredible and powerful tool that will quickly help you in day work. By using Man, you could get a lot of other options and you will find your better trick! I hope you enjoyed this article!

Mattia