10 Event Of Lsof Commands Inward Unix Together With Linux

It's been a long fourth dimension since I receive got written anything on UNIX or Linux, but today I'll beak well-nigh the lsof command, a utility ascendancy every organisation admin too developers love. The lsof ascendancy stands for listing opened upward file descriptors too every bit the get upward suggests, it is used to notice opened upward files past times process. Since almost everything inwards UNIX are file, you lot tin post away role lsof ascendancy to notice an opened upward regular file, a directory, a symbolic link, a block special file, an NFS mounted file, a socket stream, a shared library, a graphic symbol special file, a regular pipe, a named pipe, an mesh socket, a UNIX domain socket too many others. I receive got mostly used it to notice all the files opened past times a item process, which I volition present you lot inwards the coming section.

It's an incredibly useful tool to create debugging too troubleshooting inwards UNIX too Linux environment, too because of its versatile nature, it's also known every bit the "The Swiss Army Knife of Linux". Since most of the production organisation runs on the UNIX-based operating system, cognition of this tool acquire out fifty-fifty to a greater extent than important.

As a Java developer, I oft role lsof command to notice all the files opened my Java process, or roughly fourth dimension to notice which procedure has opened a item file e.g. a file which is used to stand upward for a cache inwards memory, lsof is incredibly useful on doing that.

In this tutorial, nosotros are going to larn dissimilar ways too options to role lsof roughly task, which volition farther assist you lot to notice procedure too file related information inwards UNIX or Linux.  You tin post away combine the lsof amongst grep command for advanced search too filtering.

I oft role grep ascendancy to filter output based upon file name, procedure id or file type. If you lot are non familiar amongst basic commands similar grep too find, thus I advise you lot accept a await at Linux Command Line Basics, a basic course of education for whatever programmer, tester, information safety people or anyone, who function on UNIX too Linux based environment.



How to acquire lsof command

The lsof ascendancy to a greater extent than oft than non comes pre-installed inwards many UNIX systems. If you lot are getting -bash: lsof: ascendancy non found an mistake spell using lsof thus it could endure that lsof is non inwards your PATH.  Just cheque /usr/bin or /usr/sbin folder for this command. If you lot don't notice at that spot thus you lot tin post away install it from source or you lot tin post away inquire your UNIX admin to create that for you.

1) How to listing all opened upward files past times all process

$ lsof

Simply running lsof without whatever declaration impress all opened file too process. This is non peculiarly useful but a practiced starting point.



2) How to listing all procedure which has opened a file

$ lsof /home/someuser/somefile

volition listing all the procedure which has opened this file. you lot tin post away run across the command, PID, user too total file path to find out the process.



3) How to notice all opened files past times a user 
You tin post away role lsof -u ascendancy to listing all opened file past times a user every bit shown below

$ lsof -u username

You tin post away supply comma separated listing of users to notice a list of opened upward files past times multiple users every bit shown below

$ lsof -u user1,user2,user3

You tin post away create the same past times providing -u choice multiple times :

$ lsof -u user1 -u user2

If you lot are struggling to retrieve these lsof ascendancy options, hither is a squeamish diagram from Julia Evans which volition assist them to remember. If you lot want, you lot tin post away also accept a impress out of this too continue at your desk for quick reference:
s been a long fourth dimension since I receive got written anything on UNIX or Linux 10 Example of lsof commands inwards UNIX too Linux




4) How to listing all files opened past times a item command
You tin post away role lsof -c choice to supply the get upward of the ascendancy too listing downward all the files opened past times that command, for example, to listing all file opened past times java process, you lot tin post away create this :

$ lsof -c java

This is ameliorate than using grep for filtering, every bit instead of writing lsof | grep java, you lot tin post away simply write lsof -c java.

You tin post away also notice all files opened past times apache which runs as httpd every bit shown below :

lsof -c httpd

Just similar multiple users, you lot tin post away also combine multiple processes get upward to listing downward files tally past times them e.g.

$ lsof -c coffee -c httpd




5) How to notice all files opened past times a item user too command
You tin post away combine users too procedure get upward inwards ane lsof ascendancy to listing downward all the files opened past times a item procedure or a item user every bit shown below :

$ lsof -u root -c java

This volition listing all files opened or tally past times root user + all files opened past times the coffee process. See TCP too UDP), for example

$ lsof -i 

You tin post away farther notice all TPC connexion past times using the tcp choice every bit shown below :

$ lsof -i tcp

Similarly, to notice all opened upward udp connections you lot tin post away create :

$ lsof -i udp

volition listing all procedure amongst opened upward mesh sockets.




10) How to notice which procedure is using a port 
Though you lot tin post away create this amongst netstat ascendancy every bit well, you lot would endure surprised to know that you lot tin post away find all procedure using a item TCP or UDP port using the lsof command. For event :

$ lsof -i :19500 

volition notice the procedure which is using TCP or UDP port 19500

You tin post away fifty-fifty names defined inwards etc/services instead of port publish e.g.

$ lsof -i :smtp

volition impress procedure using the SMTP port.

You tin post away also combine TCP too UDP amongst a port to create to a greater extent than specific search e.g. to notice all procedure inwards UNIX which are uses TCP port publish 19600 you lot tin post away create next :

$ lsof -i tcp:19600 

too to notice all procedure which is using UDP port 17600 you lot tin post away use

$ lsof -i udp:17600


Now that you lot receive got seen how to role lsof ascendancy to create dissimilar things, let's revise them thus that you lot tin post away retrieve whatever you lot receive got learned thus far. Here is a squeamish summary of lsof ascendancy examples inwards Linux:

s been a long fourth dimension since I receive got written anything on UNIX or Linux 10 Example of lsof commands inwards UNIX too Linux




That's all well-nigh 10 examples of lsof ascendancy inwards UNIX too Linux. As I said, it's incredibly useful to notice the listing of files opened past times a item processor to notice all the procedure which holds a lock on a file. Since almost everything is a file inwards UNIX, you lot tin post away role lsof to notice out opened upward socket, directory, symbolic link, mesh socket too many others. You tin post away also run across the lsof human page for total documentation too to a greater extent than options.

Further Learning
Linux Command Line Basics
5 Free Linux Courses for Programmers
5 Courses to Learn Shell Scripting for Developers
10 Tips to improve your speed inwards Linux ascendancy lines

Thanks for reading this article thus far. If you lot notice these lsof commands useful thus delight portion amongst your friends too colleagues. If you lot receive got whatever questions or feedback thus delight driblet a note.

Komentar

Postingan populer dari blog ini

How To Fix Invalid Target Release: 1.7, 1.8, 1.9, Or 1.10 Fault Inwards Maven Build

Top Five Books To Larn Agile Too Scrum For Programmers - Best Of Lot, Must Read

How To Schedule Leap Professional Person Certification Attempt Using Voucher Online - Pace Past Times Pace Guide