How to find out which process is listening upon a port

When we covered port scanning a short while ago we discovered how to tell which ports had processes listening upon them, via port scanning. What we didn‘t do was learn how to tell which processes were associated with each open port.

Often you‘ll know which applications are going to be using a particular port, because it‘s the standard one, or because you know you set it up.

For example when you see something listening upon port 25 you tend to expect it to be a mailserver, and similarly if you find something listening on port 80 you‘ll not be suprised to discover it‘s a webserver.

Sometimes though these assumptions can be mistaken, and other times you‘ll discover an open port which you simply don‘t recognise. If you‘re examing a machine you‘re not sure you trust fully it‘s worth checking exactly which processes are really running.

As we noted in the the introduction to port scanning with nmap you can lookup which service uses any of the "standard" ports by referring to the file /etc/services.

For example we can open that file in our favourite editor, or pager, and see that port 43/tcp is associated with "whois", and that port 53 is associated with DNS.

These don‘t help you much if you have a service which has had it‘s default port changed - something some people suggest you do as a means of increasing security. (Personally I believe such misdirection is misguided at best, and counter-productive at worst).

What you really need to do is to lookup the process which is currently bound to the given network port. Thankfully this is a simple job with use of the lsof package.

If you don‘t have lsof already you can download and install it by becoming root and running:

[email protected]:~# apt-get install lsof

This will download and install the package for you, along with any dependencies which might be required:

Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
  lsof
0 upgraded, 1 newly installed, 0 to remove and 16 not upgraded.
Need to get 339kB of archives.
After unpacking 549kB of additional disk space will be used.
Get:1 http://http.us.debian.org unstable/main lsof 4.75.dfsg.1-1 [339kB]
Fetched 339kB in 3s (90.8kB/s)
Selecting previously deselected package lsof.
(Reading database ... 69882 files and directories currently installed.)
Unpacking lsof (from .../lsof_4.75.dfsg.1-1_i386.deb) ...
Setting up lsof (4.75.dfsg.1-1) ...

Once you have the package installed you can now discover precisely which processes are bound upon particular ports.

If you have the Apache webserver running on port 80 that will provide a suitable test candidate. If not you can choose another port you know is in use.

To discover the process name, ID (pid), and other details you need to run:

lsof -i :port

So to see which process is listening upon port 80 we can run:

[email protected]:~# lsof -i :80

This gives us the following output:

COMMAND   PID     USER   FD   TYPE   DEVICE SIZE NODE NAME
apache2 10437     root    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10438 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10439 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10440 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10441 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 10442 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 25966 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)
apache2 25968 www-data    3u  IPv6 22890556       TCP *:www (LISTEN)

Here you can see the command running (apache2), the username it is running as www-data, and some other details.

Similarly we can see which process is bound to port 22:

[email protected]:~# lsof -i :22
COMMAND   PID USER   FD   TYPE   DEVICE SIZE NODE NAME
sshd     8936 root    3u  IPv6 12161280       TCP *:ssh (LISTEN)

To see all the ports open for listening upon the current host you can use another command netstat (contained in the net-tools package):

[email protected]:~# netstat -a |grep LISTEN |grep -v unix
tcp        0      0 *:2049                  *:*                     LISTEN
tcp        0      0 *:743                   *:*                     LISTEN
tcp        0      0 localhost.localdo:mysql *:*                     LISTEN
tcp        0      0 *:5900                  *:*                     LISTEN
tcp        0      0 localhost.locald:sunrpc *:*                     LISTEN
tcp        0      0 *:8888                  *:*                     LISTEN
tcp        0      0 localhost.localdom:smtp *:*                     LISTEN
tcp6       0      0 *:www                   *:*                     LISTEN
tcp6       0      0 *:distcc                *:*                     LISTEN
tcp6       0      0 *:ssh                   *:*                     LISTEN     

Here you can see that there are processes listening upon ports 2049, 743, 5900, and several others.

(The second grep we used above was to ignore Unix domain sockets).

If you‘re curious to see which programs and services are used in those sockets you can look them up as we‘ve already shown:

[email protected]:~# lsof -i :8888
COMMAND   PID    USER   FD   TYPE   DEVICE SIZE NODE NAME
gnump3d 25834 gnump3d    3u  IPv4 61035200       TCP *:8888 (LISTEN)

This tells us that the process bound to port 8888 is the gnump3d MP3 streamer.

Port 2049 and 743 are both associated with NFS. The rest can be tracked down in a similar manner. (You‘ll notice that some ports actually have their service names printed next to them, such as the smtp entry for port 25).

lsof is a very powerful tool which can be used for lots of jobs. If you‘re unfamiliar with it I recommend reading the manpage via:

man lsof

If you do so you‘ll discover that the -i flag can take multiple different types of arguments, to allow you to check more than one port at a time, and use IPv6 addresses too.

It‘s often used to see which files are open upon mounted devices, so you can kill the processes and unmount them cleanly.

时间: 2024-08-07 00:08:57

How to find out which process is listening upon a port的相关文章

Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To sta

eclipse出现:Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the

Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.

原文出处: https://blog.csdn.net/hyunbar/article/details/80111947 运行 supervisord -c /etc/supervisor/supervisord.conf 出现错误 Starting supervisor: Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut th

进程ID[PID(Process ID)]与端口号[(Port ID)]的联系

1.首先声明一点:PID不是端口(port id),而是Process ID进程号的意思. 2.那么,什么是进程号? 采集网友的意见就是: 进程号,是系统分配给么一个进程的唯一标识符.PID就是各进程的身份标识符,程序一运行系统就会自动分配给进程一个独一无二的PID.进程终止后,PID被系统回收,可能会被继续给新运行的程序. 3.深入理解二者关系: 一个程序一个端口: 一个端口可以有多个进程: (顺便提一下:一个进程可以有多个线程(将一个进程的系统资源共享给一个程序内多道分程序,并行运行,以达到

Tomcat服务器重启失败:The server may already be running in another process, or a system process may be using the port.

在控制台重启Tomcat服务器,报错如下: 原因分析: You've another instance of Tomcat already running. You can confirm this by going to http://localhost:8080 in your webbrowser and check if you get the Tomcat default home page or a Tomcat-specific 404 error page. Both are e

supervisord 启动失败 Error: Another program is already listening on a port that one of our HTTP serve...

Linux系统中 Supervisor 配置守护进程: 启动Supervisor 服务语句: supervisord -c /etc/supervisor/supervisord.conf 这个过程可能会失败,错误如下: 解决办法: 执行下面语句 unlink /var/run/supervisor.sock unlink /tmp/supervisor.sock 这个错误的原因就是supervisor.sock 这个文件会被系统自动删除或者其它原因不存在了,删除软连接就可以了. supervi

Port Forwarding in Windows

转自:http://woshub.com/port-forwarding-in-windows/ Since Windows XP there is a built-in ability in Microsoft Windows to set up network ports forwarding. Due to it, any incoming TCP connection (IPv4 or IPv6) to local port can be redirected to another lo

Kickstart Options

The following options can be placed in a kickstart file. If you prefer to use a graphical interface for creating your kickstart file, use the Kickstart Configurator application. Refer to Chapter 29, Kickstart Configurator for details. Note If the opt

Oracle 11g 新特性 -- Oracle Restart 说明(转载)

转载:http://blog.csdn.net/tianlesoftware/article/details/8435670 一.  OHASD 说明 Oracle 的Restart 特性是Oracle 11g里的新特性,在讲这个特性之前先看一下Oracle 11g RAC的进程.之前的Blog 有说明. Oracle 11gR2RAC 进程说明 http://blog.csdn.net/tianlesoftware/article/details/6009962 Oracle 11gR2 中对

linux cpu和IO优化

系统优化是一项复杂.繁琐.长期的工作,优化前需要监测.采集.测试.评估,优化后也需要测试.采集.评估.监测,而且是一个长期和持续的过程,不 是说现在优化了,测试了,以后就可以一劳永逸了,也不是说书本上的优化就适合眼下正在运行的系统,不同的系统.不同的硬件.不同的应用优化的重点也不同. 优化的方法也不同.优化的参数也不同.性能监测是系统优化过程中重要的一环,如果没有监测.不清楚性能瓶颈在哪里,怎么优化呢?所以找到性能 瓶颈是性能监测的目的,也是系统优化的关键.系统由若干子系统构成,通常修改一个子系