0

How do I find the process that is listening on port 'xxxx' and kill it?

I have RemoteForward 2022 remote.host:22 in my .ssh/config file.

Whenever I ssh into a remote server: ssh staging-environment I am getting this error: Warning: remote port forwarding failed for listen port 2022

I need this port open for port forwarding.

How do I find the process that is listening on port 2022 and kill it?

UPDATE:

Output of netstat -tulpn

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:81                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:83                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:84                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:85                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:86                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      -                   
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      -                   
tcp        0      0 127.0.0.1:2022              0.0.0.0:*                   LISTEN      -                   
tcp        0      0 127.0.0.1:2023              0.0.0.0:*                   LISTEN      -

4 Answers 4

1

The correct command to see who is using port 2022 is

  sudo ss -ntp

First, you should not use nestat, which is by now obsolete (if you are on Linux, as I believe you are, but if you are on Unix just forget this); second, you should run the command as sudo, otherwise you will not have the authority to display the processes using the port; third, you should definitely not use the flag u, because this means looking at UDP ports, while you are getting an error on ssh, which is most definitely using TCP, not UDP.

0

You can use a couple methods to do this. netstat is a command line tool which should do what you want. A command line example would be something like:

# netstat -tulpn

This will lest ports on your system and which application is listening to it.

Hope this helps

4
  • I've added the output of netstat. There is no process listed for port 2022. Why would this be?
    – eric f
    Jan 27, 2014 at 16:50
  • I think the problem may have to do with multiple old ssh connections. When I ps -ef | grep ssh I can see multiple sessions created by my user (and if they are listed, that means they are still active right?) and what looks to be some git processes running ...
    – eric f
    Jan 27, 2014 at 16:53
  • That could be the case. If you have old connections dotted about they would need to be pointed at something. If you want to start digging into your ssh connections this might help you. stackoverflow.com/questions/17199089/… Jan 27, 2014 at 16:56
  • Out of interest have you checked to see if anything is already listed for it under your router? Might have left yourself a note saying what it is. Jan 27, 2014 at 17:00
0

The way I am currently getting around this error is to log into my server and ps -ef | grep ssh and kill all of the sessions spawned by my user.

0

Just a quick one-liner will do it

for pid in `netstat -tunpl | awk '{print $4, $7}' | grep ':2022' | awk '{print $2}' | cut -d '/' -f1`; do kill -9 $pid; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .