Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got 20 or so machines scattered around the country (RPi's) and they are all on mobile networks (don't have a public IP.) Occasionally, I need to get access to a machine, so I've set up a rescue script that the machine will try to pull down every so often, and I can have that script do a reverse SSH onto my server so I can log in to it. This is working well, but I'm trying to figure out how / where I can kill that session.

Basically, I want to run the same script on all machines (they won't all run at the same time, and they pull down only their specific script anyway). So I want them to connect to my server, where I can then access them by doing something like:

ssh -p 2222 pi@localhost

But even after I connect and exit, this connection persists, which I assume will cause problems for another machine trying to connect as well. I don't want to give each machine its own port number, so I need to figure out how to kill that reverse SSH session. Not sure if it has to be done on the Pi, or on my server. Any ideas?

share|improve this question
+1 for your setup alone :) – terdon Aug 26 '13 at 18:00
What's the command you're using on the remote machines to open the SSH tunnel? I'm guessing the easiest way to close the tunnel will be to do a ps | grep <port_forwarded> on the remote machine and then kill that PID. – ernie Aug 26 '13 at 18:25
If i'm SSH'd directly into the machine (I have a test machine locally) and I run ps, I can see ssh running. However if from the server I ssh into the open tunnel and run ps, I don't see SSH running, therefore have nothing to kill. – McB Aug 26 '13 at 18:45
Sorry, my ps command probably should have been something like ps aux or something that would list all the ssh sessions (and running processes). I'm guessing the user you log in as is different than the user that initiated the tunnel? – ernie Aug 26 '13 at 18:48
What I've got working now, and I guess this is the easiest way to do it, is from the server, after having connected via the command above, I run kill $(pidof ssh) and that ends the sessions and frees up the port. – McB Aug 26 '13 at 19:37
up vote 0 down vote accepted

Got it working by doing this:

kill $(pidof ssh)
share|improve this answer
This will kill all opened sessions, not just the current one. I guess this is fine in your case. Here I can see a SSH_CLIENT and SSH_CONNECTION environment variable which both seem to contain the ssh port of the corresponding session. With the help of netstat or lsof they could be used to identify the exact PID of the current session. – scai Aug 27 '13 at 7:49
Good point, but you're right that in my particular case, with only 1 connection at a time this will work. Thanks for the heads up though, perhaps if I get some time I'll work on figuring out more specific solution. – McB Aug 27 '13 at 12:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.