How to specify the SSH key for Pkg to use

I’m trying to get Pkg to use a different SSH key when it instantiates a project. I naively tried this:

env GIT_SSH_COMMAND='ssh -i /path/to/key' julia --project -e 'using Pkg; Pkg.instantiate()'

But it doesn’t seem to work. Is there a different way to do this?

1 Like

Pkg doesn’t use the ssh binary but instead libssh2 via libgit2. libssh2 doesn’t use the GIT_SSH_COMMAND environment variable but instead SSH_KEY_PATH for the private key and SSH_PUB_KEY_PATH for the public key. Yes, it needs both and yes, it’s annoying. All information is available in the private key and the public key can be extracted from it using ssh-keygen, but libssh2 doesn’t include the necessary crypto support to do it internally.

Edit: Strictly speaking SSH_KEY_PATH and SSH_PUB_KEY_PATH aren’t used by libssh2. Rather it’s Julia’s wrapper of libgit2 that reads those and passes them on through the internal APIs to libssh2.

7 Likes

If you’re on Windows you might also run into https://github.com/JuliaLang/julia/issues/28933 depending on how your git server is configured.

That worked, thank you!

I just tried this (with julia 1.4.0) and it does not work — Defined both env variables, but Pkg keeps asking for the location of the private key (actually guesses it correctly, as it is the default), after three attemps, fails

I have submitted an issue: Pkg add private repo not using ssh key · Issue #1733 · JuliaLang/Pkg.jl · GitHub

If you already have the key in the default location, setting SSH_KEY_PATH and SSH_PUB_KEY_PATH won’t help. It is unfortunate that it keeps asking for private key location when what it really means is that something, anything, went wrong during the authentication, and frequently the location of the key is not at all the problem.

Sadly it’s not that easy to debug this. If you have control of the server end you may be able to enable debug output from the ssh server and possibly make sense of it. If you cannot get information from the server and you are sufficiently desperate you can try https://github.com/JuliaLang/Pkg.jl/issues/1516#issuecomment-560794389.

1 Like