Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

If I do a remote port forward, a la -R 3690:localhost:3690 when a binding already exists on the port on the remote host, I get this warning:

Warning: remote port forwarding failed for listen port 3690

Is there a way to have ssh fail (i.e. exit with a nonzero return code), rather than just emit a warning?

share|improve this question
Do you actually need to make a terminal channel as well, or just the forwarding? – Ignacio Vazquez-Abrams Oct 31 '11 at 1:16
@IgnacioVazquez-Abrams: Just the forwarding. – Matt Joiner Nov 7 '11 at 23:37
up vote 26 down vote accepted


ssh -o "ExitOnForwardFailure yes" ...

or put

ExitOnForwardFailure yes

into ~/.ssh/config. See ssh_config(1) using man for details.

share|improve this answer
Unfortunately I have OpenSSH 4. Can you tell me when this feature was added? – Matt Joiner Nov 1 '11 at 1:14
No, I don't know that. It may be a version 5 feature. But version 4 must be many years old now, and there are security fixes all the time. If you can't upgrade the server yourself, you may want to ask your server admin if s/he thinks it's safe to keep using that version. – Andrew Schulman Nov 1 '11 at 6:23

I use bash script on the target host to make sure the forwarding was opened correctly. The SSH connection will run this and exit if there's a problem with the port forwarding, e.g.

client side script: ( this uses .ssh/config for port forwarding settings )


while true; do
    echo -n starting at : "
    ssh user@server bin/
    echo "got back, sleeping 17 "
    sleep 17

server side script ( bin/ )


while true; do 
  echo $(date)" : SSH Reverse 1090:80, 1232:22 From Server to Client"
  sleep 17
  if ! netstat -an | grep -q ":::1090 " ; then
     echo "1090 forward missing, bailing out"

Maybe even run the client side script under screen with -dmS

share|improve this answer
The case I'm trying to avoid is that a port forward already exists, and a warning is given. I think this script will treat an existing binding to the port as success, rather than failure. – Matt Joiner Nov 4 '11 at 11:43
This is true. My problem was with the same script holding the port open for a few minutes before timing out. Thsi script would exit and rerun a few times after which the port would be open again. If you need to know for sure who owns the port you could try running netstat -anp with sudo and grepping that. – anttir Nov 7 '11 at 17:28

You must log in to answer this question.

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