Check Ports in use or Listening Ports on Linux

check ports in use

While troubleshooting network connectivity or some application-specific issues, one of the first things to check should be what ports are actually in use on your system and which application is listening on a specific port.

This article explains how to use the netstat, ss and lsof commands to find out which services are listening on which of your ports. The instructions are applicable for all Linux and Unix-based operating systems like ubuntu or macOS.

What is Listening Port

Network port is identified by its number, the associated IP address, and type of the communication protocol, such as TCP or UDP.

Listening port is a network port on which an application or process listens on, acting as a communication endpoint.

Each listening port can be open or closed (filtered) using a firewall. In general terms, an open port is a network port that accepts incoming packets from remote locations.

You can’t have two services listening to the same port on the same IP address.

For example, if you are running an Apache web server that listens on ports 80 and 443 and you try to install Nginx , the later will fail to start because the HTTP and HTTPS ports are already in use.

Check Listening Ports with netstat

Well, netstat is a command-line tool that can provide information about your network connections.

To list all TCP or UDP ports that are being listened on, including the services using the ports and the socket status we will use the following command:

$ sudo netstat -tunlp

You must be wondering the purpose of the options used in this command. So, lets have a look on them:

  • -t - Show’s TCP ports.
  • -u - Show’s UDP ports.
  • -n - Show’s numerical addresses instead of resolving hosts.
  • -l - Show’s only listening ports.
  • -p - Show’s the PID and name of the listener’s process. This information is shown only if you run the command as root or sudo user.

The output will look something like this:

terminal

The important columns in our case are:

  • Proto - The protocol used by the socket.
  • Local Address - The IP Address and port number on which the process listen to.
  • PID/Program name - The PID and the name of the process.

If you want to filter the results, use the grep command . For example, to find what process listens on TCP port 22 you would type:

$ sudo netstat -tnlp | grep :22

The output shows that on this machine port 22 is used by the SSH server:

terminal

If the output is empty it means that nothing is listening on the port.

You can also filter the list based on criteria, for example, PID, protocol, state, and so on.

netstat is obsolete and replaced with ss and ip , but still it is of the most used commands to check network connections.

Check Listening Ports with ss

ss is the new netstat. It lacks some of the netstat features, but exposes more TCP states and it is slightly faster. The command options are mostly the same, so the transition from netstat to ss is not difficult.

To get a list of all listening ports with ss you would type:

$ sudo ss -tunlp

The output is almost the same as the one reported by netstat:

terminal

Check Listening Ports with lsof

lsof is a powerful command-line utility that provides information about files opened by processes.

In Linux, everything is a file. You can think of a socket as a file that writes to the network.

To get a list of all listening TCP ports with lsof type:

$ sudo lsof -nP -iTCP -sTCP:LISTEN

The options used are as follows:

  • -n - Do not convert port numbers to port names.
  • -p - Do not resolve hostnames, show numerical addresses.
  • -iTCP -sTCP:LISTEN - Show only network files with TCP state LISTEN.
terminal

Most of the output columns names are self-explanatory:

  • COMMAND, PID, USER - The name, the pid and the user running the program associated with the port.
  • NAME - The port number.

To find what process is listening on a particular port, for example, port 22 you would use:

$ sudo lsof -nP -iTCP:22 -sTCP:LISTEN

The output shows that ssh service uses port 22:

terminal

For more information, visit the lsof man page and read about all other powerful options of this tool.

Conclusion

Now that I have shown you several commands that you can use to check what ports are in use on your system, and how to find what process listens on a specific port. I think I should very you with the your work

And yes, if you have any questions or remarks, please leave a comment below & in case you like my work then please buy me a coffee.

 by the author.

--

--

--

A web developer nd A web designer with a little bit of craziness

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Fixed Asset Reconciliation Philippines — RCS Soft

Which Version Control System should I choose for a monorepo in 2021?

Boost Android performance using Hermes

Production-ready Kubernetes PaaS in 10 steps.

Arming WordPress for a post-truth era

Continuous Deployment with Jenkins, Docker, Marathon and Mesos on the AWS

The Slow Way is the Best Way

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store