What is open files in linux

How to Check Open Files in Linux

You may have come across the saying, “Everything is a file in Linux.” Although this is not entirely true, it does hold a set of truths to it.

In Linux and Unix-like systems, everything is like a file. That means the resources in the Unix system get assigned a file descriptor, including storage devices, network sockets, processes, etc.

A file descriptor is a unique number that identifies a file and other input/output devices. It describes resources and how the kernel accesses them. Think of it as a gateway to the Kernel abstraction hardware resources.

Unfortunately, the concept of file descriptors is beyond the scope of this tutorial; consider the link provided below to get started on learning more:

That means that Unix and Unix-like systems such as Linux use such files heavily. As a Linux power user, seeing the open files and the process and users using them is incredibly useful.

This tutorial will focus on ways to view the files open and which process or user is responsible.

Pre-Requisites

Before we begin, ensure that you have:

If you have these, let us get started:

LSOF Utility

Created by Victor A Abell, List open files, or lsof for short, is a command-line utility that allows us to view the open files and the processes or users who opened them.

The lsof utility is available in major Linux distributions; however, you may find it not installed and thus may need to install manually.

How to Install lsof on Debian/Ubuntu

To install it on Debian, use the command:

sudo apt-get install lsof -y

How to Install on REHL/CentOS

To install on REHL and CentOS, use the command:

How to Install on Arch

On Arch, call the package manager using the command:

How to Install on Fedora

On Fedora, use the command:

Once you have the lsof utility installed and updated, we can begin using it.

Basic lsof Usage

To use the lsof tool, enter the command:

Once you execute the above command, lsof will dump a lot of information as shown below:

Читайте также:  Установка драйверов virtualbox linux

The above output shows all the files opened by the processes. The output has various columns, each representing specific information about the file.

  • The COMMAND column – shows the name of the process that is using the file.
  • PID – shows the Process Identifier of the process using the file.
  • The TID – Shows the task ID (threads) of the process.
  • TASKCMD – Represent the name of the task command.
  • USER – The owner of the process.
  • FD – Shows the file descriptor number. This is how processes use the file; the options available in this column output include:
  • cwd – current working directory.
  • mem – memory-mapped file
  • pd – parent directory
  • jld – jail directory
  • ltx – shared library text
  • rtd – root directory.
  • txt – program code and data
  • tr – kernel trace file.
  • err – File descriptor information error
  • mmp – Memory-mapped device.
  • TYPE – Shows the type of node associated with the file, such as:
  • Unix – for Unix domain socket.
  • DIR – represents the directory
  • REG – representing the regular file
  • CHR – represents the special character file.
  • LINK – symbolic link file
  • BLK – Block special file
  • INET – Internet domain socket
  • FIFO – a named pipe (First In First Out file)
  • PIPE – for pipes
  • DEVICES – Shows the device numbers separated by commas in the order of special character file, block special, regular, directory, and NFS file.
  • SIZE/OFF – shows the size of the file pr file offset in bytes.
  • NODE – shows the node number of the local file, type for internet protocol type, etc.
  • NAME – shows the name of the mount point and fs on which the file is located.

Note: Please Refer to the lsof Manual for detailed information on the columns.

How to Show Processes that Opened a File

Lsof provides us with options that help us filter the output to show only the processes that opened a specific file.

For example, to see the file that opened the file /bin/bash, use the command as:

This will give you an output as shown below:

COMMAND PID USER FD TYPE DEVICE SIZE / OFF NODE NAME

ksmtuned 1025 root txt REG 253 , 0 1150704 428303 / usr / bin / bash

bash 2968 centos txt REG 253 , 0 1150704 428303 / usr / bin / bash

bash 3075 centos txt REG 253 , 0 1150704 428303 / usr / bin / bash

How Show files Opened by a Specific User

We can also filter the output to show the files opened by a specific user. We do this by using the -u flag followed by the username as:

This will give you an output as shown below:

How to Show Files Opened by a Specific Process

Suppose we want to view all the files opened by a specific process? For this, we can use the PID of the process to filter the output.

For example, the below command shows the files opened by bash.

This will give you only the files opened by systemd as shown:

How to Show Files Opened in a Directory

To get the files opened in a specific directory, we can pass the +D option followed by the directory path.

Читайте также:  Linux operatsion tizimi skachat

For example, list open files in the /etc directory.

Below is the output for this:

How to Show Network Connection

Since everything in Linux is a file, we can get the network files such as TCP files or connections.

This will give you the TCP connections in the system.

You can also filter by the specific port using the command shown below:

This will give you the output as shown below:

How to Continuously Show Files

Lsof provides us with a mode to loop the output every few seconds. This allows you to monitor the files opened by a process or user continuously.

This option, however, requires you to terminate the process manually.

For example, the command below continuously monitors the files opened on port 22:

As you can see, in the third loop, lsof catches the established connection to the server on SSH.

Conclusion

Lsof is an incredibly useful utility. It allows you to monitor for critical files as well as monitor users and processes opening files. This can be incredibly useful when troubleshooting or looking for malicious attempts to the system.

As shown in this tutorial, using various examples and methods, you can combine the functionality provided by the lsof tool for custom monitoring.

Thank you for reading and sharing! I hope you learned something new!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

Linux Interview Questions : Open Files / Open File Descriptors

An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file.

What is file descriptor?

A file descriptor is a data structure used by a program to get a handle on a file. The most commonly known are:

0 for standard in 1 for standard out 2 for standard error

Does the number of open files equal the number of open file descriptors?

There is a difference in the number of current open files and the number of current file descriptors/handlers. Even though a file is open, it might not have a file descriptor associated with it such as current working directories, memory mapped files and executable text files.

The ‘lsof’ command will provide the number of open files associated with a process. For Example, for the process with PID 4448:

# lsof | grep -w 2374 pickup 2374 postfix cwd DIR 202,1 4096 8945851 /var/spool/postfix pickup 2374 postfix rtd DIR 202,1 4096 128 / pickup 2374 postfix txt REG 202,1 285112 8945807 /usr/libexec/postfix/pickup pickup 2374 postfix mem REG 202,1 62184 463326 /usr/lib64/libnss_files-2.17.so pickup 2374 postfix mem REG 202,1 155744 758332 /usr/lib64/libselinux.so.1 pickup 2374 postfix mem REG 202,1 15688 125778 /usr/lib64/libkeyutils.so.1.5 pickup 2374 postfix mem REG 202,1 58728 127331 /usr/lib64/libkrb5support.so.0.1 pickup 2374 postfix mem REG 202,1 11384 37284 /usr/lib64/libfreebl3.so .
# lsof | grep -w 2374 | wc -l 51

There are total 51 open files associated with PID 2374 (postfix).

Читайте также:  Команды консоль линукс запустить

To determine the number of open file descriptors associated with a process use the following. For the same postfix process, 2374:

# ls -l /proc/2374/fd total 0 lrwx------. 1 root root 64 Nov 18 18:06 0 -> /dev/null lrwx------. 1 root root 64 Nov 18 18:06 1 -> /dev/null l-wx------. 1 root root 64 Nov 18 18:06 10 -> pipe:[28867] lrwx------. 1 root root 64 Nov 18 18:06 2 -> /dev/null lr-x------. 1 root root 64 Nov 18 18:06 3 -> pipe:[18701] l-wx------. 1 root root 64 Nov 18 18:06 4 -> pipe:[18701] lrwx------. 1 root root 64 Nov 18 18:06 5 -> socket:[18631] lrwx------. 1 root root 64 Nov 18 18:06 6 -> socket:[18629] lrwx------. 1 root root 64 Nov 18 18:06 7 -> socket:[28851] lrwx------. 1 root root 64 Nov 18 18:06 8 -> anon_inode:[eventpoll] lr-x------. 1 root root 64 Nov 18 18:06 9 -> pipe:[28867] l-wx------. 1 root root 64 Nov 18 18:06 92 -> pipe:[18702]

There are 13 file descriptors associated with PID 2374 (postfix). We can see there is a difference between the number of open files and the number of file descriptors per process. postfix has 51 open files but only 13 file descriptors associated with it. Some of the open files do not have file descriptors associated with them; e.g., libraries, current working directories.

How to get the maximum number of file descriptors?

In Linux, maximum number of file descriptors limit can be read from /proc file system. To get the current limit on the number of file descriptors for the whole system use the following command.

# cat /proc/sys/fs/file-max 180451

How to compute the number of file descriptors currently being used?

Use the following command.

# cat /proc/sys/fs/file-nr 1344 0 180451 | | |_ Max no. of file descriptors allowed on the system | | | |__ Total free allocated file descriptors | |__ Total allocated file descriptors

To compute the number of file descriptors currently being used:

How to get the per-process limits on the file descriptors?

Use ‘ulimit -a‘ to get the per-process limits.

# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 7132 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open files (-n) 4096 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 8192 cpu time (seconds, -t) unlimited max user processes (-u) 7132 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited

How does one find the number of open file descriptors being used by any given process?

Use the following command (Substitute your PID)

What are some other useful parameters?

NR_OPEN = Maximum number of open files per process NR_FILE = Total number of files that can be open in the system at any time FILE-MAX = Kernel parameter refers to maximum number of file descriptors allowed per system FILE-NR = Refers to the number of current file descriptors used at any moment. LSOF = Gives the number of open files.

Источник

Оцените статью
Adblock
detector