87

How do you get lsof to produce numeric port information instead of attempting to resolve the port to service name?

For example, I want TCP *:http (LISTEN) to give me TCP *:80 (LISTEN) in-fact if at all possible I never want to see another service name in lsof print-out ever again. So if there is a way to make numeric ports the default I would like to understand how to do that as well.

Coder Guy
  • 1,843
  • 1
  • 15
  • 21

2 Answers2

126

Run lsof -P.

And make sure the P goes before the i, if you combine the option with -i:

lsof -Pi

According to man lsof, -P inhibits the conversion of port numbers to port names for network files. Inhibiting the conversion may make lsof run a little faster. It is also useful when port name lookup is not working properly."

mit
  • 11,083
  • 11
  • 50
  • 74
kwarunek
  • 12,141
  • 4
  • 43
  • 48
  • 6
    From the man page: `-n`: inhibits the conversion of network numbers to host names for network files. `-P`: inhibits the conversion of port numbers to port names for network files. – Kurt Zhong Apr 20 '17 at 08:25
16

Sometimes handy is:

lsof -Pi

When I tried losf -iP it gave me some trash, so make sure P goes first.


As a side note: lsof -3.14 will provide the same trash, don't try this.

prosti
  • 42,291
  • 14
  • 186
  • 151
  • 1
    Weird that order matters, even on macOS' lsof. – rodorgas Jan 15 '21 at 17:40
  • Order does not matter. From man " -i [i] selects the listing of files any of whose Internet address matches the address specified in i", so `-iP` means select Internet address matching P. Another thing is `lsof -P -i` works as `lsof -i -P`. – kwarunek Feb 09 '21 at 18:48
  • 1
    This works `lsof -i :4444 -P` for looking for processes listening to port 4444. Nice side note on 3.14. Give this a try: `alias lsof-3.14='lsof -Pi'` and then `lsof-3.14 :4444` :) – jamesmortensen Nov 30 '22 at 02:55