Tips on how to Use the ‘lsof’ Command in Linux

by | Sep 8, 2023 | Etcetera | 0 comments

The lsof command, standing for “Tick list Open Files,” is an excellent tool inside the Linux working device that provides an important information about files which might be opened by way of quite a lot of processes. This versatile command allows device admins to watch and prepare files, sockets, and neighborhood connections, offering a whole view of the way in which property are being implemented throughout the device.

Similar to directions like netstat and fuser, the lsof command is used to identify the files which might be being accessed by way of different processes, making it an indispensable tool for troubleshooting and device repairs. Whether or not or no longer you’re a neighborhood administrator on the lookout for to unravel conflicts or a developer having a look to optimize helpful useful resource allocation, the lsof command, often used alongside directions like grep, is generally a handy addition on your toolkit.

Tips about how you can Arrange the lsof Command

In a lot of Linux distributions, the lsof command comes pre-installed. Alternatively, if it’s not available on your device, you’ll merely arrange it using the package deal manager explicit on your distribution.

See also  The best way to Write a Listicle [+ Examples and Ideas]

Debian-based Systems (e.g., Ubuntu)

Arrange:

sudo apt-get substitute
sudo apt-get arrange lsof

Uninstall:

sudo apt-get remove lsof

Purple Hat-based Systems (e.g., CentOS, RHEL)

Arrange using Yum (older diversifications):

sudo yum arrange lsof

Arrange using DNF (more moderen diversifications):

sudo dnf arrange lsof

Uninstall using Yum:

sudo yum remove lsof

Uninstall using DNF:

sudo dnf remove lsof

Fedora

Arrange (older diversifications using Yum):

sudo yum arrange lsof

Arrange (more moderen diversifications using DNF):

sudo dnf arrange lsof

Uninstall using Yum:

sudo yum remove lsof

Uninstall using DNF:

sudo dnf remove lsof

Tips about how you can Use lsof

1. Tick list All Open Files

Syntax: lsof

Rationalization: Lists all open files on the device.

Example: lsof

Output:

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
systemd    1 root  cwd    DIR  253,1     4096      2 /
bash    1234 client  txt    REG  253,1   111350 123456 /bin/bash

This output shows all the open files, at the side of the command determine, process ID (PID), client, record descriptor (FD), type, tool, size, and the record’s path.

2. Tick list Files Opened by way of a Explicit Individual

Syntax: lsof -u <username>

Rationalization: Lists all files opened by way of a specific client.

Example: lsof -u john

Output:

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
bash    5678 john  cwd    DIR  253,1     4096  78901 /space/john

This output lists all the files opened by way of the patron john, at the side of the command determine, PID, client, and other details.

3. Tick list Files Used by a Explicit Process

Syntax: lsof -p <PID>

Rationalization: Lists all files used by a specific process ID.

See also  The Entire Information to AI for Amazon Dealers in 2024

Example: lsof -p 1234

Output:

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
bash    1234 client  cwd    DIR  253,1     4096  78901 /space/client

This output lists all the files used by the process with PID 1234, at the side of the command determine, PID, client, and other details.

4. Tick list Files on a Explicit Port

Syntax: lsof -i :<port>

Rationalization: Lists all files associated with a specific neighborhood port.

Example: lsof -i :80

Output:

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
apache  1234 www-data  4u  IPv4  12345      0t0  TCP *:http (LISTEN)

This output lists all the files associated with port 80, generally used for HTTP. It incorporates the command determine, PID, client, and other details related to the neighborhood connection.

5. Tick list Files Opened by way of a Explicit Command

Syntax: lsof -c <command_name>

Rationalization: Lists all files opened by way of a specific command.

Example: lsof -c sshd

Output:

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
sshd    1234 root  cwd    DIR  253,1     4096      2 /

This output lists all the files opened by way of the sshd command, at the side of the command determine, PID, client, and other details.

6. Tick list Files Opened on a Explicit Document Machine

Syntax: lsof +D <checklist>

Rationalization: Lists all files opened inside of a specific checklist or record device.

Example: lsof +D /var

Output:

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
syslog  567 root    5w   REG  253,2     4096  12345 /var/log/syslog

This output lists all the files opened throughout the /var checklist, at the side of the command determine, PID, client, and other details.

7. Tick list Group Files of a Explicit Protocol

Syntax: lsof -i <protocol>

Rationalization: Lists all neighborhood files using a specific protocol (e.g., TCP or UDP).

Example: lsof -i TCP

Output:

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    1234 root    3u  IPv4  56789      0t0  TCP *:ssh (LISTEN)

This output lists all the TCP neighborhood connections, at the side of the command determine, PID, client, and other details related to the neighborhood connection.

8. Tick list Files Excluding Those Opened by way of a Explicit Individual

Syntax: lsof -u ^<username>

Rationalization: Lists all files apart from those opened by way of a specific client.

See also  Learn how to Degree Up as a Marketer within the AI Generation (The Proper Manner)

Example: lsof -u ^john

Output:

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
systemd    1 root  cwd    DIR  253,1     4096      2 /

This output lists all the files apart from those opened by way of the patron john, at the side of the command determine, PID, client, and other details.

Further Linux directions:
Checklist Operations rmdir · cd · pwd
Document Operations cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip
Document Machine Operations chown · mkfs
Networking ping · curl · wget · iptables
Search and Text Processing in finding · grep · sed · whatis
Machine Wisdom and Keep watch over env · historical past · best · who
Individual and Session Keep watch over display screen · su · sudo

The publish Tips on how to Use the ‘lsof’ Command in Linux gave the impression first on Hongkiat.

WordPress Website Development

Supply: https://www.hongkiat.com/blog/linux-command-lsof/

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!