Determining the networking ports in use on a Linux system
How to check ports in use on a Linux system
When troubleshooting network issues on server side you will will want to know if socket is open on the server side. It is often important to know which process is listening on this socket as well.
A utility called lsof
does the job here. Lsof lists on its standard output file information about files opened by processes. An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a network file (Internet socket, NFS file or UNIX domain socket.) A specific file or all the files in a file system may be selected by path.
To get the list of ports open a a given system, run the following command,
$ sudo lsof -i -P -n | grep LISTEN
The here are what the switches used above mean.
-i [i] selects the listing of files any of whose Internet address
matches the address specified in i. If no address is
specified, this option selects the listing of all Internet
and x.25 (HP-UX) network files.
-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.
-n inhibits the conversion of network numbers to host names for
network files. Inhibiting conversion may make lsof run
faster. It is also useful when host name lookup is not
working properly.