0

I've established a port forwarding using an ssh connection to my remote server using keygen as codes below :

ssh-keygen -t rsa and ssh-copy-id -i ~/.ssh/id_rsa.pub root@IP

ssh -N -R 0.0.0.0:53407:localhost:53407 root@IP

Now i need to end this but still my port 53407 is using by ssh , how can i end this connection?

return of lsof -i -P -n | grep LISTEN

Ive deleted the port forwarding service yet nothing changed

2
  • 1
    (1) "Ive deleted the port forwarding service" -- What exactly do you mean? (2) When asking about SSH, in general it's good to explicitly state for each command if it was invoked client-side or server-side. Your question fails to do this. I especially mean lsof that looks like it has been invoked on the server, but we cannot be sure until you tell us. (3) To terminate port forwarding set up by ssh -N ... usually you just need to kill the ssh process. Did you do that? Is the issue because sshd did not notice? Commented Aug 16, 2023 at 8:21
  • 1
    (4) Please don't post images of text. Improve the question by editing it. Add information there, not in comments. Commented Aug 16, 2023 at 8:23

1 Answer 1

1
  • In the "dead" terminal, where you started the ssh -N ... you can just press Ctrlc to cancel the ssh process.
  • If you sent it to the background get the job back to the foreground to send it a ^C.
# At the client system:

# To find it:
jobs
[1]+  Running                 ssh -N -R53407:localhost:53407 user@server &

# To bring to foreground:
fg 1

# To cancel:
^c
  • Else find the ssh process in your task list, get its process-id (PID) and execute:
kill <PID>

Using kill like this is not an assassin at work but more like a nice postman that hands out the message to ssh to quit itself properly. — So give ssh some seconds to do the quit-job.


Preferably you should check the open ports by ss:

# At the remote system:
sudo ss -tanp | grep "sshd"

This will also help, when you are local at the server to terminate the ssh connection:

# Alternatively at the server:
sudo ss -tanp | grep ":53407"

LISTEN  0       128                 127.0.0.1:53407     0.0.0.0:*       users:(("sshd",pid=247750,fd=9))                                               
LISTEN  0       128                     [::1]:53407        [::]:*       users:(("sshd",pid=247750,fd=8))                                               

# Get the PID from the output and send the kill command:
kill 247750

Note: Other sshd connections will not quit, as sshd forks for each connection, so using a different PID for each of them.

You must log in to answer this question.

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