DEV Community

Cover image for git@github.com: Permission denied (Linux Guide)
Carbon Labs
Carbon Labs

Posted on • Edited on

git@github.com: Permission denied (Linux Guide)

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.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Or remove multiple key pairs (if you have)

# Remove all RSA keys
rm ~/.ssh/id_rsa*
Enter fullscreen mode Exit fullscreen mode

If no existing keys are found, generate a new SSH key:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Key Generation Steps:

  1. When prompted for file location, press Enter to accept default
  2. Optional: Set a passphrase for additional security
  3. The system will generate two files:
    • Private key (id_rsa)
    • Public key (id_rsa.pub)
  4. 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:

  1. Public Email Address

    • Visible to everyone on GitHub
    • Used in commit metadata
  2. 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

  1. Navigate to GitHub Email Settings
  2. 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"
Enter fullscreen mode Exit fullscreen mode

Verification

  • Check current Git configuration:
  git config --global user.email
  git config --global user.name
Enter fullscreen mode Exit fullscreen mode

4. Display Your Public SSH Key

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Copy the entire output - this is the key you'll add to GitHub.

5. Add SSH Key to GitHub

Web Interface Steps:

  1. Log into GitHub.com
  2. Click on your profile picture → Settings
  3. Navigate to "SSH and GPG keys"
  4. Click "New SSH key"
  5. Give the key a descriptive title (e.g., "MyThinkPad")
  6. 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
Enter fullscreen mode Exit fullscreen mode

7. Test GitHub Connection

Verify your SSH connection:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

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."

Enter fullscreen mode Exit fullscreen mode

Troubleshooting Common Issues

Permission Denied Errors

  • Ensure key permissions are correct:
  chmod 600 ~/.ssh/id_rsa
  chmod 644 ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

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

Additional Resources

Top comments (0)