Today I discovered something frustrating and annoying: For an OpenSSH key to be parsed correctly, it needs to have a space at the end of the file. I spent two hours troubleshooting this issue. Helpfully, OpenSSH gives you this very helpful error:
$ echo "Creating '.ssh' directory." # collapsed multi-line command Creating '.ssh' directory. Copying host keys to 'known_hosts' file. Modifying permissions on ssh key and known hosts file. Starting SSH Agent. Agent pid 23 Initializing docs directory and git. Initialized empty Git repository in /builds/go/my-go-project/doc_deploy/.git/ Configuring git. Updating from remote. Load key "/root/.ssh/id_rsa": invalid format <-- this is the error, no context firstname.lastname@example.org: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
So, if you’re getting “invalid format”, try a new line at the end of the file! Also, try getting rid of Windows
\r\n from your file, although I’d always recommend having git strip this out anyway. Shout out to this question from Server Fault that got me on the road to solving this issue.