Академический Документы
Профессиональный Документы
Культура Документы
lsof stands for List Open Files. It is easy to remember lsof command if you think of it as ls + of, where ls stands for list, and of stands for open files. It is a command line utility which is used to list the information about the files that are opened by various processes. In unix, everything is a file, ( pipes, sockets, directories, devices, etc.). So by using lsof, you can get the information about any opened files.
1. Introduction to lsof
www.thegeekstuff.com/2012/08/lsof-command-examples/ 1/16
7/29/13
Simply typing lsof will provide a list of all open files belonging to all active processes.
#l s o f C O M M A N D P I D i n i t 1 i n i t 1 i n i t 1 i n i t 1 i n i t 1 i n i t 1 . . . U S E R F D r o o t c w d r o o t t x t r o o t 0 u r o o t 1 u r o o t 2 u r o o t 3 r T Y P E D I R R E G C H R C H R C H R F I F O D E V I C E S I Z E / O F F 8 , 1 4 0 9 6 8 , 1 1 2 4 7 0 4 1 , 3 0 t 0 1 , 3 0 t 0 1 , 3 0 t 0 0 , 8 0 t 0 N O D EN A M E 2/ 9 1 7 5 6 2/ s b i n / i n i t 4 3 6 9/ d e v / n u l l 4 3 6 9/ d e v / n u l l 4 3 6 9/ d e v / n u l l 6 3 2 3p i p e
By default One file per line is displayed. Most of the columns are self explanatory. We will explain the details about couple of cryptic columns (FD and TYPE). FD Represents the file descriptor. Some of the values of FDs are, cwd Current Working Directory txt Text file mem Memory mapped file mmap Memory mapped device NUMBER Represent the actual file descriptor. The character after the number i.e 1u, represents the mode in which the file is opened. r for read, w for write, u for read and write. TYPE Specifies the type of the file. Some of the values of TYPEs are,
www.thegeekstuff.com/2012/08/lsof-command-examples/
2/16
7/29/13
REG Regular File DIR Directory FIFO First In First Out CHR Character special file For a complete list of FD & TYPE, refer man lsof.
7/29/13
C O M M A N D P I D U S E R F D T Y P ED E V I C ES I Z E / O F F N O D EN A M E r s y s l o g d 4 8 8s y s l o g 1 w R E G 8 , 1 1 1 5 12 6 8 9 4 0/ v a r / l o g / s y s l o g r s y s l o g d 4 8 8s y s l o g 2 w R E G 8 , 1 2 4 0 52 6 9 6 1 6/ v a r / l o g / a u t h . l o g c o n s o l e k1 4 4 r o o t 9 w R E G 8 , 1 1 0 8 7 12 6 9 3 6 9/ v a r / l o g / C o n s o l e K i t / h i s t o r y
7/29/13
C O M M A N D P I D U S E R F D T Y P E u p d a t e n o1 8 9 2l a k s h m a n a n 2 0 r F I F O u p d a t e n o1 8 9 2l a k s h m a n a n 2 1 w F I F O b a s h 1 9 9 5l a k s h m a n a n c w d D I R
D E V I C ES I Z E / O F F 0 , 8 0 t 0 0 , 8 0 t 0 8 , 1 4 0 9 6
N O D EN A M E 1 4 5 3 6p i p e 1 4 5 3 6p i p e 3 9 3 2 1 8/ h o m e / l a k s h m a n a n
Sometimes you may want to list files opened by all users, expect some 1 or 2. In that case you can use the ^ to exclude only the particular user as follows
#l s o fu^ l a k s h m a n a n C O M M A N D P I D r t k i t d a e1 3 8 0 u d i s k s d a1 5 8 4 U S E R F D r t k i t 7 u r o o t c w d T Y P E 0 0 0 0 D I R D E V I C E S I Z E / O F F 0 , 9 0 8 , 1 4 0 9 6 N O D EN A M E 4 3 6 0a n o n _ i n o d e 2/
The above command listed all the files opened by all users, expect user lakshmanan.
The above command will kill all process belonging to user lakshmanan, which has files opened. Similarly you can also use -t in many ways. For example, to list process id of a process which opened /var/log/syslog can be done by
www.thegeekstuff.com/2012/08/lsof-command-examples/ 5/16
7/29/13
#l s o ft/ v a r / l o g / s y s l o g 4 8 9
Talking about kill, did you know that there are 4 Ways to Kill a Process?
The above command uses two list options, -u and -c. So the command will list process belongs to user lakshmanan as well as process name starts with init. But when you want to list a process belongs to user lakshmanan and the process name starts with init, you can use -a option.
#l s o ful a k s h m a n a nci n i ta
The above command will not output anything, because there is no such process named init belonging to user lakshmanan.
7/29/13
= = = = = = = = = = = = = = C O M M A N D P I D U S E R i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n i n i t a . s h2 9 7 1l a k s h m a n a n = = = = = = =
F D T Y P ED E V I C ES I Z E / O F F N O D EN A M E c w d D I R 8 , 1 4 0 9 6 3 9 3 2 1 8/ h o m e / l a k s h m a n a n r t d D I R 8 , 1 4 0 9 6 2/ t x t R E G 8 , 1 8 3 8 4 8 5 2 4 3 1 5/ b i n / d a s h m e m R E G 8 , 1 1 4 3 4 1 8 01 4 4 2 6 2 5/ l i b / i 3 8 6 l i n u x g n u / l i b c 2 . 1 3 . s o m e m R E G 8 , 1 1 1 7 9 6 01 4 4 2 6 1 2/ l i b / i 3 8 6 l i n u x g n u / l d 2 . 1 3 . s o 0 u C H R 1 3 6 , 4 0 t 0 7/ d e v / p t s / 4 1 u C H R 1 3 6 , 4 0 t 0 7/ d e v / p t s / 4 2 u C H R 1 3 6 , 4 0 t 0 7/ d e v / p t s / 4 1 0 r R E G 8 , 1 2 0 3 9 3 5 7 8/ h o m e / l a k s h m a n a n / i n i t a . s h
In the above output, for the first 5 seconds, there is no output. After that a script named inita.sh is started, and it list the output.
You can also use -i4 or -i6 to list only IPV4 or IPV6 respectively.
7/29/13
The above command will list the network files opened by the processes starting with ssh.
www.thegeekstuff.com/2012/08/lsof-command-examples/
8/16
7/29/13
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you dont know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator. Get the Linux Sysadmin Course Now!
www.thegeekstuff.com/2012/08/lsof-command-examples/
9/16
7/29/13
Tags: AIX lsof, lsof for Windows, lsof Port { 11 comments read them below or add one } 1 Srinivas August 29, 2012 at 10:57 am Hi It is good article. I knew most of the lsof option but still learned few things from this article. Particularly +d option and and repeat mode. Very highly informative article. Would it be possible to write about complex one liners which gives much more information? Like using lsof command to generate top 10 files opened processes etc. Srinivas 2 raghavan August 29, 2012 at 3:29 pm Excellent tutorial again. May be you could write a second part going in detail about the various columns (FD, TYPE, DEVICE, SIZE/OFF). I generally do prefer netstat over lsof for information on networking. 3 Manojkumar August 30, 2012 at 12:08 am Nice to recall the options once again. 4 Ankit August 30, 2012 at 1:54 am Good tutorial, i just encountered the need to use the command today and here I found the good tutorial. Thanks. 5 niraj August 30, 2012 at 2:33 am informative and nice article .
www.thegeekstuff.com/2012/08/lsof-command-examples/ 10/16
7/29/13
6 Adorn August 30, 2012 at 10:18 pm Very good tutorial..! Keep on posting.. 7 Ethan August 31, 2012 at 6:08 pm very good article. Linux belong to all of us interested in it. 8 Mark September 2, 2012 at 7:14 am This is good information. On larger systems where root or oracle have lots of files open, I use these commands to properly tune my systems ulimits. Thanks. **Read more on ulimits before proceeding. 9 Jalal Hajigholamali September 5, 2012 at 9:45 pm Hi, Very good article, thanks a lot 10 Leenus December 14, 2012 at 10:48 pm Nice article, thanks a lot for the author of this book. 11 Dhamudba February 14, 2013 at 12:54 pm ThanksIts useful. Leave a Comment Name E-mail Website
www.thegeekstuff.com/2012/08/lsof-command-examples/
11/16
7/29/13
Previous post: C Linked List Data Structure Explained with an Example C Program Next post: Linux uname Command Examples (Get Kernel version, release, hostname, etc)
Search
7/29/13
COURSE
Linux Sysadmin CentOS 6 Course - Master the Tools, Configure it Right, and be Lazy
EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well
POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library 50 UNIX / Linux Sysadmin Tutorials 50 Most Frequently Used UNIX / Linux Commands (With Examples) How To Be Productive and Get Things Done Using GTD 30 Things To Do When you are Bored and have a Computer Linux Directory Structure (File System Structure) Explained with Examples Linux Crontab: 15 Awesome Cron Job Examples Get a Grip on the Grep! 15 Practical Grep Command Examples Unix LS Command: 15 Practical Examples 15 Examples To Master Linux Command Line History Top 10 Open Source Bug Tracking System Vi and Vim Macro Tutorial: How To Record and Play Mommy, I found it! -- 15 Practical Linux Find Command Examples 15 Awesome Gmail Tips and Tricks 15 Awesome Google Search Tips and Tricks RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams Can You Top This? 15 Practical Linux Top Command Examples Top 5 Best System Monitoring Tools Top 5 Best Linux OS Distributions How To Monitor Remote Linux Host using Nagios 3.0
www.thegeekstuff.com/2012/08/lsof-command-examples/ 13/16
7/29/13
Awk Introduction Tutorial 7 Awk Print Examples How to Backup Linux? 15 rsync Command Examples The Ultimate Wget Download Guide With 15 Awesome Examples Top 5 Best Linux Text Editors Packet Analyzer: 15 TCPDUMP Command Examples The Ultimate Bash Array Tutorial with 15 Examples 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id Unix Sed Tutorial: Advanced Sed Substitution Examples UNIX / Linux: 10 Netstat Command Examples The Ultimate Guide for Creating Strong Passwords 6 Steps to Secure Your Home Wireless Network Turbocharge PuTTY with 12 Powerful Add-Ons
CATEGORIES
Linux Vim Sed Awk Bash Nagios OpenSSH IPTables Apache MySQL Perl Google Ubuntu PostgreSQL Hello World C Programming C++ Programming DELL Oracle
www.thegeekstuff.com/2012/08/lsof-command-examples/ 14/16
7/29/13
VMware
My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.
Support Us
Support this blog by purchasing one of my ebooks. Bash 101 Hacks eBook Sed and Awk 101 Hacks eBook Vim 101 Hacks eBook Nagios Core 3 eBook
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!. Follow us on Twitter
www.thegeekstuff.com/2012/08/lsof-command-examples/ 15/16
7/29/13
Become a fan on Facebook Copyright 20082013 Ramesh Natarajan. All rights reserved | Terms of Service | Advertise
www.thegeekstuff.com/2012/08/lsof-command-examples/
16/16