Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a box sat behind a firewall, and when its powered on (or internet connection resumed), I want it to connect to a server, allowing me to do a reverse SSH connection back to it.

I'm currently using:

ssh "$USER@$HOST" "tunnel.port.sh '$PORT' '$HOSTNAME'";

ssh -N -R $PORT:localhost:222 -o ServerAliveInterval=30 "$USER@$HOST";

The $PORT is randomly assigned... because I want to avoid the "address already in use" issue, but am aware that a 0 would be "dynamically allocated on the server and reported to the client at run time" (not sure how to obtain that though).

But where it is randomly assigned, I need to connect to the box first, to run "tunnel.port.sh", which records what that $PORT number is.

Obviously I would like a better (less hacky) solution.

I've looked at autossh, but thats more to keep the connection alive.

And for extra complication, neither box should trust each other... so when it does ssh in via the "$USER" account, its put in a restricted shell (rbash), and only able to run the "tunnel.port.sh" script, or keep the connection alive... and likewise connections back still require authentication... so I don't think a VPN would work.

share|improve this question
As a side note, I'm also trying to get this to work with Chef, so something like ssh_tunnel (by engineyard), but works on Debian (not Gentoo), and will leave a record of the port number to use when connecting back (probably more than 1 box). –  Craig Francis Jan 17 at 17:42

Your Answer


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

Browse other questions tagged or ask your own question.