Fatmawati Ahmad Zaenuri / Shutterstock.com

If everything in Linux is a file, there must be more to it than just the files on your hard drive. This tutorial will show you how to use lsof to view all other devices and processes that are treated as files.

In Linux everything is a file

The oft-quoted phrase that everything in Linux is a file is kind of true. A file is a collection of bytes. When they are read in program or sent to the printer, they create byte stream. When they are recorded , they accept byte stream.

Many other system components accept or generate byte streams, such as keyboards, sockets, printers, and communication processes. Because they receive, generate, or receive and generate streams of bytes, these devices can be treated — at a very low level — as files.

This design concept simplified the implementation of the Unix operating system. This meant that a small set of handlers, tools, and APIs could be created to handle a wide variety of different resources.

The data and program files that reside on your hard drive are ordinary file system files. We can use the command ls to bring them out and find out some details about them.

How do we know about all other processes and devices that are treated as files? We use the command lsof . This is a list of open files on the system. That is, it lists everything that is processed as if it were a file.

CONNECTED: What does «All this is a file» mean in Linux?

lsof command

Many processes or devices that lsof can report whether they are owned by root or have been started by root, so you will need to use the command sudo With lsof .

And since this list is going to be very long, we’re going to show it less .

  Судо Лсоф |  Меньше 

lsof in terminal window

Before lsof output lsof GNOME users may see a warning message in the terminal window.

  lsof: ПРЕДУПРЕЖДЕНИЕ: невозможно stat () fuse.gvfsd-fuse файловая система / run / user / 1000 / gvfs
 Выходная информация может быть неполной. 

lsof tries to process all mounted filesystems. This is a warning message lsof because lsof discovered the GNOME Virtual File System (GVFS). This is a special case of the user-space (FUSE) file system. It acts as a bridge between GNOME, its API, and the core. No one, not even root, can access one of these filesystems other than the owner who mounted it (in this case, GNOME). You can ignore this warning.

Conclusion from lsof very wide. Leftmost columns:

leftmost columns of lsof output in terminal window

Far right columns:

rightmost columns of lsof output in terminal window

Columns of Lsof

All columns do not apply to every type of open file. It is normal for some of them to be empty.

  • Team : The name of the command associated with the process that opened the file.
  • PID : Identification number of the process that opened the file.
  • TID : Task (thread) Identification number. An empty column means that this is not a task; it’s a process.
  • User : the user ID or username that owns the process, or the user ID or username that owns a directory in /proc where lsof finds information about the process.
  • FD : shows the file descriptor of the file. File descriptors are described below.
  • Type of : The type of node associated with the file. Note types are described below.
  • Device : contains either comma-separated device numbers for a special character, special block, regular file, directory, or NFS, or a kernel reference address that identifies the file. The base address or device name of a Linux AX.25 socket device may also be displayed.
  • Size / Off .: Shows the file size or file offset in bytes.
  • Knot : Shows the node number of the local file or the node number of the NFS file on the server host or Internet protocol type. It may show the STR for the stream, IRQ, or the inode number of a device with a Linux AX.25 socket.
  • Name : Shows the name of the mount point and the file system where the file resides.

FD column

The file descriptor in the FD column can be one of many options; on the man page, list them all.

An entry in the FD column can consist of three parts: a file descriptor, a mode character, and a lock character. Some common file descriptors:

  • cwd : current working directory.
  • err : FD information error (see NAME column).
  • ltx : shared library text (code and data).
  • m86 : mapped DOS Merge file.
  • meme : memory-mapped file.
  • mmap : Memory mapped device.
  • pd : parent directory.
  • rtd : root directory.
  • TXT : program text (code and data)
  • A number representing the file descriptor.

The mode symbol can be one of the following:

  • r : Read access.
  • w : write access.
  • U : Read and write access.
  • ‘: Space if the mode is unknown and there is no lock character.
  • : The mode is unknown and there is a lock symbol.

The lock character can be one of:

  • r : read lock on part of file.
  • R : Read lock on the entire file.
  • w : write lock for part of the file.
  • W : Write lock on the whole file.
  • u : read and write lock of any length.
  • U : Unknown lock type.
  • ‘: Space character. No blocking

Column TYPE

More than 70 entries can be displayed in the TYPE column. Some common entries you will see:

  • REG : a normal file system file.
  • DIR : Handbook.
  • FIFO : first on first.
  • CHR : special character file.
  • BLK : lock special file.
  • INET : internet socket.
  • Unix : UNIX domain socket

See processes that have a file open

To see the processes that have opened a specific file, specify the file name as a parameter to lsof . For example, to see the processes that have a file open kern.log use this command:

  sudo lsof /var/log/kern.log 

sudo lsof /var/log/kern.log in terminal window

lsof responds by displaying one process rsyslogd which was launched by the user syslog .

output to terminal window

View all files opened from a directory

To see the files that were opened from a directory and the processes that opened them, pass the directory to lsof as a parameter. You must use the option +D (catalog).

To see all files that are open in a directory /var/log/ use this command:

  sudo lsof + D / var / log / 

sudo lsof + D /var/log/ in a terminal window

lsof responds with a list of all open files in that directory.

output to terminal window

To see all files that have been opened from a directory /home use the following command:

  sudo lsof + D / home 

sudo lsof + D /home in a terminal window

Files were opened from directory /home . Note that with shorter descriptions in some columns, the entire list is narrower.

exit to terminal window

List of files opened by the process

To see files that have been opened by a particular process, use the option -c (team). Please note that you can lsof more than one search term for lsof simultaneously.

  sudo lsof -c ssh -c init 

sudo lsof -c ssh -c init in terminal window

lsof provides a list of files that have been opened by any of the processes presented on the command line.

output to terminal window

See files opened by the user

To limit the display to files that have been opened by a specific user, use the option -u (user). In this example, we’ll look at files that have been opened by processes that are owned or run as Mary.

  судо лсоф -у мэри 

sudo lsof -y mary in a terminal window

All of the listed files were opened on behalf of the user Mary. This includes files that have been opened, for example, in the desktop environment, or simply as a result of Mary’s login.

output to terminal window

Excluding files opened by the user

To exclude files that have been opened by the user, use the statement ^ . Excluding users from the list makes it easier to find the information you are interested in. You must use the option -u as before and add the character ^ at the beginning of the username.

  sudo lsof + D / home -u ^ mary 

sudo lsof + D / home -u ^ mary in a terminal window

This time in the list for the directory /home there are no files that have been opened by the user Mary.

output to terminal window

List of files opened by the process

To get a list of files that have been opened by a particular process, use the parameter -p (process) and provide the process ID as a parameter.

  Судо Лсоф - р 4610 

sudo lsof -p 4610 in terminal window

All files that were opened by the process id you specified are listed for you.

output to terminal window

List of process IDs that have the file open

To see the IDs of the processes that have a particular file open, use the parameter -t (terse) and specify the filename on the command line.

  sudo lsof -t /usr/share/mime/mime.cache 

sudo lsof -t /usr/share/mime/mice.cache in a terminal window

Process IDs are displayed as a simple list.

output to terminal window

Use AND and OR Lookups

Let’s list the files that have been opened by user Mary that are associated with SSH processes. We know we can provide more than one search item on the command line, so this should be easy.

  судо лсоф -у мэри -с сш 

sudo lsof -u mary -c ssh in terminal window

Now let’s look at the output lsof . It doesn’t look right; there are entries in the output that were started by the root user.

output to terminal window

This is not what we expected. What happened?

If you provide multiple search terms, lsof will return any file that matches the first search term or second search query, and so on. In other words, it does an OR search.

To lsof performed an AND search, use the option -a (and). This means that only those files that match the first search term, the second search term, and so on will be listed.

Let’s try it again and use the option -a .

  судо лсоф -у мэри -с сш -а 

sudo lsof -u mary -c ssh -a in terminal window

Now each file in the list is a file that was opened by or as Mary and associated with the SSH command.

output to terminal window

Automatic display update

We can use the option +|-r (repeat) to translate lsof into repeat mode. The repeat option can be applied in two ways: +r or -r . We also need to add the number of seconds we want to lsof before updating the display.

Using the repeat option in any format causes lsof display results as usual, but adds a dotted line at the bottom of the screen. It waits for the number of seconds specified on the command line and then updates the display with the new result set.

With option -r this will continue until you press Ctrl+C. In the format +r it will continue until no results are displayed, or until you press Ctrl+C.

  sudo lsof -u mary -c ssh -a -r5 

sudo lsof -u mary -c ssh -a -r5 in terminal window

Notice the dotted line at the bottom of the list. This separates each new data display when the output is updated.

output to terminal window

Displaying Files Related to Internet Connections

Option -i (Internet) allows you to view files opened by processes associated with network and Internet connections.

  lsof -i 

lsof -i in a terminal window

All files opened on the network and Internet connections are displayed.

output to terminal window

Display files associated with Internet connections by process ID

To see files opened by Internet connections that are associated with a specific process ID, add the parameter -p and -a .

Here we are looking for files opened over the internet or network connection using process id 606.

  sudo lsof -i -a -p 606 

lsof -i in a terminal window

Displays all files opened with process ID 606 that are associated with the Internet or network connections

output to terminal window

Displaying files related to Internet connections and commands

We can use the option -c (command) to search for files opened by specific processes. To find files that have been opened over the Internet or network connections associated with a process ssh use the following command:

  lsof -i -a -c ssh 

lsof -i -a -c ssh in terminal window

All files opened due to ssh processes are listed in the output.

output to terminal window

Displaying files associated with Internet connections and ports

We can do it lsof a report on files that have been opened over the Internet or network connections on a specific port. For this we use the symbol : followed by the port number.

Here we ask lsof list files that have been opened by a network or internet connection on port 22.

  lsof -i: 22 

lsof -i: 22 in terminal window

All files listed have been opened by processes associated with port 22 (which is the default port for SSH connections).

output to terminal window

Displaying files related to Internet connections and protocols

We can ask lsof show files that have been opened by processes associated with network and internet connections that use a specific protocol. We can choose from TCP, UDP and SMTP. Let’s use the TCP protocol and see what we get.

  sudo lsof -i tcp 

sudo lsof -i tcp in terminal window

Only those files that are open by processes using the TCP protocol are listed.

output to terminal window

We’ve only scratched the surface

This is a good rationale in some common use cases lsof but it’s much more than that. How much longer can be judged by the fact that the length of the manual page is more than 2800 lines.

lsof can be used for more lsof layers of open files and pseudo files. We provided a map sketch; The atlas is in the reference page.

Похожие записи