How to check which service is running on which port
Introduction
To check service port numbers on a Unix based operating system, you may use one of the following methods…
- netstat command
netstat command is used to display network related data. It is available on most Unix flavors including MacOS. The syntax may vary so it is recommended to check the manual (ex. man netstat or netstat -h). Here is an example syntax on Mac and Linux…
1 2 |
# Mac netstat -p tcp | grep 80 |
The output should be something like…
1 2 |
tcp4 0 0 localhost.4244 localhost.50114 ESTABLISHED tcp4 0 0 localhost.50114 localhost.4244 ESTABLISHED |
1 2 |
# Linux netstat -ltnp | grep 4 |
The output should be something like…
1 |
tcp6 0 0 :::30380 :::* LISTEN 1/--CODINGGROUND-- |
- lsof command
lsof command is used to find which files are open by which process. It can be used to check port numbers as well. Let us have an example…
1 |
lsof -i :80 |
The output should be something like…
1 2 3 |
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Google 83660 moabualr 202u IPv4 0x...b7 0t0 TCP 10.35.164.227:52239->72.21.81.48:http (CLOSE_WAIT) Google 83660 moabualr 203u IPv4 0x...b7 0t0 TCP 10.35.164.227:52240->72.21.81.48:http (CLOSE_WAIT) |
- fuser command
fuser command is used to show which processes are using a specified file, file system, or unix socket. I personally used it but I could not get any useful results. Let me know if I am missing anything here in the comments section. Here is the syntax…
1 |
fuser 80/tcp |
- ss command
ss (i.e. Socket Statistics) command is available on Linux. It is used to display useful network socket information. Here is an example…
1 |
ss -lpntu |
The output should be something like….
1 2 3 4 |
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=156,fd=3)) tcp LISTEN 0 128 :::22 :::* users:(("sshd",pid=156,fd=4)) tcp LISTEN 0 128 :::8443 :::* users:(("lxd",pid=123,fd=19)) |
- Port scanners
Depending on the operating system, we can use tools specifically developed to scan ports. For example, on Mac, we can use the Network Utility app. Take a look at the following screenshot.
I hope this summary was useful. Thanks for visiting.
About Author
Mohammed Abualrob
Software Engineer @ Cisco