How to find and kill a port

Photo by Markus Winkler on Unsplash

This is a simple approach which we need on daily basis which is find and kill a port.

Following are the steps to find and kill the port.

  1. You should find the process id which is known as pid using the port as follows.
sudo lsof -i :<port_id>

Following is a sample output for sudo lsof -i :3000

docker-pr 299618 root 4u IPv4 3950094 0t0 TCP *:3000 (LISTEN)

2. Then get the pid which is bolded in the above example and kill it as follows. kill [option] <pid>

kill -15 299618   // -15 for give time to safely cleanup and die.

or you can simply kill it without give time to cleanup.

kill -9 299618

Okay what if I don’t know what is the port id? Then list all the ports and find it as follows.

sudo lsof -nP -iTCP -sTCP:LISTEN 

Important: If you don’t know the port but you know process name or anything you can simply use grep using pipe and filter out as follow.

sudo lsof -nP -iTCP -sTCP:LISTEN | grep mysqld

Above will filter processes with mysqld as follows.

Hope this will be helpful.

If you have found this helpful please hit that 👏 and share it on social media :).




Technical Writer | Tech Enthusiast | Open source contributor

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

Recommended from Medium

Running a script continously on Linux

Redundant server setup

Why your great trading idea is secondary at first

How to Improve Hotspot Data Hashing on an Elastic Cache Platform

Why Interviewing as a Developer Is Complete Nonsense

[Android] Using Dagger 2 with Kotlin — Part1: Field Injection

7 Best Practices That Help To Avoid Common Data Management Mistakes

Industry 4.0 and Why Semantic Web is Crucial for It.

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
Yashod Perera

Yashod Perera

Technical Writer | Tech Enthusiast | Open source contributor

More from Medium

Avoiding common mistakes in your Ansible Playbooks

Did I really just commit and push a file with a password in it?-Remove

The dangers of mutability in Python

Can we run docker inside the docker container??