How does git know which ssh key to use for its operations?

jayarjo

I have SSH keys in place, inside ~/.ssh. Many of them actually. So I wonder how does git know which one to take when it tries to connect to a repository over [email protected]:group/repo.git endpoint?

torek

Git does not know, or care. It just runs ssh.

How does ssh know? It looks at your ~/.ssh/config file (edit: or gets it from ssh-agent; see below):

Host github.com
    # IdentitiesOnly yes # see below to decide if you want this
    IdentityFile ~/.ssh/github_id_file

Host domain.com
    IdentitiesOnly yes # again, see below
    IdentityFile ~/.ssh/another_id_file

Edit: here is a link to a Linux version of the ssh_config documentation. While each system (MacOS, Linux, the various BSDs, even the Windows ports) has its own flavor of ssh config handling, they all share most of these configurables. Note these two items in particular (I have adjusted formatting slightly for StackOverflow markdown):

IdentitiesOnly

      Specifies that ssh(1) should only use the authentication identity files configured in the ssh_config files, even if ssh-agent(1) or a PKCS11Provider offers more identities. The argument to this keyword must be “yes” or “no”. This option is intended for situations where ssh-agent offers many different identities. The default is “no”.

IdentityFile

      Specifies a file from which the user's DSA, ECDSA, ED25519 or RSA authentication identity is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa for protocol version 2. Additionally, any identities represented by the authentication agent will be used for authentication unless IdentitiesOnly is set. ssh(1) will try to load certificate information from the filename obtained by appending -cert.pub to the path of a specified IdentityFile.

      The file name may use the tilde syntax to refer to a user's home directory or one of the following escape characters: ‘%d’ (local user's home directory), ‘%u’ (local user name), ‘%l’ (local host name), ‘%h’ (remote host name) or ‘%r’ (remote user name).

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

      IdentityFile may be used in conjunction with IdentitiesOnly to select which identities in an agent are offered during authentication.

As Alexey Ten noted in a comment, IdentityFile is peculiar in that it is additive (rather than one-setting-overrides-another).

You can also run ssh (manually) with additional -v options to trace the connection. In Git, you can set GIT_SSH to the name of a script that runs ssh -vvv for a temporary trace (or fuss with the log level in your ~/.ssh/config file). I've found this useful to debug occasionally. (Note that you cannot pass options to ssh via GIT_SSH, you need a one-line script such as ssh-vvv with one line reading ssh -vvv $@.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How does shared_ptr<void> know which destructor to use?

From Dev

How does git know which version of a line to keep?

From Dev

How does git know which commit to check a submodule out at?

From Dev

How to configure command line git to use ssh key

From Dev

How does spring know which view resolver to use?

From Dev

How does Bouncy Castle API know which key to encrypt with?

From Dev

How does this model factory know which method to use?

From Dev

how does tomcat know which certificaate to provide

From Dev

How does git know which ssh key to use for its operations?

From Dev

At runtime, how does Swift know which implementation to use?

From Dev

How does a transparent SOCKS proxy know which destination IP to use?

From Dev

How does maven know which repo to use for a dependency?

From Dev

How does SSH know which key to use?

From Dev

How does a transparent SOCKS proxy know which destination IP to use?

From Dev

How to know which public key was used when someone used passwordless ssh login?

From Dev

How does git know which commit to check a submodule out at?

From Dev

How does spring know which view resolver to use?

From Dev

How does this model factory know which method to use?

From Dev

How does Rails 4 know which format to use for views?

From Dev

Use my ssh key to sign git commits

From Dev

How does ssh choose the correct key to use?

From Dev

How does my server pick which key it uses with an SSH client?

From Dev

How does /usr/bin/env know which program to use?

From Dev

How does a class know the sizeof its members?

From Dev

How do I know which key combination does what for the Dvorak international alternative layout?

From Dev

At runtime, how does Swift know which implementation to use?

From Dev

How does BIOS bootloader know which disk to use?

From Dev

Does Git Bash determine identify by ssh key?

From Dev

How does Django know which model manager to use?

Related Related

  1. 1

    How does shared_ptr<void> know which destructor to use?

  2. 2

    How does git know which version of a line to keep?

  3. 3

    How does git know which commit to check a submodule out at?

  4. 4

    How to configure command line git to use ssh key

  5. 5

    How does spring know which view resolver to use?

  6. 6

    How does Bouncy Castle API know which key to encrypt with?

  7. 7

    How does this model factory know which method to use?

  8. 8

    how does tomcat know which certificaate to provide

  9. 9

    How does git know which ssh key to use for its operations?

  10. 10

    At runtime, how does Swift know which implementation to use?

  11. 11

    How does a transparent SOCKS proxy know which destination IP to use?

  12. 12

    How does maven know which repo to use for a dependency?

  13. 13

    How does SSH know which key to use?

  14. 14

    How does a transparent SOCKS proxy know which destination IP to use?

  15. 15

    How to know which public key was used when someone used passwordless ssh login?

  16. 16

    How does git know which commit to check a submodule out at?

  17. 17

    How does spring know which view resolver to use?

  18. 18

    How does this model factory know which method to use?

  19. 19

    How does Rails 4 know which format to use for views?

  20. 20

    Use my ssh key to sign git commits

  21. 21

    How does ssh choose the correct key to use?

  22. 22

    How does my server pick which key it uses with an SSH client?

  23. 23

    How does /usr/bin/env know which program to use?

  24. 24

    How does a class know the sizeof its members?

  25. 25

    How do I know which key combination does what for the Dvorak international alternative layout?

  26. 26

    At runtime, how does Swift know which implementation to use?

  27. 27

    How does BIOS bootloader know which disk to use?

  28. 28

    Does Git Bash determine identify by ssh key?

  29. 29

    How does Django know which model manager to use?

HotTag

Archive