Fix SSH Access Denied for Git SSH remotes

This same process is true for GitLab, GitHub, Bitbucket, etc.


If you have more than about 5 SSH keys on your PC in ~/.ssh/, you need to setup your ~/.ssh/config file so that SSH to GitHub (or other SSH server) uses the correct SSH Public Key the first connect try.

Do this by adding to ~/.ssh/config

Host github.com
   User git
   IdentitiesOnly yes
   PubKeyAuthentication yes
   IdentityFile ~/.ssh/mygit

If you get owner/permissions error, fix by:

chmod 400 ~/.ssh/config

Create SSH key for Git

~/.ssh/mygit is created with a command like:

ssh-keygen -t ed25519 -f ~/.ssh/mygit

git over SSH instead of HTTP

Set the remote URL to use SSH by (assuming the repo remote is github.com/username/myrepo)

git remote set-url origin ssh://github.com/username/myrepo

For large numbers of repos, you can pull over HTTPS and push over SSH.

Categories: ,

Written by Michael Hirsch, Ph.D. //

Comments