How to use the lsof command to troubleshoot Linux (2024)

Posted: December 9, 2022 | | by Giancarlo del Rossi (Red Hat)

Image

How to use the lsof command to troubleshoot Linux (1)

One of the things I appreciate most about Linux is the chance to explore my operating system. Nothing is off limits in Linux. That's a huge opportunity—a very comfortable benefit—for anyone using Linux daily, whether you're a sysadmin, developer, network admin, or hobbyist eager to learn more.

It has real-world ramifications, too, because sometimes you really do need to probe into the inner workings of a system to understand a problem. Every sysadmin knows the feeling of getting a vague message like "The server is slow" or "This application isn't responding." That lucky sysadmin gets to perform a "treasure hunt" to find the problem. However, Linux provides all the tools you need.

[Cheat sheet:Old Linux commands and their modern replacements]

The lsof command, in combination with other tools like top or ps, can be very helpful when diagnosing a potential bottleneck or similar issue.

List open files

The lsof command is an acronym for "list open files," but its potential isn't limited to just that role. It's commonly said that in Linux, everything is a file. In many ways, that's true, so a utility that lists open files is actually pretty useful. The lsof utility is a robust interface for the information inside the /proc virtual filesystem.

For example, suppose you need to know what process is using a particular directory:

$ lsof /runCOMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEsystemd 1 root 31u FIFO 0,24 0t0 19314 /run/initctlsystemd 1 root 38u FIFO 0,24 0t0 19331 /run/dmeventd-serversystemd 1 root 39u FIFO 0,24 0t0 19332 /run/dmeventd-clientsystemd-j 713 root mem REG 0,24 8388608 12871 /run/log/journal/aaff550ef54147ddb4d1776d86b04055/system.journalNetworkMa 881 root 20w FIFO 0,24 0t0 24896 /run/systemd/inhibit/1.refrhsmcertd 906 root 3wW REG 0,24 0 22479 /run/lock/subsys/rhsmcertdcrond 910 root 3uW REG 0,24 4 24002 /run/crond.pidrsyslogd 1224 root mem REG 0,24 8388608 12871 /run/log/journal/aaff550ef54147ddb4d1776d86b04055/system.journalrsyslogd 1224 root 10r REG 0,24 8388608 12871 /run/log/journal/aaff550ef54147ddb4d1776d86b04055/system.journalsshd 1651 root 11w FIFO 0,24 0t0 27390 /run/systemd/sessions/1.refsshd 1666 admin 11w FIFO 0,24 0t0 27390 /run/systemd/sessions/1.ref

Or maybe you need the exact opposite. Perhaps you need to know what files a particular process has open. This example looks at sshd:

$ lsof -p 890CMD PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEsshd 890 root cwd DIR 253,0 224 128 /sshd 890 root rtd DIR 253,0 224 128 /sshd 890 root txt REG 253,0 886344 646480 /usr/sbin/sshdsshd 890 root mem REG 253,0 6940392 34389337 /var/lib/sss/mc/groupsshd 890 root mem REG 253,0 9253600 34389336 /var/lib/sss/mc/passwdsshd 890 root mem REG 253,0 46312 570898 ...libnss_sss.sosshd 890 root mem REG 253,0 144392 1847 ...libgpg-error.so[...]sshd 890 root 0r CHR 1,3 0t0 1027 /dev/nullsshd 890 root 1u unix 0xff... 0t0 22395 type=STREAMsshd 890 root 2u unix 0xff... 0t0 22395 type=STREAMsshd 890 root 4u unix 0xff... 0t0 24654 type=STREAMsshd 890 root 5u IPv4 24658 0t0 TCP *:ssh (LISTEN)sshd 890 root 6r REG 253,0 6940392 34389337 /var/lib/sss/mc/groupsshd 890 root 7u IPv6 24666 0t0 TCP *:ssh (LISTEN)

Skip to the bottom of list

Image

Download now

Here's an explanation of each column:

  • COMMAND: The command name
  • PID: Process ID (PID) of the process
  • USER: Owner of the process
  • FD: File descriptor definition
  • TYPE: Type of file descriptor
  • DEVICE: Device number or, in the case of a block device, characteror other
  • SIZE/OFF: Dimension of the file or offset (the suffix 0tis the offset)
  • NODE: Node description of the local file; this could be the number of the local file, TCP, UDP, or STR (stream)
  • NAME: The name of the mount point where the file resides

Open files

You can also discover what files a particular user has open:

$ lsof -u admin

This information is especially useful when you can't unmount an external device because a file on it is being actively accessed.

[ Get the guide to installing applications on Linux. ]

Network debugging

As I've said, everything on Linux is a file, so lsof isn't limited to the local filesystem. You can also use it for network debugging.

For example, suppose you need to know what process uses a particular TCP port (like 22, for example):

$ lsof -i TCP:22COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEsshd 890 root 5u IPv4 24658 0t0 TCP *:ssh (LISTEN)sshd 890 root 7u IPv6 24666 0t0 TCP *:ssh (LISTEN)

You can also obtain a report on all network processes associated with a particular interface:

$ lsof -i TCP@127.0.0.1COMMAND PID USER FD TYPE DEVICE SZ/OFF NODE NAMEcupsd 4408 root 8u IPv4 32331 0t0 TCP ...ipp (LISTEN)qemu-s 13462 qemu 11u IPv4 98536 0t0 TCP ...rfb (LISTEN)

[ For more tips on Linux administration, readHow to analyze a Linux process' memory map with pmap. ]

Great potential

Despite its seemingly humble purpose, the lsof tool is a powerful tool on a system that can treat almost anything as a file. Read the lsof man page to get more ideas of how this simple utility can help you perform some complex tasks.

Topics: Command line utilities Troubleshooting Linux administration

How to use the lsof command to troubleshoot Linux (2024)

FAQs

How to use lsof command in Linux? ›

It can be used with the syntax, lsof [option] [action or filepath] . In this example, we use the 'lsof' command followed by the path to a file. This command lists all processes that have opened the specified file.

How do I check troubleshooting in Linux? ›

You can diagnose a non-booting Linux system by employing these steps:
  1. Check the boot loader. Start by ensuring the boot loader (such as GRUB) is properly configured.
  2. Access recovery mode. ...
  3. Examine the log files. ...
  4. Inspect the kernel messages. ...
  5. Test the hardware. ...
  6. Perform a file system check. ...
  7. Reinstall packages.

What is the purpose of lsof? ›

lsof : This is the command itself, standing for “List Open Files.” It is the primary command used to gather information about open files and processes on a Linux system. [options] : This part of the syntax refers to the various options or flags that can be added to the lsof command to customize its behavior.

How to check open files in Linux without lsof? ›

Still, you need to be root. ls -l /proc/*/fd to see all open files like lsof does by default.

How to check ports in Linux using lsof command? ›

Check Open Ports Using lsof (List Open Files): Open Terminal: Launch the terminal on your Linux system. Run lsof Command: Execute the following command to list open ports:cssCopy codesudo lsof -i -P -n | grep LISTEN This command provides a detailed list of listening ports.

How to check running process in Linux? ›

The 'ps' command displays information about running processes. To list all running processes, you can use the following command: ps -A or ps -e Both commands will provide you with a list of currently running processes along with their relevant details, such as process ID (PID), terminal, CPU usage, and more.

How do I troubleshoot performance issues in Linux? ›

The dashboard
  1. Uptime and average load. The first line in the dashboard gives us the time, how long the system has been running, the number of users logged in, and the average load on the CPU. ...
  2. Tasks. The second line gives us the number of tasks currently running. ...
  3. CPU. The third line is all about CPU. ...
  4. Memory. ...
  5. Swap memory.

How to run diagnostics on Linux? ›

Linux
  1. Ensure you have the Diagnostics CLI: From the command line, change the directory to your application's root directory and ensure that the nrdiag. ...
  2. Unzip nrdiag. zip if necessary.
  3. From the nrdiag_1.2.3/linux directory, move nrdiag into the application's root directory.
  4. Run nrdiag (along with any CLI options.

Which command is used for verifying and troubleshooting problems? ›

The below mentioned commands are some of the most useful commands required to troubleshoot network problems and configure network settings.
  • IPCONFIG. ...
  • NSLOOKUP. ...
  • HOSTNAME. ...
  • PING. ...
  • TRACERT. ...
  • NETSTAT. ...
  • ARP(Address Resolution Protocol) ...
  • SYSTEMINFO.
Aug 13, 2024

What is the other command for lsof? ›

If all you want is to find the process ID(s) that have a particular file open, then you can use fuser on any POSIX-compliant system. On operating systems with a /proc directory, you can query the files open by a process (the reverse from lsof 's most common mode of operation) through information in /proc .

What is the difference between lsof and SS in Linux? ›

lsof also displays the names but it has done the matching internally using the number(s); it can similarly be given -n to display the numbers instead. Also note ss -l only shows sockets in LISTEN state, so you will never find ESTABLISHED connections that way, if that is what you want.

What is the lsof package in Linux? ›

What is Linux lsof? To monitor and analyze your Linux system, lsof is an easy-to-use tool. The program, developed and published in 1994 by Vic Abell, is open source and is part of the standard installation for numerous Linux distributions, such as Debian or Ubuntu. lsof stands for “List open files”.

How to check lsof? ›

Using the lsof -p <processID> command, the files opened by a particular process can be checked. With that, we can check if we have a more accurate number of open files for that process.

What is the lsof +L1 command? ›

The "+L1" option instructs lsof to display files with a size greater than or equal to 1 byte. This ensures that we only get relevant results. The output of this command will list all the open files on the system. However, we are only interested in files associated with disk space usage.

Do you need sudo for lsof? ›

The lsof Command

Many of the processes or devices that lsof can report on belong to root or were launched by root, so you will need to use the sudo command with lsof . And because this listing will be very long, we are going to pipe it through less, too.

How to track a file using Linux command? ›

Syntax of Find Command in Linux :
  1. path: Starting directory for the search. Example: find /path/to/search.
  2. options: Additional settings or conditions for the search. Example: find /path/to/search -type f -name "*.txt"
  3. expression: Criteria for filtering and locating files. Example: find /path/to/search -type d -name "docs"
Jul 5, 2024

How to use Jstat command in Linux? ›

How to launch jstat?
  1. -gc: garbage collection related statistics will be printed.
  2. -t timestamp since JVM was started will be printed.
  3. 11656: target JVM process Id.
  4. 10000: statistics will be printed every 10,000 milliseconds (i.e. 10 seconds).
  5. 30: statistics will be printed for 30 iterations.
Nov 18, 2019

How to check file permissions in Linux using command? ›

Type the command ls -l to list the files and directories with file permissions for your current location. The first character denotes whether an item is a file or a directory. If 'd' is shown, it's a directory, and if '-' is shown, it's a file.

How to check current open files in Linux? ›

Using lsof

The lsof command lists information about open files for running processes on the system. When we run lsof without any arguments, it displays the open files of every process running on the system.

References

Top Articles
fort myers cars & trucks "cars and trucks" - craigslist
Used Trucks for Sale Near Me in Fort Myers, FL - Autotrader
Byrn Funeral Home Mayfield Kentucky Obituaries
Puretalkusa.com/Amac
craigslist: south coast jobs, apartments, for sale, services, community, and events
Craigslist - Pets for Sale or Adoption in Zeeland, MI
Moe Gangat Age
Skylar Vox Bra Size
7440 Dean Martin Dr Suite 204 Directions
Alejos Hut Henderson Tx
Dit is hoe de 130 nieuwe dubbele -deckers -treinen voor het land eruit zien
Conan Exiles Thrall Master Build: Best Attributes, Armor, Skills, More
National Office Liquidators Llc
Dr. med. Uta Krieg-Oehme - Lesen Sie Erfahrungsberichte und vereinbaren Sie einen Termin
Echat Fr Review Pc Retailer In Qatar Prestige Pc Providers – Alpha Marine Group
Las 12 mejores subastas de carros en Los Ángeles, California - Gossip Vehiculos
Today Was A Good Day With Lyrics
Wics News Springfield Il
Jermiyah Pryear
Milwaukee Nickname Crossword Clue
Craigslist Rome Ny
Black Panther 2 Showtimes Near Epic Theatres Of Palm Coast
Unity Webgl Car Tag
Craigslist Comes Clean: No More 'Adult Services,' Ever
2004 Honda Odyssey Firing Order
O'reilly's In Monroe Georgia
Pioneer Library Overdrive
Courtney Roberson Rob Dyrdek
Otis Inmate Locator
UPC Code Lookup: Free UPC Code Lookup With Major Retailers
Roadtoutopiasweepstakes.con
Sun Haven Pufferfish
The Wichita Beacon from Wichita, Kansas
Texters Wish You Were Here
Scanning the Airwaves
KM to M (Kilometer to Meter) Converter, 1 km is 1000 m
Elizaveta Viktorovna Bout
Busted Newspaper Campbell County KY Arrests
World Social Protection Report 2024-26: Universal social protection for climate action and a just transition
Subdomain Finder
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
Promo Code Blackout Bingo 2023
Expendables 4 Showtimes Near Malco Tupelo Commons Cinema Grill
4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
About Us
Darkglass Electronics The Exponent 500 Test
Greg Steube Height
The Quiet Girl Showtimes Near Landmark Plaza Frontenac
Craigslist Anc Ak
Bones And All Showtimes Near Emagine Canton
Asisn Massage Near Me
Die 10 wichtigsten Sehenswürdigkeiten in NYC, die Sie kennen sollten
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 6134

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.