I am writing a script to cycle through a list a known hostnames so as to find a working SSH server on which I can auth with my SSH key.

The key is already loaded with ssh-agent and ssh-add, so if I connect to a working remote host that knows my key, no passphrase is prompted and I am successfully connected without any interaction.

Thing is, when the remote host does not know my key, ssh prompts me with the passphrase (it has no chance to succeed because, if it could, it would have already logged me in thanks to ssh-agent). I would like to prevent this behavior and make ssh abort when the ssh-agent auth fails.

I am currently using the following command to bypass most of the interaction but I can not prevent the one I just described:

$ ssh -i ~/.ssh/id_rsa                      \
      -o UserKnownHostsFile=/dev/null       \
      -o KbdInteractiveAuthentication=no    \
      -o StrictHostKeyChecking=no           \
      -o PreferredAuthentications=publickey \
      -o ConnectTimeout=1                   \
      $host -n "whoami"

Thanks for your answers.

2 Answers 2


man ssh_config:

         If set to ``yes'', passphrase/password querying will be disabled.
         This option is useful in scripts and other batch jobs where no
         user is present to supply the password.  The argument must be
         ``yes'' or ``no''.  The default is ``no''.
  • Indeed, it did the trick. I did not notice this option in SSH manapage, thanks.
    – zopieux
    May 18, 2013 at 23:04

While the BatchMode answer is certainly the right one, I want to also mention that you can also do: -o PasswordAuthentication=no ; same man page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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