1, Lsof Introduction
Lsof is a very practical system-level monitoring and diagnostic tool under Linux.
It means List Open Files, and it's easy for you to remember that it's a combination of "ls + of" ~
It can be used to list the file information opened by various processes, remember: Linux under "All Files",
including but not limited to pipes, sockets, directories, devices, etc.
So, with lsof, you can get a variety of information about any file that is opened.
You can generate a lot of information simply by entering lsof, because lsof needs to access core memory and various files, so it must run as root to fully function.
Example output for lsof:
- [Email Protected]:~/lab 0# lsof
- COMMAND PID TID USER FD TYPE DEVICE size/off NODE NAME
- Systemd 1 root cwd DIR 8,6 4096 2/
- Systemd 1 root RTD DIR 8,6 4096 2/
- Systemd 1 root txt REG 8,6 2273340 1834909/usr/lib/systemd/systemd
- Systemd 1 root mem REG 8,6 210473 1700647/lib/libnss_files-2.15.s
- ...
2. Common usage of lsof
2.1 Monitoring open files, devices
View files, which processes are occupied by the device:
- # lsof/dev/tty1
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Bash 1770 Jian 0u CHR 4,1 0t0 1045/dev/tty1
- Bash 1770 Jian 1u CHR 4,1 0t0 1045/dev/tty1
- Bash 1770 Jian 2u CHR 4,1 0t0 1045/dev/tty1
- Bash 1770 Jian 255u CHR 4,1 0t0 1045/dev/tty1
- StartX 1845 Jian 0u CHR 4,1 0t0 1045/dev/tty1
- StartX 1845 Jian 1u CHR 4,1 0t0 1045/dev/tty1
- ...
2.2 Monitoring File System
Specify the directory, mount point, and you can see which processes are opening the files under it:
- # lsof/data/
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Bash 15983 Jian cwd DIR 8,5 4096 8252/data/backup
- ...
This is useful when umount a file system failure (typically reported to the FS is busy).
List files that are opened under a directory (mount point such as/home):
- # lsof +d/var/log/
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- RSYSLOGD 488 syslog 1w REG 8,1 1151 268940/var/log/syslog
- RSYSLOGD 488 syslog 2w REG 8,1 2405 269616/var/log/auth.log
- Console-k 144 root 9w REG 8,1 10871 269369/var/log/consolekit/history
Lists files opened by the specified process name:
- # lsof-c SSH-C Init
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Init 1 root txt REG 8,1 124704 917562/sbin/init
- Init 1 root mem REG 8,1 1434180 1442625/lib/i386-linux-gnu/libc-2.13.so
- Init 1 root mem REG 8,1 30684 1442694/lib/i386-linux-gnu/librt-2.13.so
- ...
- Ssh-agent 1528 Lakshmanan 1u CHR 1,3 0t0 4369/dev/null
- Ssh-agent 1528 Lakshmanan 2u CHR 1,3 0t0 4369/dev/null
- Ssh-agent 1528 Lakshmanan 3u Unix 0xdf70e240 0t0 10464/tmp/ssh-suymkxxw1495/agent.1495
2.3 Monitoring Process
Specify the process number to view the files opened by the process:
- # lsof-p 2064
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Firefox 2064 Jian cwd DIR 8,6 4096 1571780/home/jian
- Firefox 2064 Jian rtd DIR 8,6 4096 2/
- Firefox 2064 Jian txt REG 8,6 44224 1985670/usr/lib/firefox-12.0/firefox
- Firefox 2064 Jian Mem REG 8,6 14707012 925361/usr/share/fonts/chinese/msyhbd.ttf
- Firefox 2064 Jian Mem REG 8,6 15067744 925362/usr/share/fonts/chinese/msyh.ttf
- Firefox 2064 Jian Mem REG 8,6 16791251 1701681/USR/SHARE/FONTS/WENQUANYI/WQY-ZENHEI.TTC
- Firefox 2064 Jian Mem REG 0,16 67108904 10203/dev/shm/pulse-shm-3021850167
- ...
When you want to kill all open files and devices for a user, you can do this:
- Kill-9 ' lsof-t-u Lakshmanan '
The role of-T here is to list the process ID column separately.
For 4 ways to kill a process, please refer to:
http://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/
2.4 Monitoring Network
View which processes are in use on the specified port (Lsof-i lists all open Network connections):
- # lsof-i:22
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- sshd 1569 root 3u IPv4 10303 0t0 TCP *:ssh (LISTEN)
- sshd 1569 Root 4u IPv6 10305 0t0 TCP *:ssh (LISTEN)
- ...
List all network files that are opened by a process:
- Lsof-i-a-p 234
Or:
- Lsof-i-a-c SSH
List all TCP, UDP connections:
- Lsof-i TCP;
- Lsof-i UDP;
List all NFS files:
- Lsof-n-U lakshmanan-a
To see which processes are in use for a specified network port:
- # lsof [email protected]
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Skype 1909 Jian 54u IPv4 9116 0t0 TCP 192.168.1.91:40640->64.4.23.153:40047 (established)
- Pidgin 1973 Jian 7u IPv4 6599 0t0 TCP 192.168.1.91:59311->hx-in-f125.1e100.net:https (established)
- Pidgin 1973 Jian 13u IPv4 9260 0t0 TCP 192.168.1.91:54447->BY2MSG3010511.PHX.GBL:MSNP (established)
- ...
3, more use of skills
3.1 Monitoring user
To view the files that are open by the specified user (Lsof-u ^lakshmanan can exclude someone):
- # lsof-u Messagebus
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Dbus-daem 1805 Messagebus cwd DIR 8,6 4096 2/
- Dbus-daem 1805 Messagebus RTD DIR 8,6 4096 2/
- Dbus-daem 1805 Messagebus txt REG 8,6 1235361 1834948/usr/bin/dbus-daemon
- Dbus-daem 1805 Messagebus mem REG 8,6 210473 1700647/lib/libnss_files-2.15.so
- Dbus-daem 1805 Messagebus mem REG 8,6 190145 1700642/lib/libnss_nis-2.15.so
- Dbus-daem 1805 Messagebus mem REG 8,6 490366 1700636/lib/libnsl-2.15.so
- ...
3.2 Monitoring Applications
To view files opened by the specified program:
- # lsof-c Firefox
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Firefox 2064 Jian cwd DIR 8,6 4096 1571780/home/jian
- Firefox 2064 Jian rtd DIR 8,6 4096 2/
- Firefox 2064 Jian txt REG 8,6 44224 1985670/usr/lib/firefox-12.0/firefox
- Firefox 2064 Jian Mem REG 8,6 14707012 925361/usr/share/fonts/chinese/msyhbd.ttf
- Firefox 2064 Jian Mem REG 8,6 15067744 925362/usr/share/fonts/chinese/msyh.ttf
- Firefox 2064 Jian Mem REG 8,6 16791251 1701681/USR/SHARE/FONTS/WENQUANYI/WQY-ZENHEI.TTC
- ...
4. Command mode tricks
4.1 Combining logical query conditions
Only multiple query conditions are met, with the "-a" parameter, the default is-O.
- # Lsof-a-C bash-u Root
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- Bash 1986 Root CWD DIR 8,6 4096 1701593/root/lab
- Bash 1986 root RTD DIR 8,6 4096 2/
- Bash 1986 Root txt REG 8,6 1994157 1700632/bin/bash
- Bash 1986 root mem REG 8,6 9690800 405214/usr/lib/locale/locale-archive
- Bash 1986 root mem REG 8,6 210473 1700647/lib/libnss_files-2.15.so
4.2 The recurrence mode of the lsof command:
Repeats the lsof based on the given parameter delay of how many seconds:
- +r says repeat mode will end itself when no file is opened.
- -R means that it will execute regardless of whether the file exists or is opened, until you interrupt it.
The output of each loop is delimited with ' ======= ', and you can also use '-R ' | ' +r ' specifies the delay time.
- # lsof-u lakshmanan-c init-a -r5
- =======
- =======
- COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
- inita.sh 2971 Lakshmanan cwd DIR 8,1 4096 393218/home/lakshmanan
- inita.sh 2971 Lakshmanan RTD DIR 8,1 4096 2/
- inita.sh 2971 Lakshmanan txt REG 8,1 83848 524315/bin/dash
- inita.sh 2971 Lakshmanan mem REG 8,1 1434180 1442625/lib/i386-linux-gnu/libc-2.13.so
- inita.sh 2971 Lakshmanan mem REG 8,1 117960 1442612/lib/i386-linux-gnu/ld-2.13.so
- inita.sh 2971 Lakshmanan 0u CHR 136,4 0t0 7/DEV/PTS/4
- inita.sh 2971 Lakshmanan 1u CHR 136,4 0t0 7/DEV/PTS/4
- inita.sh 2971 Lakshmanan 2u CHR 136,4 0t0 7/DEV/PTS/4
- inita.sh 2971 Lakshmanan 10r REG 8,1 393578/home/lakshmanan/inita.sh
- =======
The above output is not output for the first 5 seconds, and then "inita.sh" starts, with output.
5, the last skill
About disk space alarm df-h--max=1 and DU-HX--max=1 display inconsistent issues,
The most common scenario is the following:
- Lsof|grep-i Delete
Look at the deleted files: some deleted files, but the process is not reload, those space is still occupied, you can understand that the process is similar to Windows, the handle is not released concept bar ~ Just windows if there are files being used by the process, you are generally not deleted, and Linux Although the deletion limit is not done, but wait until the process is finished using the file to completely release, to prevent the process to collapse, this is the operating system of resources management differences IT ~
For example Nginx will have a lot of temporary files occupy the/tmp directory, deleted, still occupy space,
At this point you can:
- pkill-9 nginx &&/etc/init.d/nginx Restart
Well, this article to the end of this, about lsof there are many, but the elder brother commonly used, know that this, elder brother can only help you here,
If you still need other content, please google it yourself, Sao years ...
6, refer:
To find open files using lsof:
Http://www.ibm.com/developerworks/cn/aix/library/au-lsof.html
Linux lsof Command Examples (Identify Open Files):
http://www.thegeekstuff.com/2012/08/lsof-command-examples/
The lsof of practical system Tools:
http://www.yLinux.org/forum/t/276
Linux System Monitoring and diagnostics tool: Lsof