Git error: could not read from remote repository (SSH key fix)
You run git push or git pull on a server and get “fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.” Almost always this means the SSH key on that machine is missing, not loaded, or not added to your Git hosting account (Bitbucket, GitHub, GitLab).
Problem: git push or git pull fails with “Could not read from remote repository”. Often it is preceded by a line like this:
Warning: Permanently added ‘bitbucket.org,18.205.93.0’ (RSA) to the list of known hosts. git@bitbucket.org: Permission denied (publickey).
The first part is harmless: SSH remembered the server’s host key. The important part is Permission denied (publickey): the server didn’t accept any of your keys.
Step 1. Check for an existing key
ls -la ~/.ssh/ # Look for id_rsa.pub or id_ed25519.pub
If a key exists, skip to Step 3.
Step 2. Generate a key pair
# Ed25519: shorter and more secure than RSA ssh-keygen -t ed25519 -C "deploy@example.com" # RSA, if Ed25519 is not supported somewhere ssh-keygen -t rsa -b 4096 -C "deploy@example.com"
Press Enter to accept the default file name.
Step 3. Add the public key to your Git hosting account
Print the public key and copy it:
cat ~/.ssh/id_ed25519.pub
Then add it in your account:
- Bitbucket: Personal settings → SSH keys → Add key
- GitHub: Settings → SSH and GPG keys → New SSH key
- GitLab: Preferences → SSH Keys
Step 4. Make sure SSH uses the key
Load the private key into the SSH agent:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519
If the key has a non-default name, tell SSH which key to use for the host in ~/.ssh/config:
Host bitbucket.org
IdentityFile ~/.ssh/id_ed25519
Step 5. Test the connection
ssh -T git@bitbucket.org # Bitbucket: "logged in as username" ssh -T git@github.com # GitHub: "Hi username! You've successfully authenticated" ssh -T git@gitlab.com # GitLab: "Welcome to GitLab, @username!"
If it still fails, run ssh -Tvv git@bitbucket.org: the verbose output shows which key files SSH offers and which one the server rejects.
Still failing? Check the remote URL
If the test succeeds but Git still fails, check that the repository’s remote URL uses SSH. Run git remote -v: the URL should start with git@. If it starts with https://, Git doesn’t use your key at all. Switch it:
git remote set-url origin git@bitbucket.org:username/repo.git
Also make sure the account the key belongs to has access to that repository: a key that works for your own repositories is still rejected for a client’s repository you were not invited to.