54

Like most sysadmins I use openssh all the time. I have about a dozen ssh keys, I like to have a different ssh key for each host. However this causes a problem when I am connecting to a host for the first time, and all I have is a password. I want to just connect to the host using a password, no ssh key in this case. However the ssh client will offer all the public keys in my ~/.ssh/ (I know this from looking at the output of ssh -v). Since I have so many, I will get disconnected for too many authentication failures.

Is there some way to tell my ssh client to not offer all the ssh keys?

0

6 Answers 6

51

This is expected behaviour according to the man page of ssh_config:

 IdentityFile
         Specifies a file from which the user's DSA, ECDSA or DSA authentica‐
         tion identity is read.  The default is ~/.ssh/identity for protocol
         version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and ~/.ssh/id_rsa for
         protocol version 2.  Additionally, any identities represented by the
         authentication agent will be used for authentication.  

         [...]

         It is possible to have multiple identity files specified in configu‐
         ration files; all these identities will be tried in sequence.  Mul‐
         tiple IdentityFile directives will add to the list of identities
         tried (this behaviour differs from that of other configuration
         directives).

Basically, specifying IdentityFiles just adds keys to a current list the SSH agent already presented to the client.

Try overriding this behaviour with this at the bottom of your .ssh/config file:

Host *
  IdentitiesOnly yes

You can also override this setting on the host level, e.g.:

Host foo
  User bar
  IdentityFile /path/to/key
  IdentitiesOnly yes
3
  • 9
    You can also use ssh -o "IdentitiesOnly true" -v -A user@host which is what I use to login to a machine which has none of my keys but I want to offer agent-forwarding to go on. (-v for verbose debugging).
    – eckes
    Commented Mar 21, 2017 at 18:40
  • 2
    @eckes that's a nice tip, but shouldn't it be yes (and not true) though? Commented Oct 1, 2019 at 13:46
  • 2
    IdentitiesOnly may not always help, you may have to exclude a host specifically; see superuser.com/questions/859661/… Commented Oct 2, 2019 at 8:37
57

Although others have hinted at this with configuration-based solutions, it's probably worth pointing out that you can easily do this one-time-only on the command line with:

ssh -o 'PubkeyAuthentication no' myhostname.mydomain
2
  • 6
    Perfect .. THE solution IMHO
    – drAlberT
    Commented Oct 21, 2014 at 16:38
  • 4
    Correct this should have been the accepted answer. Commented Mar 8, 2017 at 18:41
14

Following James Sneeringer's solution, you might just want to set an ssh_config along the lines of:

Host *.mycompany.com
  IdentityFile .ssh/id_dsa_mycompany_main

Host *.mycustomer.com
  IdentityFile .ssh/id_dsa_mycustomer

Host *
  RSAAuthentication no #this should be up top, avoid ssh1 at all costs
  PubkeyAuthentication no

If you connect with a particular key to many machines not in a common domain, consider giving them all CNAMEs in your own DNS. I do this with all customer systems.

2

Similar to user23413's solution, you can disable public key authentication altogether for a particular host (or wildcard pattern):

Host *.example.org
RSAAuthentication no        # SSHv1
PubkeyAuthentication no     # SSHv2
2

There is an IdentitiesOnly option that you can set to yes, either with -o IdentitiesOnly=yes on the command line or IdentitiesOnly yes in ~/.ssh/config.

From the ssh_config(5) man page:

 IdentitiesOnly
         Specifies that ssh(1) should only use the configured
         authentication identity and certificate files (either the default
         files, or those explicitly configured in the ssh_config files or
         passed on the ssh(1) command-line)

However! If you don’t have any IdentifyFile specified, either in ~/.ssh/config or with some -i option on the command line, then the default identity files ~/.ssh/id_rsa, ~/.ssh/id_ed25519 will still be used!

So if you really want ssh to use only explicitly specified keys, you also need to specify some invalid IdentifyFile, e.g., -i /dev/null.

For example:

Host *.example.com
    IdentityFile ~/.ssh/id_rsa.example.com

Host *.example.org
    IdentityFile ~/.ssh/id_rsa.example.org

# Important! The `Host *` block must come **last**, because subsequent `Host`
# blocks cannot override any preceding `Host *` settings. See ssh_config(5):
# “For each parameter, the first obtained value will be used”
Host *
    IdentitiesOnly yes
    IdentityFile /dev/null
-1

If you point to a particular key file with ssh -i /path/to/key it'll only use that one even if others are loaded into the agent, and you won't be prompted for the password. You can also edit you ~/.ssh/config and ad something like this...

Host foo.example.com
IdentityFile .ssh/id_rsa_foo.example.com

you can also do...

Host *.example.org
IdentityFile .ssh/id_rsa_example.org

1
  • 1
    That just adds to the target key to the end of the list, which won't solve the problem. IdentitiesOnly only with that will.
    – Jo Rhett
    Commented Oct 16, 2018 at 17:49

You must log in to answer this question.

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