9

I'm using this ssh command:

ssh -fnN -L $LOCALPORT:$REMOTEHOST:$REMOTEPORT $HOST

This creates my ssh connection in the background and forwards that port to the remote host through another host. This is great, and allows me to use my service behind a firewall by connecting to a server which can access both networks.

I need to stop this ssh connection when done, and I'm not sure how.

2 Answers2

3

Find the PID with the ps command and send it a QUIT signal with the kill command.

Find the PID with:

ps -o pid,cmd | grep ssh

Send the QUIT signal with:

kill -QUIT <pid>
Elijah Lynn
  • 151
  • 6
  • 17
virullius
  • 1,058
  • I was thinking there was something more to it. Probably overthinking it. Thanks. – Mnebuerquo Jan 05 '18 at 13:12
  • 1
    this didn't work for me, but this other answer did: https://superuser.com/questions/87014/how-do-i-remove-an-ssh-forwarded-port/87019 – nsheff May 23 '19 at 15:39
2

The best solution I found is following to kill all the tunnels:

ps -o pid,cmd|grep "ssh -L"|grep -v grep|awk '{print $1}'|xargs kill

  1. Gets id and command of all processes
  2. Filter for ssh -L
  3. gets the process id and kills it