
Under Linux, many objects are considered a file, regardless of whether the object is actually a file, device, directory, or socket. Listing a file is easy, there is the shell built-in ls for that. But what if a user wanted to see which files are currently opened by the web server process? Or if that user wanted to find out which files are opened in a certain directory? That's where lsof comes into play. Imagine lsof as a ls with the addition of "open files".
Please note that while the BSD's have a different utility for this job, fstat, several other flavors of Unix (Solaris, for example) also possess lsof. The options and flags are different on the other platforms, as well as the look of the output, but generally the knowledge in this article should be applicable for them too.
First, let's take a look at the format of lsof output and how it is to be read. The usual output of lsof without any parameters would resemble the following. This has been trimmed for readability.
These columns mean the following:
init.init, it's almost always root.cwd - The current working directory (you might notice the similarity to the pwd command which prints the current working directory).rtd - The root directory of a process.txt - A text file, this can either mean a configuration file related to the process or the "source code" related to (or belonging to) the process.mem - A so called "memory mapped file", that means a segment of virtual memory (read: RAM) that has been assigned to a file.r - Read.w - Write.u - Read and write.REG - A regular file.DIR - A directory.FIFO - First in, first out.This might be a little bit overwhelming for now, but if you work with lsof a few times, it will quickly sink into your brain.
As mentioned above, the output of lsof has been shortened here. Without any arguments or filters, lsof produces hundreds of lines of output which will only leave you confused.
There are two basic approaches to solve that problem:
lsof command line options to narrow down the results.grep.While the latter option may sound more comfortable since you won't have to memorize the lsof command line options, it's generally not as flexible and efficient, so we'll stick to the first one.
Let's imagine that you want to open a file with your favorite text editor, and that the text editor tells you that it can only be opened in read-only mode because another program is already accessing it. lsof will help you find out who the perpetrator is:
This will produce an output similar to this:
Apparently, you forgot to close and older session! A very similar problem happens when you try to unmount an NFS share and umount tells you it can't because something is still accessing the mounted folder. Again, lsof can help you with identifying the culprit:
Notice the trailing slash, that's important. Otherwise lsof will assume you mean a regular file. Don't be confused by the + in front of the flag - lsof has so many command line options that it needs + in addition to the more common -. The output would look like this:
That means that the process mocp, with the PID 5637, belonging to the user music has opened a file called RMS_GNU_SONG.ogg. However, even after closing that process, there is still a problem - the NFS volume can't be unmounted.
lsof has a -c flag that displays files opened an arbitrary process name.
That would produce an output looking like this:
In this example, there is another instance of mocp running, preventing you from unmounting the share. After shutting down that process, you want to make sure that the user music has no other potentially problematic files open. lsof has a -u flag for showing files opened by a specific user. Remember, a file isn't always just a regular file on your hard disk!
You can also pass several users, separated by commas:
An important note on the default behavior of lsof: the results are OR-based, which means that you will see file results opened by processes that are owned by either the user music, or the user moremusic. If you wanted to see results matching processes that are owned by both users, then you would have to pass the flag -a:
Since both of the users are in the group musicusers, then you can also list files based on group:
You can also combine command line flags:
In the last line, we added another special flag - ^, which stands for a logical NOT. If the output is empty after running that command, then the unmounting will most likely be successful.
In the previous examples, we mostly looked at regular files. How about sockets and network connections?
To list all current network connections lsof has the -i flag:
The output looks similar to what we've seen so far...
... except for one difference: instead of file names or directories, the column NAME now shows connection information. Each connection consists of the following parts:
As with many other tools, you may opt-out of resolving DNS names and ports (-n and -P, respectively). The flag -i takes additional parameters. You can specify whether or not to show tcp, udp or icmp connections or certain ports:
Again, parameters can be combined. The following example...
... will only show you TCP connections using port 80. You may also combine it with the options that you already know from "classic" files:
This will show you all TCP connections opened by the user httpd. Note the -a flag, which changes the default behavior of lsof (as mentioned earlier). As with most command-line tools, you can go extremely deep. The following will only show you TCP connections whose state is "ESTABLISHED":
At this point, you should have a basic understanding on how lsof works, along with some common use cases. For further reading, see the manpage of lsof on your system.
0 Comments
Be the first to comment and share your perspective with the community.