Find process using port number in Bash

Something that has bugged me recently is that when I go to proxy on a particular port, the port is already in use and I get the following error.

bind: Address already in use
channel_setup_fwd_listener: cannot listen to port: 8090
Could not request local forwarding.


So I had a quick look to find out how to locate the process hogging the port. All I need to do is use the lsof command.

lsof -i:8090

This command returns a list of open processes on this port.

Something like…

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ssh 75782 eoin 5u IPv6 0x01c1c234 0t0 TCP localhost:8090 (LISTEN)

To free the port, kill the process using it…
kill 75782

 

[Update: Simpler solution found]

Easier to kill all ssh processes and restart them (if you use terminal aliases!), since in my case it is always an ssh process that is hogging the port.

killall ssh

2 thoughts on “Find process using port number in Bash

  1. Nice tip. I’ve been using:

    ps ax | grep ssh | grep -v grep

    to check for ssh sessions, and killing the process from there.

    Another way to check the port usage is:

    netstat -an | grep 8090

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s