GitHub SSH Authentication and Email Privacy Guide (git@github.com: Permission denied)
If you ever encounter the error below, be sure to check this guide:
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
Prerequisites
- Git installed
- GitHub account
- Linux/Unix terminal (bash)
1. Check for Existing SSH Keys
First, check if you already have SSH keys:
ls -al ~/.ssh
Look for files like:
-
id_rsa
(private key) -
id_rsa.pub
(public key)
2. Generate a New SSH Key
If you want to delete the existing keys (id_rsa, id_rsa.pub) and start over, follow these steps:
# Remove private key
rm ~/.ssh/id_rsa
# Remove public key
rm ~/.ssh/id_rsa.pub
Or remove multiple key pairs (if you have)
# Remove all RSA keys
rm ~/.ssh/id_rsa*
If no existing keys are found, generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Key Generation Steps:
- When prompted for file location, press Enter to accept default
- Optional: Set a passphrase for additional security
- The system will generate two files:
- Private key (
id_rsa
) - Public key (
id_rsa.pub
)
- Private key (
-
You can always check it from here:
ls -al ~/.ssh
3. Email Privacy Configuration
GitHub Email Privacy
GitHub provides several options to protect your email privacy:
-
Public Email Address
- Visible to everyone on GitHub
- Used in commit metadata
-
Private Email Address
- Provided by GitHub
- Keeps your personal email hidden
- Format:
username@users.noreply.github.com
Configuring Email Privacy
Step 1: GitHub Account Settings
- Navigate to GitHub Email Settings
- Choose your preferred privacy option:
- Keep email address private
- Use GitHub-provided private email
Step 2: Configure Local Git Email
Use the GitHub-provided private email for commits:
# Set global Git email (replace with your GitHub username)
git config --global user.email "username@users.noreply.github.com"
# Optionally set your name
git config --global user.name "Your Name"
Verification
- Check current Git configuration:
git config --global user.email
git config --global user.name
4. Display Your Public SSH Key
cat ~/.ssh/id_rsa.pub
Copy the entire output - this is the key you'll add to GitHub.
5. Add SSH Key to GitHub
Web Interface Steps:
- Log into GitHub.com
- Click on your profile picture → Settings
- Navigate to "SSH and GPG keys"
- Click "New SSH key"
- Give the key a descriptive title (e.g., "MyThinkPad")
- Paste the copied public key
6. Configure SSH Agent
Start the SSH agent and add your key:
# Start the SSH agent
eval "$(ssh-agent -s)"
# Add your SSH private key
ssh-add ~/.ssh/id_rsa
7. Test GitHub Connection
Verify your SSH connection:
ssh -T git@github.com
Expected output includes a success message with your GitHub username like this:
"Hi <username>! You've successfully authenticated, but GitHub does not provide shell access."
Troubleshooting Common Issues
Permission Denied Errors
- Ensure key permissions are correct:
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
Multiple GitHub Accounts
- Use different keys for different accounts
- Configure
~/.ssh/config
to specify keys
Best Practices
- Use a strong passphrase
- Regularly rotate SSH keys
- Remove keys from GitHub when no longer in use
- Protect your email privacy
- Use GitHub-provided private email for commits
Verification Checklist
- SSH key generated
- Key added to GitHub
- SSH agent configured
- Email privacy configured
- Successful GitHub connection test
Top comments (0)