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

I have a bash script that runs ssh to create a port forward, using a command like this:

ssh -N -i keyfile -L 1000:localhost:22 *remote_ip*

There are occasions where the listen port may be busy, so this command gives the error:

channel_setup_fwd_listener: cannot listen to port: 1000
Could not request local forwarding.

However, the ssh connection remains up and the ssh command blocks. How can I make ssh actually fail when this occurs, so my script can handle it?

Unfortunately, I also need to support this on Solaris (Intel), and the ssh command there doesn't support the ExitOnForwardFailure option - any ideas in this case?

share|improve this question

1 Answer 1

up vote 8 down vote accepted

If you check the ssh man page, you'll find there is a config option called ExitOnForwardFailure and you can specify it on the command line by adding:

-o "ExitOnForwardFailure yes"

All the ssh config options are described in the ssh_config and sshd_config man pages. If you find the option is not supported, you may have to upgrade to a newer version of ssh.

Good Luck.

share|improve this answer
Thanks, that wasn't in the man page I googled, but is available to my ssh. –  xorsyst Feb 24 '14 at 10:38

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.