SHARE
Facebook X Pinterest WhatsApp

Using lsof to Find Open Files

Written By
thumbnail Joe Brockmeier
Joe Brockmeier
Nov 23, 2010
ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More



More on open source servers

One of the most useful utilities you’ll find on Linux and *nix systems is lsof. Short for “list open files,” the lsof utility can help identify which files are being used by any given application, which network ports are open, and much more.

lsof is one of the most useful utilities for Linux and Unix systems. At its most basic, lsof can help identify which files are being used by any given application and which network ports are open.

The lsof utility has all manner of useful applications. My first serious application of lsof was hunting down rootkits and IRC bots that were deployed on shared Linux servers. A process would show up in top or ps aux, but the executable didn’t seem to exist. Using lsof, I could hunt down the scripts or executables used to run the malware.

You can do a lot with lsof, but let’s focus on a couple of basics for starters. If you run just lsof, it will attempt to show all files (which includes network sockets, pipes and special files) that are open. That’s a lot to digest, and it’s probably more than you need. Let’s look at narrowing that down. For example, if you want to see all the open files owned by a process, you can use the -p option (for PID) like so:

lsof -p XXXXX

Here, you want to replace XXXXX with the process ID (PID) of the process you want to see. Note that you want to run this as root or using sudo. The output will show the command that has the file open, the PID, the user, the file descriptor, type, size of the file and the name of the file.

You can also see what files are open by users. Running lsof -u user will show all open files by processes owned by the user. You can also substitute the user ID (UID) for the username. If you want to eliminate a user from the listing, use ^user instead. The preceding caret will negate the selection, so the user will be ignored.

If you want to see what files are open over the network, use -i.
This will show you which files and sockets are open, and their respective protocols,
hostnames and so on. You can narrow network parameters down by IP version (-i4 for
IPv4, -i6 for IPv6), protocol (UDP or TCP), and even hostname
or port. By default, lsof will look up hostnames — but you can
turn this off using the -n option. It will run faster without
needing to do name lookups.

You can also “and” things using the -a option. Want to see what network sockets are owned by a particular user or process? Try lsof -u user -a -i. That will show only the open TCP and UDP sockets.

Just the options given here can be fairly useful in day-to-day administration. But be sure to check the lsof man page for more options and operating system specific options.

Joe ‘Zonker’
Brockmeier
is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZDNet, and many other publications. You can reach Zonker at jzb@zonker.net and follow him on Twitter.

Follow ServerWatch on Twitter

thumbnail Joe Brockmeier

Joe Brockmeier is the editorial director of the Red Hat Blog. He joined Red Hat in 2013 as part of the Open Source and Standards (OSAS) group, now the Open Source Program Office (OSPO). Prior to Red Hat, Brockmeier worked for Citrix on the Apache OpenStack project, and was the first OpenSUSE community manager for Novell between 2008-2010. Brockmeier also has an extensive history in the tech press and publishing, having been editor-in-chief of Linux Magazine, editorial director of Linux.com, and a contributor to LWN.net, ZDNet, UnixReview.com, and many others.

Recommended for you...

What Is a Container? Understanding Containerization
What Is a Print Server? | How It Works and What It Does
Nisar Ahmad
Dec 8, 2023
6 Best Linux Virtualization Software for 2024
What Is a Network Policy Server (NPS)? | Essential Guide
ServerWatch Logo

ServerWatch is a top resource on servers. Explore the latest news, reviews and guides for server administrators now.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.