How to List Open Files on Ubuntu

Listing Open Files on Ubuntu using lsof

Overview

In this tutorial, you will learn how to list open files on your Ubuntu servers using the lsof command.

The are a number of scenarios where you, a system administrator, will need to know what files are in use by your users or services. It could be to discover which files are open on a volume that needs to be unmounted for maintenance, or to ensure a service is running correctly, for example.

List All Open Files

In the first example, a listing all of open files by all users will be generated with the lsof command.

lsof /dev/null

The output will look similar to the following.

COMMAND     PID   TID             USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
 systemd       1                   root  cwd       DIR              202,1     4096          2 /
 systemd       1                   root  rtd       DIR              202,1     4096          2 /
 systemd       1                   root  txt       REG              202,1  1595792      19540 /lib/systemd/systemd
 systemd       1                   root  mem       REG              202,1  1700792       2083 /lib/x86_64-linux-gnu/libm-2.27.so
 systemd       1                   root  mem       REG              202,1   121016      16613 /lib/x86_64-linux-gnu/libudev.so.1.6.9
 systemd       1                   root  mem       REG              202,1    84032       2068 /lib/x86_64-linux-gnu/libgpg-error.so.0.22.0
 systemd       1                   root  mem       REG              202,1    43304       2212 /lib/x86_64-linux-gnu/libjson-c.so.3.0.1
 systemd       1                   root  mem       REG              202,1    34872       6169 /usr/lib/x86_64-linux-gnu/libargon2.so.0
 systemd       1                   root  mem       REG              202,1   432640       2208 /lib/x86_64-linux-gnu/libdevmapper.so.1.02.1

[ truncated results ]

Listing Files Opened by User

Another example is listing files opened only by a specific user. The -u flag is used with the lsof command to filter the list for a specified user. For example, here is an example of the command for a user named www-data.

lsof -u www-data

The user is for an Apache2 web server, and the resulting list would look similar to the following. The results have been truncated, as the listing can get quite long.

COMMAND     PID   TID             USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
 systemd       1                   root  cwd       DIR              202,1     4096          2 /
 systemd       1                   root  rtd       DIR              202,1     4096          2 /
 systemd       1                   root  txt       REG              202,1  1595792      19540 /lib/systemd/systemd
 systemd       1                   root  mem       REG              202,1  1700792       2083 /lib/x86_64-linux-gnu/libm-2.27.so
 systemd       1                   root  mem       REG              202,1   121016      16613 /lib/x86_64-linux-gnu/libudev.so.1.6.9
 systemd       1                   root  mem       REG              202,1    84032       2068 /lib/x86_64-linux-gnu/libgpg-error.so.0.22.0
 systemd       1                   root  mem       REG              202,1    43304       2212 /lib/x86_64-linux-gnu/libjson-c.so.3.0.1
 systemd       1                   root  mem       REG              202,1    34872       6169 /usr/lib/x86_64-linux-gnu/libargon2.so.0
 systemd       1                   root  mem       REG              202,1   432640       2208 /lib/x86_64-linux-gnu/libdevmapper.so.1.02.1