Wednesday, April 6, 2011

Linux-Recipe: List all listening ports and the PID of the associated processes

Once in a while, you find yourself having to work on a box you know very little about. You try running an application you get an error telling you another application is already using the network port you need. What do you?
  • #lsof -Pan -i tcp -i udp
The lsof command is used to give a list of all open files - very powerful if you consider that *everything* is a file in Linux. This includes regular files, directories, block special files, a character special files, executing text reference, libraries, streams and network sockets.

It works for AIX, Apple Darwin, FreeBSD, Linux, NetBSD, NEXTSTEP, SCO OpenServer and Solaris 9 and 10.

Options
  • -P don't bother converting port numbers to port names - speeds up lsof.
  • -n don't bother converting ip addresses to host names
  • -i internet address must match the address specified (-i4 and -i6 are used to IPv4 and IPv6 respectively).
  • -a used to AND list options i.e show only files that satisfy all list options. The default behaviour is to show files that satisfy any of the list options.
Remember to refer to the man pages if in doubt.

No comments:

Post a Comment