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.
This command returns a list of open processes on this port.
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…
[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.
2 thoughts on “Find process using port number in Bash”
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
It didn’t kill for me initially. I had to use the flag “-9”, e.g.
`kill -9 541`