🔑 macOS SSH Key Setup (with Keychain)
When connecting to Bitbucket/GitHub/GitLab via SSH, you might see:
Permission denied (publickey).
This happens because your SSH agent has no key loaded.
1. Generate an SSH Key (if you don’t already have one)
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/[email protected]
This creates:
- Private key →
~/.ssh/[email protected]
- Public key →
~/.ssh/[email protected]
2. Add Key to macOS Keychain
ssh-add --apple-use-keychain ~/.ssh/[email protected]
Check if loaded:
ssh-add -l
3. Configure ~/.ssh/config
Edit (or create) the file:
nano ~/.ssh/config
Add:
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/[email protected]
UseKeychain yes
AddKeysToAgent yes
💡 This ensures:
- The key auto-loads into Keychain.
- Git automatically uses this key for Bitbucket.
4. Upload Public Key to Bitbucket
Copy your key:
cat ~/.ssh/[email protected]
Go to Bitbucket → Personal Settings → SSH Keys → Add Key → paste it.
5. Test Connection
ssh -T [email protected]
Expected output:
authenticated via ssh key.
You can use git to connect to Bitbucket!
6. (Optional) Linux Setup
If using Linux, add this to ~/.bashrc
or ~/.zshrc
:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/[email protected]
✅ Executive Summary
- Problem: SSH agent had no key → “Permission denied”.
- Fix: Load key with
ssh-add
and persist via Keychain. - Best Practice: Use
~/.ssh/config
+ Keychain → no more manualssh-add
.