`n

SSH Key GitHub Setup - Complete Guide

Published: September 25, 2024 | Reading time: 18 minutes

SSH Key Overview

SSH keys provide secure authentication for GitHub:

SSH Key Benefits
# SSH Key Benefits
- Secure authentication
- No password required
- Key-based access
- Multiple key support
- Easy key management
- Cross-platform compatibility
- Enhanced security

SSH Key Generation

Creating SSH Keys

SSH Key Generation
# SSH Key Generation

# 1. Generate SSH Key Pair
ssh-keygen -t ed25519 -C "your-email@example.com"

# 2. Generate RSA Key (if ed25519 not supported)
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

# 3. Specify custom key name
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/github_ed25519

# 4. Generate key with passphrase
ssh-keygen -t ed25519 -C "your-email@example.com" -N "your-passphrase"

# 5. Generate key without passphrase
ssh-keygen -t ed25519 -C "your-email@example.com" -N ""

# 6. List existing SSH keys
ls -la ~/.ssh/

# 7. View public key
cat ~/.ssh/id_ed25519.pub

# 8. View private key (be careful!)
cat ~/.ssh/id_ed25519

# 9. Test SSH key
ssh -T git@github.com

# 10. Add key to SSH agent
ssh-add ~/.ssh/id_ed25519

GitHub Configuration

Adding SSH Key to GitHub

GitHub SSH Setup
# GitHub SSH Configuration

# 1. Copy public key to clipboard
# macOS
pbcopy < ~/.ssh/id_ed25519.pub

# Linux
cat ~/.ssh/id_ed25519.pub | xclip -selection clipboard

# Windows (PowerShell)
Get-Content ~/.ssh/id_ed25519.pub | Set-Clipboard

# 2. Add key to GitHub
# Go to GitHub Settings > SSH and GPG keys
# Click "New SSH key"
# Paste public key
# Add descriptive title
# Click "Add SSH key"

# 3. Test GitHub connection
ssh -T git@github.com

# 4. Clone repository with SSH
git clone git@github.com:username/repository.git

# 5. Change remote URL to SSH
git remote set-url origin git@github.com:username/repository.git

# 6. Verify remote URL
git remote -v

# 7. Multiple SSH keys
# Create SSH config file
touch ~/.ssh/config

# Add configuration
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_ed25519

# 8. Test specific key
ssh -T -i ~/.ssh/github_ed25519 git@github.com

# 9. SSH agent management
# Start SSH agent
eval "$(ssh-agent -s)"

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

# List keys in agent
ssh-add -l

# Remove key from agent
ssh-add -d ~/.ssh/id_ed25519

# 10. Remove all keys from agent
ssh-add -D

SSH Configuration

Advanced SSH Setup

SSH Configuration
# SSH Configuration

# 1. SSH Config File
# ~/.ssh/config
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes
    AddKeysToAgent yes
    UseKeychain yes

# 2. Multiple GitHub Accounts
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_work_ed25519

Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_personal_ed25519

# 3. SSH Agent Configuration
# ~/.ssh/config
Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentitiesOnly yes

# 4. SSH Key Permissions
# Set correct permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config

# 5. SSH Key Rotation
# Generate new key
ssh-keygen -t ed25519 -C "new-email@example.com" -f ~/.ssh/github_new

# Add new key to GitHub
# Remove old key from GitHub
# Update local configuration

# 6. SSH Key Backup
# Backup private key (encrypted)
gpg --symmetric --cipher-algo AES256 ~/.ssh/id_ed25519

# Backup public key
cp ~/.ssh/id_ed25519.pub ~/backup/

# 7. SSH Key Recovery
# Restore from backup
gpg --decrypt ~/.ssh/id_ed25519.gpg > ~/.ssh/id_ed25519

# Set permissions
chmod 600 ~/.ssh/id_ed25519

# 8. SSH Key Validation
# Validate key format
ssh-keygen -l -f ~/.ssh/id_ed25519.pub

# Check key fingerprint
ssh-keygen -l -E sha256 -f ~/.ssh/id_ed25519.pub

# 9. SSH Connection Debug
# Debug SSH connection
ssh -vT git@github.com

# Verbose output
ssh -vvT git@github.com

# 10. SSH Key Troubleshooting
# Check SSH agent
ssh-add -l

# Restart SSH agent
eval "$(ssh-agent -s)"

# Test connection
ssh -T git@github.com

SSH Best Practices

Security Guidelines

SSH Best Practices

  • Use strong key types (ed25519)
  • Set appropriate permissions
  • Use passphrases for keys
  • Rotate keys regularly
  • Backup keys securely
  • Use SSH agent
  • Monitor key usage

Common Mistakes

  • Weak key types
  • Incorrect permissions
  • No passphrase protection
  • Not backing up keys
  • Sharing private keys
  • Using default key names
  • Not testing connections

Summary

SSH key setup for GitHub involves several key components:

  • Key Generation: Creating secure SSH key pairs
  • GitHub Configuration: Adding keys to GitHub account
  • SSH Configuration: Advanced setup and management
  • Best Practices: Security guidelines and common mistakes

Need More Help?

Struggling with SSH key setup or need help configuring GitHub authentication? Our Git experts can help you implement secure SSH authentication.

Get SSH Help