lsof 指令詳解
lsof指令的原始功能是列出打開的檔案的程序,但LINUX下,所有的裝置都是以檔案的行式存在的,
是以一般root使用者才能執行lsof指令,普通使用者可以看見/usr/sbin/lsof指令,
但是普通使用者執行會顯示“permission denied”;總結一下lsof指令的用法:
lsof abc.txt 顯示開啟檔案abc.txt的程序
lsof -i :22 知道22端口現在運作什麼程式
lsof -c abc 顯示abc程序現在打開的檔案
lsof -g gid 顯示歸屬gid的程序情況
lsof +d /usr/local/ 顯示目錄下被程序開啟的檔案
lsof +D /usr/local/ 同上,但是會搜尋目錄下的目錄,時間較長
lsof -d 4 顯示使用fd為4的程序
lsof -i 用以顯示符合條件的程序情況
文法: lsof -i[46] [@hostname|hostaddr][:service|port]
46 --> IPv4 or IPv6
protocol --> TCP or UDP
hostname --> Internet host name
hostaddr --> IPv4位置
service --> /etc/service中的 service name (可以不隻一個)
port --> 端口号 (可以不隻一個)
例子: TCP:25 - TCP and port 25
@1.2.3.4 - Internet IPv4 host address 1.2.3.4
[email protected]:ftp - TCP protocol hosthaha.ks.edu.tw service name:ftp
lsof -n 不将IP轉換為hostname,預設是不加上-n參數
例子: lsof -i [email protected]:ftp -n
lsof -p 12 看程序号為12的程序打開了哪些檔案
lsof +|-r 控制lsof不斷重複執行,預設是15s重新整理
-r,lsof會永遠不斷的執行,直到收到中斷信号
+r,lsof會一直執行,直到沒有檔案被顯示
例子:不斷檢視目前ftp連接配接的情況:lsof -i [email protected]:ftp -r
lsof -s 列出打開檔案的大小,如果沒有大小,則留下空白
lsof -u username 以UID,列出打開的檔案
[root@tcx160 FILES]# lsof -i tcp:22
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
sshd 3261 root 3u IPv6 7301 TCP *:ssh (LISTEN)
sshd 19692 root 3u IPv6 348642 TCP 9.186.96.160:ssh->9.186.96.117:32914 (ESTABLISHED)
[root@localhost ~]# lsof -i tcp:8282 -------------------smb使用端口實際的例子
sshd 2116 root 3u IPv6 5142 TCP *:8282 (LISTEN)
sshd 4304 root 3u IPv6 11728 TCP 192.168.210.234:8282->192.168.210.161:1469 (ESTABLISHED)
[root@localhost ~]# lsof --help
lsof: illegal option character: -
lsof: illegal option character: e
lsof: no process ID specified
lsof 4.72
latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
usage: [-?abhlnNoOPRstUvV] [+|-c c] [+|-d s] [+D D] [+|-f]
[-F ] [-g ] [-i ] [+|-L ] [+m ] [+|-M] [-o ]
[-p s] [+|-r ] [-S ] [-T ] [-u s] [+|-w] [-x ] [--]
Defaults in parentheses; comma-separate set (s) items; dash-separate ranges.
-?|-h list help -a AND selections (OR) -b avoid kernel blocks
-c c cmd c, /c/ +c w COMMAND width (9)
+d s dir s files -d s select by FD set +D D dir D tree *SLOW?*
-i select IPv[46] files -l list UID numbers
-n no host names -N select NFS files -o list file offset
-O avoid overhead *RISK -P no port names -R list paRent PID
-s list file size -t terse listing -T disable TCP/TPI info
-U select Unix socket -v list version info -V verbose search
+|-w Warnings (+) -- end option scan
+f|-f +filesystem or -file names
-F select fields; -F? for help
+|-L list (+) suppress (-) link counts < l (0 = all; default = 0)
+m use|create mount supplement
+|-M portMap registration (-) -o o o 0t offset digits (8)
-p s select by PID set -S t second stat timeout (15)
-T qs TCP/TPI Q,St (s) info
-g select by process group ID set and print process group IDs
-i i select by IPv[46] address: [46][@host|addr][:svc_list|port_list]
+|-r repeat every t seconds (15); + until no files, - forever
-u s exclude(^)|select login|UID set s
-x cross over +d|+D File systems or symbolic Links
names select named files or files on named file systems
Anyone can list all files; /dev warnings disabled; kernel ID check disabled.