Linux find which process port

How to know what program is listening on a given port?

I suspect a program is listening on port 8000 on my machine. When I run the following command, I get this error:

> python -m SimpleHTTPServer # Lots of python error socket.error: [Errno 98] Address already in use 

If I use another port ( 8000 is the default), the web server runs fine. If I run wget localhost:8000 from the command line, it returns 404 Not Found . What can I do (or what tools are available) to find what program is listening on port 8000 , and from there where that program is configured?

8 Answers 8

Open your terminal and type as

that command will list you the application used by that port with PID. (If no results run via sudo since your might have no permission to certain processes.)

For example, with port 8000 ( python3 -m http.server ):

$ lsof -i :8000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python3 3269 user 3u IPv4 1783216 0t0 TCP *:8000 (LISTEN) 
$ sudo lsof -i :22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 998 root 3u IPv4 1442116 0t0 TCP *:ssh (LISTEN) sshd 998 root 4u IPv6 1442118 0t0 TCP *:ssh (LISTEN) 

@Imray the example searches for port 8881. The PID column contains the process IDs and the NAME column contains the ports.

You can use netstat to see which process is listening on which port.

You can use this command to have a full detail :

if you need to know exactly which one is listening on port 8000 you can use this :

sudo netstat -peanut | grep ":8000 " 

There is no process that can hide from netstat.

To kill/end the process use kill where is the process id displayed in the last column of netstat ‘s output. Eg. kill 31612 .

To expound on the answer by @33833 you can get some very detailed info, for example:

$ lsof -i :8000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME squid3 1289 proxy 15u IPv6 14810490 0t0 TCP *:8000 (LISTEN) $ ps -fp 1289 UID PID PPID C STIME TTY TIME CMD proxy 1289 1 0 09:48 ? 00:00:00 /usr/sbin/squid3 -N -f /etc/squid-deb-proxy/squid-deb-proxy.conf 

I can see right there that squid is the process, but it is actualy my squid-deb-proxy that is taking up the port.

Another good example of a java app:

$ lsof -i :4242 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 3075 root 86u IPv4 12019 0t0 TCP *:4242 (LISTEN) $ ps -fp 3075 UID PID PPID C STIME TTY TIME CMD root 3075 1 15 May24 ? 3-16:07:25 /usr/local/crashplan/jre/bin/java -Dfile.encoding=UTF-8 -Dapp=CrashPlanService -DappBaseName=CrashPl 

You can see in lsof (LiSt Open Files) that it is java, which is less than helpful. Running the ps command with the PID we can see right away that it is CrashPlan.

Источник

Check Which Process Is Using a Port on Linux

In computer networking, a port represents a logical entry and exit point for a connection. Ports are based on software and are entirely virtual. These ports on a computer are managed by the operating system.

Читайте также:  1с ошибка при создании информационной базы linux

What Will We Talk About?

This quick tutorial demonstrates the various methods to determine which Linux process or service is currently listening on a specific port. Let’s talk about ports and their purpose.

How Are Ports Analogous to Physical Ports?

Just as physical ports help to interact with various peripheral devices connected to a computer, ports help the different services to communicate with each other. These services can be on the same computer or on different computers.

A Bit About Port of a Service

To listen for incoming connection requests, a process associates itself with a port number. Most processes are set up with a default port, and they have to use that port as per their specification. They do not automatically switch to the other port unless their configuration is explicitly modified.

A few examples of protocols and their associated default ports include the Secure Shell (SSH) protocol (port22), the Apache HTTP (port80), the MySQL database server (port3306), and so forth. You may use this information to discover which default port does a service utilizes.

The config file of these services can be edited to use some other port as well.

Checking the Ports on Linux

Let’s now see how to check what port/ports a process is using on Linux. Here, we will show you the different commands for this purpose.

1. Lsof Command

The lsof utility is helpful to obtain a list of the ports which are used by your system. Let’s consider the following example to get an information about a process (processes) using the TCP port 22:

The lsof command gives more information like the user’s name and what process IDs are linked to each process. It works with both TCP and UDP ports.

2. SS Command

The ss command is another way to find out which processes are linked to a certain port. Although lsof is the more common abbreviation, some people may find ss to be more handy.

Let’s look for the processes or services that listen on port 3306:

Let’s break down this command:

1. t: It tells the ss command to display the TCP packets.

2. u: It tells the ss command to display the UDP packets.

3. n: It is used to display the port numbers instead of their translations.

4. a: It is used to display the listening as well as non-listening sockets of all types.

5. p: It is used to display the processes that utilize a socket.

The result of the previous command shows which process is utilizing which port. You may also issue the following command:

Here, sport signifies the source port.

These two approaches may help you find the IDs of the processes that are connected to different ports.

3. Netstat Command

The netstat command shows the information about your network and can be used to fix the problems or change the way that your network is set up. It can also keep a close watch on your network connections.

This command is often used to see an information about inbound and outbound connections, routing tables, port listening, and usage stats. Although it has been rendered obsolete in recent years, netstat is still a useful tool for analyzing networks.

With the grep command, netstat can determine which process or service is using a certain port (by mentioning the port):

The options used here can be classified as follows:

1. t: It only shows the TCP connection.

Читайте также:  Linux просмотр wifi сетей

2. l: It is used to display the results in a list.

3. n: It displays addresses and port numbers in numerical format.

4. p: It displays the PID and program name which are associated with each socket.

4. Fuser Command

The fuser command determines the processes that utilize the files or sockets. You can use it to list the services which run on a specific port. Let’s take the example of port 3306 and see what services are running here:

This provides us with the process numbers using this port. You can use this process number to find the corresponding process names. For example, if the process number is 15809, the command to use here is as follows:

However, certain tools are required to identify the processes that utilize a non-standard port. “LSOF” is a tool for discovering what services are available on a network and what ports they use. Consider the following example. This shows how to list the UDP and TCP listening ports:

The following is a description of the options that are used here:

1. P: It suppresses the port service name lookup.

2. n: It displays the numeric network addresses.

3. i: It lists the IP sockets.

Both the ports and the associated processes are shown in the previously-mentioned result. This way is particularly useful for processes with non-default ports.

Conclusion

In this article, we talked about four possible Linux command-line tools and provided the examples on how to use them to find out which process is listening on a certain port.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.

Источник

Linux: Find Out Which Port Number a Process is Listening on

Linux Listen port

Linux Listen port

As Linux users, we sometimes need to know what port number a particular process is listening on. All ports are associated with a process ID or service in an operating system. So how do we find this port? In this article, we will present three different methods that you can use to find out which port a process is listening on.

We have run the commands and procedures described in this article on an Ubuntu 22.04 LTS system.

Method 1: Using the netstat command

Netstat, the network statistics utility, is used to display information about the network connections. This includes information about interface statistics, routing tables and much more. This utility is available on most Linux systems, so we use it to find out which ports certain processes on the system are using.

To use the netstat command, you must install the net-tools utility, if it is not already installed on your system, using the following command:

$ sudo apt install net-tools

Install net-tools

Then run the following command:

Run netstat command

The above command gives netstat information based on the following features:

  • l: display only listening sockets
  • t: display tcp connection
  • n: display addresses in a numerical form
  • p: display process ID/ Program name

For example, in the above output of the netstat command, Apache2 program with process ID 950 is running on port number 80.

You can also filter statistics for a specific port by incorporating the grep function into your command.

$ sudo netstat -ltnp | grep -w ':80'

This command will tell you specifically which process is running on port number 80.

Check which program listens on port 80

Method 2: Using the lsof command

The lsof or the List of Open Files utility helps in listing all the open files on your Linux system. We can use this utility to view all processes open on a specific port.

Читайте также:  Pidgin astra linux установка

For using the lsof command, you need to install the lsof utility if it is already not installed on your system through the following command:

Install lsof tool

Let us use lsof to view the service listening on a specific port.

This command will list all processes using TCP port number 80.

Check which application uses port 80 with lsof

Method 3: Using the fuser command

The fuser command displays which process IDs are using the named files, sockets or file systems. We can use this command in order to view process IDs running on a specific TCP port.

For using the fuser command, you need to install the psmisc utility if it is already not installed on your system through the following command:

Install psmisc

Let us view all the process IDs running on TCP port 3306 through the following command:

You can specify any port number in this command to view its listening processes.

Use fuser command

In the above output, you can see that process ID 975 is listening on TCP 3306.

In order to view which program this process ID corresponds to, run the following command:

Check port of a specific process ID

The output shows that process ID 975 corresponds to the program name MySDLd. Thus process ID 975 of the program MySQLd is listening on port number 3306.

Through the three methods you have learned in this article, you can easily view which TCP port a specific process on Linux is listening upon.

About This Site

Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.

Latest Tutorials

Источник

Determine the process pid listening on a certain port

As the title says, I’m running multiple game servers, and every of them has the same name but different PID and the port number. I would like to match the PID of the server which is listening on certain port, and then I would like to kill this process. I need that in order to complete my bash script. Is that even possible? Because it didn’t find yet any solutions on the web.

8 Answers 8

Short version which you can pass to kill command:

This also includes processes that are connected on that port. lsof -i4TCP:80 -sTCP:LISTEN -t is probably what you want, instead.

Exactly what I was looking for. I wanted to kill a process by searching for the port it is running at.

@Nevir what do you mean by » also includes processes that are connected on that port»? Can you please explain?

The -p flag of netstat gives you PID of the process:

*use sudo if showing — instead of PID

Edit: The command that is needed to get PIDs of socket users in FreeBSD is sockstat . As we worked out during the discussion with @Cyclone, the line that does the job is:

sockstat -4 -l | grep :80 | awk '' | head -1 

netstat: 80: unknown or uninstrumented protocol used the 80 (nginx) port for testing purpoes. Not worked.

netstat -p -l | grep $PORT and lsof -i :$PORT solutions are good but I prefer fuser $PORT/tcp extension syntax to POSIX (which work for coreutils ) as with pipe:

it prints pure pid so you can drop sed magic out.

One thing that makes fuser my lover tools is ability to send signal to that process directly (this syntax is also extension to POSIX):

$ fuser -k $port/tcp # with SIGKILL $ fuser -k -15 $port/tcp # with SIGTERM $ fuser -k -TERM $port/tcp # with SIGTERM 

Источник

Оцените статью
Adblock
detector