[Linux] 使用 lsof 查詢某個 socket fd 是哪個連線

[Linux] 使用 lsof 查詢某個 socket fd 是哪個連線

今天看到某個 process 的 fd 開了一堆,而且有一堆的 socket,

不曉得是在做什麼連線呢?

testuser@localhost ~ # ll /proc/26708/fd
lr-x------    1 root     root           64 Jan 26 09:58 0 -> /dev/null
l-wx------    1 root     root           64 Jan 26 09:58 1 -> ~/log/test.log
lrwx------    1 root     root           64 Jan 26 10:02 10 -> socket:[36222307]=
lrwx------    1 root     root           64 Jan 26 10:02 11 -> socket:[35748018]=
lrwx------    1 root     root           64 Jan 26 10:02 12 -> socket:[35751132]=
lrwx------    1 root     root           64 Jan 26 10:02 13 -> socket:[35598981]=
lr-x------    1 root     root           64 Jan 26 10:02 14 -> socket:[36838101]=
l-wx------    1 root     root           64 Jan 26 10:02 15 -> socket:[36209739]=
lr-x------    1 root     root           64 Jan 26 10:02 16 -> socket:[39404442]=
l-wx------    1 root     root           64 Jan 26 10:02 17 -> socket:[35604609]=
lr-x------    1 root     root           64 Jan 26 10:02 18 -> socket:[35605172]=
l-wx------    1 root     root           64 Jan 26 10:02 19 -> socket:[36753211]=
l-wx------    1 root     root           64 Jan 26 09:58 2 -> ~/log/test.log
lr-x------    1 root     root           64 Jan 26 10:05 20 -> socket:[35617426]=
l-wx------    1 root     root           64 Jan 26 10:05 21 -> socket:[36877641]=
lr-x------    1 root     root           64 Jan 26 10:05 22 -> socket:[39291798]=
l-wx------    1 root     root           64 Jan 26 09:58 3 -> ~/log/test.log
l-wx------    1 root     root           64 Jan 26 09:58 4 -> ~/log/test.log
lrwx------    1 root     root           64 Jan 26 09:58 5 -> socket:[35573140]=
lr-x------    1 root     root           64 Jan 26 09:58 6 -> /dev/urandom
lrwx------    1 root     root           64 Jan 26 09:58 7 -> /dev/urandom
l-wx------    1 root     root           64 Jan 26 09:58 8 -> socket:[35751130]=
lrwx------    1 root     root           64 Jan 26 10:02 9 -> socket:[39338031]=

 

這時可以再請出 lsof 大神來幫忙~

舉例來說,想知道 fd 10 這個 socket 是連到哪的話,

先看它是 socket:[36222307],接著就用 lsof 查一下有含 36222307 這個字的地方,

這邊的 -n 是不要將 port number 轉成名稱 (會省一點時間),-i 是只秀出網路相關的檔案:

testuser@localhost ~ # lsof -n -i 2>/dev/null | grep 36222307
python    26708 root   10u  IPv4 36222307       TCP 10.1.1.1:47810->203.69.138.75:http (CLOSE_WAIT)

 

如上例,就可以看到這個 socket 就是從 10.1.1.1 連到 203.69.138.75 的一個連線囉~

 

 

(本頁面已被瀏覽過 1,832 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料