Q. How do I find out which service is listening on a specific port? How do I find out what program is listening on a specific TCP Port?

A. Under Linux and UNIX you can use any one of the following command to get listing on a specific TCP port:
=> lsof : list open files including ports.

=> netstat : The netstat command symbolically displays the contents of various network-related data and information.

lsof command example

Type the following command to see IPv4 port(s), enter:
# lsof -Pnl +M -i4
Type the following command to see IPv6 listing port(s), enter:
# lsof -Pnl +M -i6
Sample output:

gweather- 6591     1000   17u  IPv4 106812       TCP> (ESTABLISHED)
firefox-b 6613     1000   29u  IPv4 106268       TCP> (ESTABLISHED)
firefox-b 6613     1000   31u  IPv4 106321       TCP> (ESTABLISHED)
firefox-b 6613     1000   44u  IPv4 106325       TCP> (ESTABLISHED)
firefox-b 6613     1000   50u  IPv4 106201       TCP> (ESTABLISHED)
deluge    6908     1000    8u  IPv4  23179       TCP *:6881 (LISTEN)
deluge    6908     1000   30u  IPv4  23185       UDP *:6881 
deluge    6908     1000   45u  IPv4 106740       TCP> (SYN_SENT)
deluge    6908     1000   57u  IPv4  70529       TCP> (ESTABLISHED)
deluge    6908     1000   58u  IPv4 106105       TCP> (ESTABLISHED)
ssh       6917     1000    3u  IPv4  23430       TCP> (ESTABLISHED)

First column COMMAND – gives out information about program name. Please see output header for details. For example, gweather* command gets the weather report weather information from the U.S National Weather Service (NWS) servers (, including the Interactive Weather Information Network (IWIN) and other weather services.

  1. -P : This option inhibits the conversion of port numbers to port names for network files. Inhibiting the conver-
    sion may make lsof run a little faster. It is also useful when port name lookup is not working properly.
  2. -n : This option inhibits the conversion of network numbers to host names for network files. Inhibiting conversion may make lsof run faster. It is also useful when host name lookup is not working properly.
  3. -l : This option inhibits the conversion of user ID numbers to login names. It is also useful when login name lookup is working improperly or slowly.
  4. +M : Enables the reporting of portmapper registrations for local TCP and UDP ports.
  5. -i4 : IPv4 listing only
  6. -i6 : IPv6 listing only

netstat command example

Type the command as follows:
# netstat -tulpn
# netstat -npl

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp        0      0  *               LISTEN     6908/python         
tcp        0      0 *               LISTEN     5562/cupsd          
tcp        0      0*               LISTEN     6278/(squid)        
tcp        0      0  *               LISTEN     5854/exim4          
udp        0      0 *                          6278/(squid)        
udp        0      0  *                          6278/(squid)        
udp        0      0    *                          4583/dhclient3      
udp        0      0  *                          6908/python   

Last column PID/Program name gives out information regarding program name and port.

  • -t : TCP port
  • -u : UDP port
  • -l : Show only listening sockets.
  • -p : Show the PID and name of the program to which each socket / port belongs
  • -n : No DNS lookup (speed up operation)

/etc/services file

/etc/services is a plain ASCII file providing a mapping between friendly textual names for internet services, and their underlying assigned port numbers and protocol types. Every networking program should look into this file to get the port number (and protocol) for its service. You can view this file with the help of cat or less command:
$ cat /etc/services
$ grep 110 /etc/services
$ less /etc/services

Further readings:

  • man pages – lsof, nmap, services, netstat

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 14 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
14 comments… add one
  • Claudio Miranda Feb 20, 2008 @ 20:36

    netstat offers other argument to better visualize process and ports, inclusive root programs.

    sudo netstat -antpuew

  • pinnacle Feb 20, 2008 @ 20:45

    Hey Claudio

    FreeBSD 6.3 – netstat -antpuew
    netstat: uew: unknown or uninstrumented protocol

    Any idea? lsof is only working :(

  • Claudio Miranda Feb 21, 2008 @ 12:31

    pinnacle, the previous netstat arguments is available for linux, solaris and freebsd needs to use pfiles and lsof, respectively. Sorry, I didn’t say before.

  • Gagan Brahmi Jan 15, 2009 @ 8:19

    @ pinnacle

    The following is your command:

    # sockstat -4 -l

    In case you want to list the IPV6 IP addresses, use the following command:

    # sockstat -6 -l

  • pierpaolo1 May 20, 2009 @ 7:04

    I have 2 ports without a process; here is part of my netstat -tulpn:

    tcp 0 0* LISTEN 0 8491 –

    udp 0 0* 0 8487 –

    I tried with lsof and fuser but there is no way to find the owner of these ports.
    Should I worry about it?

    • 🐧 nixCraft May 20, 2009 @ 8:20

      Run netstat as root user. Than you will get PID in 2nd last output filed. Run lsof as follows on that PID:
      lsof -p PID

  • pierpaolo1 May 20, 2009 @ 10:40

    It was as root user.
    As you can see, other ports gave me PID and daemon name:

    tcp 0 0* LISTEN 3012/mysqld
    tcp 0 0* LISTEN 3141/smbd
    tcp 0 0* LISTEN –
    tcp 0 0* LISTEN 3052/clamd
    tcp 0 0* LISTEN 2628/portmap

    but only two don’t give me PID or daemon name.

  • Billy Bob Nov 12, 2009 @ 23:42

    I have been looking into this problem too. I found one possible answer in rpcinfo. With this I was able to associate a port with nlockmgr.

    But I still have a port without a process. So if there are any better solutions, please post here again.

  • ragab Jan 1, 2011 @ 5:56

    all commands work well in linux fedora except sockstat

  • Jacob Apr 1, 2012 @ 16:56

    Hello I want to track this.
    tcp 0 0* LISTEN 3583/java

    I see the program but I dont know where’s its located. Anyone can help me find the location of the file or where its going from? I really need help.

  • Jacob Apr 1, 2012 @ 19:06

    Sorry its tcp 0 0* LISTEN 3583/java

  • PVPcTutorials Aug 20, 2012 @ 11:26

    @Jacob TCP-Port 25565 is Minecraft. And since Minecraft is run with java this is probably the only possible answer.

  • Farhad Mar 14, 2013 @ 12:03

    Hello sir,
    I am writing an SNMPv3 code in Linux using C,and in my socket programming I used PORT No 161.
    It shows me an error like:”Permission Denied”.
    What should I do to enable my code to work in 161 standard port?

  • LD Sep 10, 2017 @ 10:42

    Farhad, you can’t use ports under 1024 without root

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum