Linux作業系統上Lsof命令詳解 一般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的進程 www.2cto.com lsof -i 用以顯示合格進程情況文法: lsof -i[46] [protocol][@hostname|hostaddr][:service|port]46 --> IPv4 or IPv6protocol --> TCP or UDPhostname --> Internet host namehostaddr --> IPv4位置service --> /etc/service中的 service name (可以不只一個)port --> 連接埠號碼 (可以不只一個)
例子: TCP:25 - TCP and port 25@1.2.3.4 - Internet IPv4 host address 1.2.3.4tcp@ohaha.ks.edu.tw:ftp - TCP protocol hosthaha.ks.edu.tw service name:ftplsof -n 不將IP轉換為hostname,預設是不加上-n參數
例子: lsof -i tcp@ohaha.ks.edu.tw:ftp -nlsof -p 12 看進程號為12的進程開啟了哪些檔案lsof +|-r [t] 控制lsof不斷重複執行,預設是15s重新整理-r,lsof會永遠不斷的執行,直到收到中斷訊號+r,lsof會一直執行,直到沒有檔案被顯示
例子:不斷查看目前ftp串連的情況:lsof -i tcp@ohaha.ks.edu.tw:ftp -rlsof -s 列出開啟檔案的大小,如果沒有大小,則留下空白lsof -u username 以UID,列出開啟的檔案 www.2cto.com 作者 san_yun