Linux
To check the listening ports and applications on Linux:
- Open a terminal application i.e. shell prompt.
- Run any one of the following command on Linux to see open ports:
# Variante 1
sudo lsof -i -P -n
Consider the following line from above outputs:
sshd 85379 root 3u IPv4 0xffff80000039e000 0t0 TCP 10.86.128.138:22 (LISTEN)
- sshd is the name of the application.
- 10.86.128.138 is the IP address to which sshd application bind to (LISTEN)
- 22 is the TCP port that is being used (LISTEN)
- 85379 is the process ID of the sshd process
# Variante 2
sudo ss -tulwn
Where ss command options are as follows:
- -t : Show only TCP sockets on Linux
- -u : Display only UDP sockets on Linux
- -l : Show listening sockets. For example, TCP port 22 is opened by SSHD server.
- -p : List process name that opened sockets
- -n : Don’t resolve service names i.e. don’t use DNS
Windows
netstat -a
PS-1: To list any process listening to the particular port, for example 8080, in Linux :
lsof -i:8080
PS-2: To kill a process listening on a particular port, for example 8080 (in Linux):
kill -9 $(lsof -t -i:8080)
PS: -9
corresponds to the SIGKILL - terminate immediately/hard kill
. It implies the process will be killed forcefully.