Lots of you may have encountered some error message stating that the port has been taken by another program while trying to start a program on Linux. And you would want to know which program takes the port you want to use. This post will provide some feasible ways to check out which program is taking a specific port.
lsof -i:[port]
lsof is the command to list open files on Linux. And if you know more about Linux you should get to know everything on Linux is a file even including network connection. Hence you can use lsof to list the network programs which are running.
The -i option will select the listing of files any of whose Internet address matches the address specified in i. And it can have a port specified as format :[port]. This will list the program using the port number specified.
Below is an example of check which service is taking port 3306.
From the output, it is clearly indicating that the port is taken by the mysql service. Yes, this port is the default port taken by mysql service.
netstat -tunlp | grep [port]
netstat is the command to print the network statistic for all network related services on Linux. You can use netstat -tunlp to print all the programs which are using tcp or udp protocols(With option t and u) and their corresponding address and listening port information(Option n and l). Then you can use grep to find the exact program you want to find.
The same 3306 example can be shown below.
The last column shows that this port is taken by mysqld which is the mysql service.
ss -tunlp | grep [port]
ss is used to dump socket statistics. It allows showing information similar to netstat but it can display more TCP and state information than other tools. It is widely considered as a replacement of netstat.
The output can be seen below.
grep [port] /etc/services
/etc/services contains information about common services and their default port and network protocol. This can be used to check some famous port number information.
There are also other methods which can help find out a port is taken by which program. We will keep update them.