How to find the PID of the process that is using specific port ?
This tutorial guides you on how to find the PID of the process that is using specific port. As you know that a port can be used by a single application at any point of time. Sometimes we may need to find which process is listening on specific port.
Find the PID of the process that is using specific port
In my case I had installed VNC server and started many VNC server sessions. And I wanted to kill the VNC process manually using PID and free the port which need to be used by another instance. Therefore it is important to find the PID of the process that is using specific port.
In this tutorial let’s see various approaches to find the process listening on a particular port in Ubuntu Linux.
Using netstat command
The netstat command is part of net-tools package. And it is widely used for this purpose. Let’s see how to use netstat command to list all TCP listening sockets, TCP connections, UDP connections with program ID or program name as shown below.
$ netstat -ltnup (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:35993 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:59003 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:46305 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:5901 0.0.0.0:* LISTEN 17186/Xtigervnc tcp 0 0 127.0.0.1:5902 0.0.0.0:* LISTEN 17737/Xtigervnc tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:49267 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN - -------- ---------
Note, the meaning of the options that we are using in the netstat command is below.
- l – List only listening sockets
- t – List TCP connections
- n – addresses in a numerical form
- u – List UDP connections
- p – List PID/Program name
You can also use grep command to filter and check the PID information for the specific port alone.
For example,
$ netstat -ltnup | grep ':59' (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 0.0.0.0:59003 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:5901 0.0.0.0:* LISTEN 17186/Xtigervnc tcp 0 0 127.0.0.1:5902 0.0.0.0:* LISTEN 17737/Xtigervnc tcp6 0 0 :::5900 :::* LISTEN - tcp6 0 0 ::1:5901 :::* LISTEN 17186/Xtigervnc tcp6 0 0 ::1:5902 :::* LISTEN 17737/Xtigervnc
lsof Command to find PID
The lsof command can be used list all open files.
For example,
$ lsof -i :5901 -i :5902 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME Xtigervnc 17186 ubuntu 7u IPv4 176047 0t0 TCP localhost:5901 (LISTEN) Xtigervnc 17186 ubuntu 8u IPv6 176048 0t0 TCP ip6-localhost:5901 (LISTEN) Xtigervnc 17737 ubuntu 7u IPv4 181502 0t0 TCP localhost:5902 (LISTEN) Xtigervnc 17737 ubuntu 8u IPv6 181503 0t0 TCP ip6-localhost:5902 (LISTEN)
Use ss tool to get PID information
ss is another tool that can be used to investigate sockets and process information.
For example,
$ ss -ltnup 'sport = :5901' Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process tcp LISTEN 0 5 127.0.0.1:5901 0.0.0.0:* users:(("Xtigervnc",pid=17186,fd=7)) tcp LISTEN 0 5 [::1]:5901 [::]:* users:(("Xtigervnc",pid=17186,fd=8)) u$ ss -ltnup 'sport = :5902' Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process tcp LISTEN 0 5 127.0.0.1:5902 0.0.0.0:* users:(("Xtigervnc",pid=17737,fd=7)) tcp LISTEN 0 5 [::1]:5902 [::]:* users:(("Xtigervnc",pid=17737,fd=8))
Using fuser
You can also use fuser to identify processes using files and sockets in Linux system.
For example,
$ fuser -v 5901/tcp USER PID ACCESS COMMAND 5901/tcp: ubuntu 17186 F.... Xtigervnc $ fuser -v 5902/tcp USER PID ACCESS COMMAND 5902/tcp: ubuntu 17737 F.... Xtigervnc
That’s it. You had learnt numerous approaches to get the process information of the process that is listening on a specific port.
Hope this article is helpful 🙂
Also See:
- Set or change root password in Ubuntu Linux
- Linux: sudo: apt-get: command not found
- How to Start Stop Restart MariaDB on Linux OS ?
- Putty Fatal Error No supported authentication methods available
- How to find which users belongs to a specific group in linux
- Give write permissions for specific user or group for specific folder in linux
- How to unzip a zip file from Terminal (Google Cloud Shell)
- Build a Docker Image with a Dockerfile and Cloud Build in GCP?
- How to create GCP project on Google Cloud Platform
- MariaDB – How to set max_connections permanently ?
- How to create GCP project on Google Cloud Platform
- Is it possible to change Google Cloud Platform Project ID ?
- Create non-root SSH user account and provide access to specific folders
- MySQL : How to grant all privileges to the user on database ?
- How to install OpenJDK 11 in Ubuntu Machine ?
- Amazon Linux AMI : apt-get command not found
- How to get the first and last element of a list in Python ?
- Connect to Github using SSH ?
- Check OS version in Linux from command line ?
- Error: You’ll have to kill the Xvnc process manually
- Install and configure VNC on Ubuntu 20.04 with graphical desktop?