The lsof command, status for “Checklist Open Recordsdata,” is an impressive software within the Linux running gadget that gives crucial details about recordsdata which are opened by means of more than a few processes. This flexible command permits gadget admins to observe and arrange recordsdata, sockets, and community connections, providing a complete view of the way assets are being applied inside the gadget.

Very similar to instructions like netstat and fuser, the lsof command is used to spot the recordsdata which are being accessed by means of other processes, making it an indispensable software for troubleshooting and gadget upkeep. Whether or not you’re a community administrator looking for to get to the bottom of conflicts or a developer taking a look to optimize useful resource allocation, the lsof command, frequently used along instructions like grep, is usually a to hand addition for your toolkit.

Tips on how to Set up the lsof Command

In lots of Linux distributions, the lsof command comes pre-installed. Then again, if it’s no longer to be had for your gadget, you’ll simply set up it the use of the bundle supervisor particular for your distribution.

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

Set up:

sudo apt-get replace
sudo apt-get set up lsof

Uninstall:

sudo apt-get take away lsof

Crimson Hat-based Programs (e.g., CentOS, RHEL)

Set up the use of Yum (older variations):

sudo yum set up lsof

Set up the use of DNF (more recent variations):

sudo dnf set up lsof

Uninstall the use of Yum:

sudo yum take away lsof

Uninstall the use of DNF:

sudo dnf take away lsof

Fedora

Set up (older variations the use of Yum):

sudo yum set up lsof

Set up (more recent variations the use of DNF):

sudo dnf set up lsof

Uninstall the use of Yum:

sudo yum take away lsof

Uninstall the use of DNF:

sudo dnf take away lsof

Tips on how to Use lsof

1. Checklist All Open Recordsdata

Syntax: lsof

Clarification: Lists all open recordsdata at the gadget.

Instance: lsof

Output:

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

This output presentations the entire open recordsdata, together with the command identify, procedure ID (PID), consumer, report descriptor (FD), kind, software, measurement, and the report’s trail.

2. Checklist Recordsdata Opened by means of a Particular Person

Syntax: lsof -u <username>

Clarification: Lists all recordsdata opened by means of a particular consumer.

Instance: lsof -u john

Output:

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

This output lists the entire recordsdata opened by means of the consumer john, together with the command identify, PID, consumer, and different main points.

3. Checklist Recordsdata Utilized by a Particular Procedure

Syntax: lsof -p <PID>

Clarification: Lists all recordsdata utilized by a particular procedure ID.

Instance: lsof -p 1234

Output:

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

This output lists the entire recordsdata utilized by the method with PID 1234, together with the command identify, PID, consumer, and different main points.

4. Checklist Recordsdata on a Particular Port

Syntax: lsof -i :<port>

Clarification: Lists all recordsdata related to a particular community port.

Instance: 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 the entire recordsdata related to port 80, usually used for HTTP. It contains the command identify, PID, consumer, and different main points associated with the community connection.

5. Checklist Recordsdata Opened by means of a Particular Command

Syntax: lsof -c <command_name>

Clarification: Lists all recordsdata opened by means of a particular command.

Instance: 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 the entire recordsdata opened by means of the sshd command, together with the command identify, PID, consumer, and different main points.

6. Checklist Recordsdata Opened on a Particular Record Gadget

Syntax: lsof +D <listing>

Clarification: Lists all recordsdata opened inside a particular listing or report gadget.

Instance: 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 the entire recordsdata opened inside the /var listing, together with the command identify, PID, consumer, and different main points.

7. Checklist Community Recordsdata of a Particular Protocol

Syntax: lsof -i <protocol>

Clarification: Lists all community recordsdata the use of a particular protocol (e.g., TCP or UDP).

Instance: 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 the entire TCP community connections, together with the command identify, PID, consumer, and different main points associated with the community connection.

8. Checklist Recordsdata Except The ones Opened by means of a Particular Person

Syntax: lsof -u ^<username>

Clarification: Lists all recordsdata with the exception of the ones opened by means of a particular consumer.

Instance: 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 the entire recordsdata with the exception of the ones opened by means of the consumer john, together with the command identify, PID, consumer, and different main points.

Extra Linux instructions:
Listing Operations rmdir · cd · pwd
Record Operations cat · cp · dd · much less · ls · mkdir · mv · tail · tar · zip
Record Gadget Operations chown · mkfs
Networking ping · curl · wget · iptables
Seek and Textual content Processing in finding · grep · sed · whatis
Gadget Knowledge and Control env · historical past · best · who
Person and Consultation Control display screen · su · sudo

The submit Tips on how to Use the ‘lsof’ Command in Linux seemed first on Hongkiat.

WordPress Website Development Source: https://www.hongkiat.com/blog/linux-command-lsof/

[ continue ]