Linux List The Open Ports And The Process That Owns Them

So how do you list the network open ports on your Linux server and the process that owns them? The answer is simple. Use the following command (must be run as the root user):

ADVERTISEMENTS


sudo lsof -i
sudo netstat -lptu
sudo netstat -tulpn

Sample outputs (see video demo):

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
12 comments… add one
  • Jeff Schroeder May 30, 2008 @ 12:50

    Shameless plug to a really good article I wrote about lsof and some of the more nifty tricks:

    Troubleshooting running systems with lsof

    • Ankita Nautiyal Apr 21, 2015 @ 6:24

      Hi,

      We recently did patching in our linux servers and want to know which process is using which patch. Do we have any linux command to know that.

      Thanks in advance
      Ankita

  • Mark Nixon May 30, 2008 @ 18:42

    Thanks Vikrant for the useful tip…I’ve used lsof for ages but sort of taken it for granted,didn’t check the man pages, and wasn’t aware of this option.

    Great (!!!) site, by the way, I’ll start contributing my own tips as well. Thanks for giving me lots of ideas-projects to increase my understanding of Linux/BSD.

  • harsha gowda Jul 10, 2008 @ 11:36

    Thanks

  • alojamento de sites Jul 22, 2010 @ 12:29

    Very useful, thanks

  • paul Mar 31, 2011 @ 3:08

    What should I do when netstat lists the PID as “-” and lsof doesn’t list anything for that port? I know something is listening on that port because “nc -z -v -w1 1-65534” says so.

  • Jeff Schroeder Mar 31, 2011 @ 6:35

    @paul: did you run the commands as root?

  • paul Mar 31, 2011 @ 17:45

    @jeff

    Thanks for the reply! Yes, I did run all the commands as root. I ran nc from both a different machine on the network and also the same machine that has the port open.

    Any ideas?

  • Mark Nett Jan 5, 2012 @ 11:51

    Running sudo lsof -i gives me command not found?

    • Sarjeet Singh Aug 13, 2013 @ 23:38

      Can you try “which lsof” and see if it tells the path. If it does, try using the full path and see if this works?

      If not, try installing using apt-get install lsof and try then.

  • sob Mar 28, 2012 @ 12:33

    Thanks!

  • Jim Aman Feb 28, 2013 @ 19:59

    What music is played in the video “Linux List The Open Ports And The Process That Owns Them”
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.