In a new terminal session, running git pull gives a ‘Public key denied’ error

Method 1:

Login to Server using SSH and Type below command

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/private_key

eval "$(ssh-agent -s)" — starts the SSH agent and sets environment variables like SSH_AUTH_SOCK.

ssh-add ~/.ssh/tondemo_rsa — adds your private SSH key to the agent so it can authenticate automatically (e.g., with GitHub or your server).

Method 2: Permanantly Fix

Linux/OpenSSH

nano ~/.ssh/config

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/tondemo_rsa
  IdentitiesOnly yes
  AddKeysToAgent yes

Notes

  • UseKeychain and ssh-add -K are macOS-specific — remove them on Linux.
  • AddKeysToAgent yes will prompt to add the key to the agent on first use (works with modern OpenSSH).
  • Save and exit (Ctrl+O, Enter, Ctrl+X in nano).

MacOs

nano ~/.ssh/config

Host github.com
  IdentityFile ~/.ssh/tondemo_rsa
  AddKeysToAgent yes
  UseKeychain yes
  IdentitiesOnly yes

Leave a Comment

Your email address will not be published. Required fields are marked *