LoginSignup
54
51

More than 5 years have passed since last update.

ポートを握っているプロセスを見たい時

Last updated at Posted at 2014-05-27

ふとCentOSでman netstatを眺めていて『へぇ〜! ° °( Д )』となったので、メモ

普通にやるなら

$ sudo lsof -i:8080
COMMAND     PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
squid      1986    squid   11u  IPv6 1771222      0t0  TCP *:webcache (LISTEN)
clock-app 10204    user1   22u  IPv4 1883730      0t0  TCP example.local:46261->example.local:webcache (CLOSE_WAIT)

man netstatより抜粋

-p, --program
Show the PID and name of the program to which each socket belongs.

という事は、以下で良いという事ですよ!

$ sudo netstat -anp | grep 8080
tcp        1      0 127.0.0.1:46273             127.0.0.1:8080              CLOSE_WAIT  10204/clock-applet  
tcp        0      0 :::8080                     :::*                        LISTEN      1986/(squid)     

出てくる情報は簡略化されているので、分かり難い場合があるかもしれませんが、その場合は、pidがあるので、psを叩けば(結局叩くのかよ)何か分かります。

ただし

macでman netstatをすると以下になっています

 -p protocol
       Show statistics about protocol, which is either a well-known name for a protocol or an alias for it.  Some protocol names
       and aliases are listed in the file /etc/protocols.  The special protocol name ``bdg'' is used to show bridging statistics.
       A null response typically means that there are no interesting numbers to report.  The program will complain if protocol is
       unknown or if there is no statistics routine for it.

なので、環境によってですけどね。

まとめ

netstatに-pオプションがある環境でプロセスを知りたい時

sudo netstat -anp | grep ポート番号
必要に応じてsudo ps aux | grep プロセスID

何も気にしたくない時

sudo lsof -i:ポート番号

と、まとめてしまうと使えない子みたいに見えますが、「-pオプションが使える環境ではとりあえずつけておく」という使い方も良いかもしれませんよね。
どのポートを調べる事になるか分からない事もありますし!

おまけ

というような事を書いていたのですが、先日某所で行われた勉強会で発表をされていた先生(学生さん)が「ぱんつ♪ぱんつ♪」と言いながらデモをされていたのが耳に残ってしまいました・・・(笑
最初は『なんの事を言っているのだろう?』と思っていたのですが、よくよく画面を観ると

sudo netstat -pantu

とありました。
よく思いつきますね〜。
tuをつけると、Unix domain socketsが表示されなくなりますが、普段はこちらを気にする機会は少ないと思います。
なので、皆様もパンツで調べましょう!

54
51
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
51