使用sudo lsof -nP -iTCP -sTCP:LISTEN查看占用端口的程序;因為 lsof 需要訪問核心內存和各種文件,所以必須以 root 用戶的身份運行它才能夠充分地發揮其功能
概念
文件句柄是windows系統的概念,在linux下稱之為文件描述符FD(file description)
常用命令
sort -nr 安裝數字逆序排列 -n number -r reverse
uniq -c 統計相同項的數量,分兩列顯示,左列顯示數量,右列顯示內容 -c count
1)統計各進程打開FD數:lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr
2)統計各用戶打開FD數:lsof -n|awk '{print $3}'|sort|uniq -c|sort -nr
3)統計各命令打開FD數:lsof -n|awk '{print $1}'|sort|uniq -c|sort -nr
單個進程的可用文件描述符的最大數量
ulimit -n
ulimit -a
強大的工具 lsof
man lsof
可以查看fd/type等各個字段的含義
FD:文件描述符,應用程序通過文件描述符識別該文件
1)cwd:表示 current work dirctory,即:應用程序的當前工作目錄,這是該應用程序啟動的目錄,除非它本身對這個目錄進行更改
2)txt:該類型的文件是程序代碼,如應用程序二進制文件本身或共享庫,如上列表中顯示的 /sbin/init 程序
3)lnn: library references (AIX)
4)er: FD information error (see NAME column)
5)jld: jail directory(FreeBSD)
6)ltx: shared library text(code and data)
7)mxx: hex memory-mapped type number xx.
8)m86:DOS Merge mapped file
9) mem: memory-mapped file
10)mmap: memory-mapped device
11)pd: parent directory
12)rtd: root directory
13)tr: kernel trace file (OpenBSD)
14)v86 VP/ix mapped file
15)0:表示標准輸出
16)1:表示標准輸入
17)2:表示標准錯誤
一般在標准輸出、標准錯誤、標准輸入后還跟着文件狀態模式:r/w/u 等
1)u:表示該文件被打開並處於讀取/寫入模式
2)r:表示該文件被打開並處於只讀模式
3)w:表示該文件被打開並處於只寫入模式
4)空格:表示該文件的狀態模式為 unknown ,且沒有鎖定
5)- : 表示該文件的狀態模式為 unknown ,且被鎖定
同時在文件狀態模式后面,還跟着相關的鎖
1)N:for a Solaris NFS lock of unknown type
2)r: for a read lock on part of the file
3)R:for a read lock on the entire file
4)w: for a write lock on part of the file (文件的部分寫鎖)
5)W: for a write lock on the entire file(整個文件的寫鎖)
6)u: for a read and write lock of any length
7)U: for a lock of unknown type
8)x: for an SCO OpenServer Xenix lock on part of the file
9)X:for an SCO OpentServer Xenix lock on the entire file
10)space : if there is no lock