I have SSH set up with separate public/private key pairs for every server I regularly access, and I configure these identities in the SSH config.
The very first time I connect to a new private server, however, I will need to use a password to copy my public key into the authorized_keys file.
Currently if PubkeyAuthentication is enabled, when I run ssh -v <new-server> I see it trying to use my default private key three times before failing with the error "too many failed attempts". Why it would try an SSH key a second or third time after it failed once is beyond me -- it's not like it typo'd a password.
How can I configure it so that it will only try to use an identity file once, then fall back to password authentication?
My current (rather manual) solution is:
- Add the server to the config file with
PubkeyAuthentication no - Copy the public key
- Edit the config file and set
PubkeyAuthenticationtoyes
The Logs
Per some of the comments, here are the logs when I attempt to connect to a new server (I removed the authorized_keys file from this server prior to doing this to force the error)
{11:48}|~/s/qos >ssh -v sbarnett.vm
OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /home/sbarnett/.ssh/config
debug1: /home/sbarnett/.ssh/config line 30: Applying options for sbarnett.vm
debug1: /home/sbarnett/.ssh/config line 62: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Executing proxy command: exec ssh -q -W sbarnett.vm.domain.com:22 jumphost
debug1: key_load_public: No such file or directory
debug1: permanently_drop_suid: 1000
debug1: identity file /home/sbarnett/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/sbarnett/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/sbarnett/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/sbarnett/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/sbarnett/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/sbarnett/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/sbarnett/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/sbarnett/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to sbarnett.vm.domain.com:22 as 'sbarnett'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:m9EW13TUteWcf/0vkNdFRFjwRhoe28neqN77B71DX8M
debug1: Host 'sbarnett.vm.domain.com' is known and matches the ECDSA host key.
debug1: Found key in /home/sbarnett/.ssh/known_hosts:16
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
WARNING. You have accessed a private computer system. Unauthorized access,
use, connection, or entry is not permitted and constitutes a crime
punishable by law. We reserve the right to fully pursue criminal and civil
legal penalties. All individuals using this computer system with or without
proper authority are subject to having all their activities monitored and
recorded. Anyone using this system implicitly consents to this monitoring.
Any evidence of suspected criminal activity revealed by such monitoring may
be provided to law enforcement officials
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: sbarnett@Gimli
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: sbarnett@Gimli
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: sbarnett@Gimli
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: sbarnett@Gimli
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: sbarnett@Gimli
debug1: Authentications that can continue: publickey,password
debug1: Offering RSA public key: sbarnett@Gimli
Received disconnect from UNKNOWN port 0:2: Too many authentication failures
debug1: Authentication succeeded (publickey).
Authenticated to sbarnett.vm.domain.com (via proxy).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: proc
packet_write_wait: Connection to UNKNOWN port 0: Broken pipe