Troubleshooting with lsof

Lsof is a standard Linux utility that can help you troubleshoot a range of issues related to open files. If you run it without any options it will display ALL the open files in the system, which might be a bit overwhelming.

Fortunately you can use the -p <pid> option to see all the files opened by a specific process. Another useful way to call lsof is by passing in a file name as argument, this way lsof will show you the process that has this file open.

Using lsof, an example

Have you ever been in a situation were you couldn’t do something because you got a “Resource temporarily unavailable” error or something like that? Well this is the kind of thing lsof can help you with. Let’s see an example:

locked
This is an attempt to update our system with apt. It fails because another program is using apt, let’s find out who is the culprit:

lsof

Looks like aptitude is the culprit! But maybe we could get a bit more info, what is running this? We can check the parent id (ppid) of aptitude using ps.

ppid

We run ps twice, one to see the ppid and another to see the name of the process, we can also run ps with the -f flag to see the arguments.

Open ports

You can also use lsof to find open ports in your system. For example using lsof -i:80 we can find out what is listening in this port:

lsof port 80

At this point we should have the information we wanted. Have fun and good luck on your troubleshooting adventures!

2 Responses to “Troubleshooting with lsof”

  1. I actually just used this today for the first time at work. Works like a charm!

Leave a Reply