Why SSH?

SSH (Secure Shell): Encrypted authentication protocol

  • No password needed after setup
  • More secure than HTTPS
  • Uses public/private key pairs

Key Concepts

Public Key

  • Safe to share
  • Added to GitHub account
  • Like a lock

Private Key

  • Never share
  • Stays on your computer
  • Like a key that opens the lock

SSH Agent

  • Background program
  • Holds private keys in memory
  • Prevents re-entering passphrase

Generate SSH Key

# Generate Ed25519 key (modern, secure)
ssh-keygen -t ed25519 -C "your_email@example.com"

# When prompted:
# File: Press Enter (default ~/.ssh/id_ed25519)
# Passphrase: Optional (press Enter to skip)

Output:

  • Private key: ~/.ssh/id_ed25519
  • Public key: ~/.ssh/id_ed25519.pub

Add Key to SSH Agent

# Start agent
eval "$(ssh-agent -s)"

# Add key
ssh-add ~/.ssh/id_ed25519

Add Public Key to GitHub

# Display public key
cat ~/.ssh/id_ed25519.pub

# Copy output (starts with ssh-ed25519)
  1. GitHub → Settings → SSH and GPG keys
  2. New SSH key
  3. Paste public key
  4. Add SSH key

Test Connection

ssh -T git@github.com

# Success output:
# Hi username! You've successfully authenticated...

Multiple GitHub Accounts

Problem: Can’t use same key on multiple accounts

Solution: Create separate keys + SSH config

1. Generate Two Keys

# Personal account
ssh-keygen -t ed25519 -C "personal@email.com" -f ~/.ssh/id_ed25519_personal

# Work account
ssh-keygen -t ed25519 -C "work@email.com" -f ~/.ssh/id_ed25519_work

2. Add Both to Agent

ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work

3. Create SSH Config

Edit ~/.ssh/config:

# Personal GitHub
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
  IdentitiesOnly yes

# Work GitHub
Host github.com-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes

4. Clone with Custom Host

# Personal repo
git clone git@github.com-personal:username/repo.git

# Work repo
git clone git@github.com-work:username/repo.git

5. Set Git Identity Per Repo

# Personal repo
cd personal-repo
git config user.name "Personal Name"
git config user.email "personal@email.com"

# Work repo
cd work-repo
git config user.name "Work Name"
git config user.email "work@email.com"

How SSH Config Works

git push → git@github.com-personal:user/repo.git
         SSH sees "github.com-personal"
         Checks ~/.ssh/config
         Finds matching Host entry
         Uses IdentityFile: ~/.ssh/id_ed25519_personal
         Connects to HostName: github.com
         GitHub authenticates with personal account

Common Issues

“Permission denied”:

# Check keys loaded
ssh-add -l

# If empty, add key
ssh-add ~/.ssh/id_ed25519

Wrong account used:

  • Check remote URL: git remote -v
  • Should use custom host: git@github.com-personal
  • Not generic: git@github.com

Notes

  • One key can be used for multiple services (GitHub, GitLab, etc.)
  • SSH config Host is just an alias (not a different server)
  • IdentitiesOnly yes prevents trying all keys
  • Git config sets commit author (separate from SSH authentication)