To find out what program or service is listening on a specific TCP port on a Linux or Unix system, you can use the lsof command. lsof stands for "list open files", and it is a utility that lists the open files on the system, along with the processes that have these files open.
To use lsof to find out what program is listening on a specific TCP port, you can use the -i flag to specify the network interface and the port number, and the -n flag to disable name resolution.
For example, to find out what program is listening on TCP port 80 (the default HTTP port), you can use the following command:
$ lsof -i :80 -n COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 13362 root 6u IPv4 19987 0t0 TCP *:http (LISTEN)
This lsof command will display the process that is listening on TCP port 80, along with its PID, command name, and other information. In this example, the nginx web server is listening on TCP port 80.
Alternatively, you can use the netstat command with the -tulpn flags to list all the listening TCP ports on the system, along with the corresponding programs and PIDs.
For example:
$ netstat -tulpn Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1234/sshd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13362/nginx
This netstat command will display all the listening TCP ports on the system, along with the program names and PIDs. You can use the grep command to filter the output and display only the desired port.
For example:
$ netstat -tulpn | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13362/nginx
Both the lsof and netstat commands are useful tools for finding out what programs or services are listening on specific ports on a Linux or Unix system.