The Computer Oracle

How to use netstat to show what process is listening on a port

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzling Curiosities

--

Chapters
00:00 How To Use Netstat To Show What Process Is Listening On A Port
00:24 Accepted Answer Score 59
01:09 Answer 2 Score 8
01:28 Answer 3 Score 6
01:42 Answer 4 Score 4
01:57 Answer 5 Score 1
02:05 Thank you

--

Full question
https://superuser.com/questions/627391/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#networking #macos #netstat

#avk47



ACCEPTED ANSWER

Score 59


Unfortunately on OSX you're stuck with the BSD netstat which will not show you the process ID that is attached to a given port. What you have to do instead is use lsof. The syntax you'll need to use is:

lsof -i :8080

This will print out gobs of information, most of which you don't care about, but the fields are well labeled. For example, check out this example output.

lsof -i :53237
COMMAND   PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
GoogleTal 927 guest   29u  IPv4 0x2c3f7f95244855c3      0t0  TCP localhost:53237 (LISTEN)

This tells me that port 53237 is in use by process ID 927. When reading the COMMAND field keep in mind that this output is truncated, in reality the full name of the binary is GoogleTalkPlugin.




ANSWER 2

Score 8


This is what I like to use when looking for a listening port's PID. For Linux use: netstat -tunlp

  • n network
  • l listening ports
  • p process
  • t tcp
  • u udp

Additional information can be found in the man pages.




ANSWER 3

Score 6


I was in the process of modifying netstat on OS X to provide this feature and stumbled upon the fact that -v will give you the pid associated with a socket.




ANSWER 4

Score 4


For me, the following two lines work best to show which apps have listening ports open, and tunnel, lsof is fully cross-platform:

netstat -Watn | grep LISTEN
lsof -Pnl +M -i -cmd | grep LISTEN



ANSWER 5

Score 1


To find out specific port use below netstat command

  netstat -an | grep ':8080'