Windows Git with Public Key Authentication

Windix Feng
2 min readApr 23, 2024

--

TL;DR: make sure newline is LF only and put a new line to your private key file!

What I was trying to do: Cloning a github repo via git protocol with a given private key (the public key has been added to the repo as deploy key) under Windows cli (I used old school cmd.exe).

The SSH client is the one shipped with Windows Server 2023 (OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2) and git is installed via choco

Tried many different ways but keep getting this error:

C:\tmp>ssh -i id_rsa.txt git@github.com
Load key "id_rsa.txt": invalid format
git@github.com: Permission denied (publickey).

Someone says Windows SSH client only supports ED25519 format, another source says the private key needs to be in a single line, or need to be in LF line format not CRLF. But none of above work.

After try and error, here is the final answer works for me:

  • The newline format for file needs to be LF (not CRLF, can be easily updated using editors like vscode)
  • Need an extra new line after ---END OPENSSH PRIVATE KEY--- !

See below example:

C:\tmp>ssh -i id_rsa.txt git@github.com
PTY allocation request failed on channel 0
Hi! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

Here are two one-liners for Linux / Mac and Windows usage for future references:

Linux / Mac (bash)

GIT_SSH_COMMAND='ssh -i id_rsa_github -o IdentitiesOnly=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' git clone git@github.com:who/what.git

Windows (cmd.exe)

set GIT_SSH_COMMAND=ssh -i id_rsa_github -o IdentitiesOnly=yes -o UserKnownHostsFile=\\.\NUL -o StrictHostKeyChecking=no & git clone git@github.com:who/what.git

--

--

No responses yet