What are SSH Keys?
SSH (Secure Shell) keys are a secure way to authenticate with remote servers and services like GitHub, GitLab, and cloud platforms. Instead of typing passwords, SSH keys use cryptographic key pairs to verify your identity.
🔑 Public Key
Shared with servers and services. Safe to copy and paste anywhere. Acts like a padlock that others can see.
🔐 Private Key
Kept secret on your computer. Never share this! Acts like the key that opens the padlock.
Choose Your Operating System
Step-by-Step Setup
Step 1: Generate Your SSH Key
Option A: Using Windows Terminal or PowerShell (Recommended)
Generate Ed25519 key (most secure and fast)
ssh-keygen -t ed25519 -C 'your.email@example.com'If your system doesn't support Ed25519, use RSA instead:
Generate 4096-bit RSA key (older systems)
ssh-keygen -t rsa -b 4096 -C 'your.email@example.com'Option B: Using Git Bash
If you have Git for Windows installed, open Git Bash and use the same commands above.
Open Terminal (Applications → Utilities → Terminal) and run:
Generate Ed25519 key (recommended for macOS 10.12+)
ssh-keygen -t ed25519 -C 'your.email@example.com'For older macOS versions, use RSA:
Generate 4096-bit RSA key (older macOS versions)
ssh-keygen -t rsa -b 4096 -C 'your.email@example.com'Open your terminal and run:
Generate Ed25519 key (recommended)
ssh-keygen -t ed25519 -C 'your.email@example.com'If Ed25519 is not available, use RSA:
Generate 4096-bit RSA key (alternative)
ssh-keygen -t rsa -b 4096 -C 'your.email@example.com'⚡ During Key Generation:
- File location: Press Enter to use default location (~/.ssh/id_ed25519)
- Passphrase: Enter a strong passphrase for extra security (recommended)
- Confirm passphrase: Enter the same passphrase again
Step 2: Add Key to SSH Agent
Start the SSH agent and add your key:
Start SSH agent
eval $(ssh-agent -s)Add your SSH key to the agent
ssh-add ~/.ssh/id_ed25519If you used RSA, replace id_ed25519 with id_rsa
Start the SSH agent and add your key to the keychain:
Start SSH agent
eval "$(ssh-agent -s)"Add key to macOS keychain
ssh-add --apple-use-keychain ~/.ssh/id_ed25519Tip: Create or modify ~/.ssh/config to automatically load keys:
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Host gitlab.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519Start the SSH agent and add your key:
Start SSH agent
eval "$(ssh-agent -s)"Add your SSH key to the agent
ssh-add ~/.ssh/id_ed25519Step 3: Copy Your Public Key
Copy your public key to clipboard:
Copy public key to clipboard (Windows)
clip < ~/.ssh/id_ed25519.pubAlternative method if clip doesn't work:
Display public key to copy manually
cat ~/.ssh/id_ed25519.pubCopy your public key to clipboard:
Copy public key to clipboard (macOS)
pbcopy < ~/.ssh/id_ed25519.pubAlternative method:
Display public key to copy manually
cat ~/.ssh/id_ed25519.pubCopy your public key:
Display public key to copy
cat ~/.ssh/id_ed25519.pubIf you have xclip installed, you can copy to clipboard:
Copy to clipboard (if xclip is installed)
xclip -selection clipboard < ~/.ssh/id_ed25519.pubPlatform Setup
🐙 GitHub Setup
- Go to GitHub SSH Settings
- Click "New SSH key"
- Give it a descriptive title (e.g., "My Laptop - 2024")
- Paste your public key into the "Key" field
- Click "Add SSH key"
- Test with:
ssh -T git@github.com
🦊 GitLab Setup
- Go to GitLab SSH Keys
- Paste your public key into the "Key" field
- Add a descriptive title
- Set expiration date (optional but recommended)
- Click "Add key"
- Test with:
ssh -T git@gitlab.com
☁️ Cloud Providers
Import key in EC2 Console → Key Pairs → Import Key Pair
Add in Compute Engine → Metadata → SSH Keys
Add in Account → Security → SSH Keys
🖥️ Your Own Server
- Connect to your server via password/existing method
- Create SSH directory:
mkdir -p ~/.ssh - Add your public key:
echo "your-public-key" >> ~/.ssh/authorized_keys - Set permissions:
chmod 600 ~/.ssh/authorized_keys - Set directory permissions:
chmod 700 ~/.ssh
Testing Your Setup
Test GitHub Connection
Should return: Hi username! You've successfully authenticated...
ssh -T git@github.comTest GitLab Connection
Should return: Welcome to GitLab, username!
ssh -T git@gitlab.comClone a Repository
Clone using SSH instead of HTTPS
git clone git@github.com:username/repository.gitCommon Issues & Solutions
❌ "Permission denied (publickey)"
Causes & Solutions:
- SSH agent not running: Run
eval $(ssh-agent -s) - Key not added to agent: Run
ssh-add ~/.ssh/id_ed25519 - Wrong key file: Check if you're using the right key name
- Public key not added to platform: Re-add your public key
⚠️ "Could not open a connection to your authentication agent"
Solution: Start the SSH agent first:
eval $(ssh-agent -s)Then add your key: ssh-add ~/.ssh/id_ed25519
ℹ️ "Host key verification failed"
Solution: Remove the problematic host key:
ssh-keygen -R hostname-or-ipThen try connecting again to accept the new host key.
💡 Debug Connection Issues
Use verbose mode to see what's happening:
ssh -vT git@github.comThis will show detailed connection information to help debug issues.
Security Best Practices
🔒 Key Security
- Always use a strong passphrase for your private key
- Never share your private key with anyone
- Use Ed25519 keys for better security and performance
- Generate separate keys for different purposes if needed
- Regularly rotate your SSH keys (annually)
⚙️ Configuration Tips
- Set up SSH config file (~/.ssh/config) for convenience
- Use different keys for personal and work accounts
- Set key expiration dates where supported
- Keep your SSH client updated
- Monitor your account's SSH key list regularly
Advanced: SSH Config File
Create a ~/.ssh/config file to simplify SSH connections and manage multiple accounts:
# Personal GitHub account
Host github.com
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
# Work GitHub account
Host github-work
HostName github.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519_work
# GitLab
Host gitlab.com
HostName gitlab.com
User git
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
# Custom server
Host myserver
HostName your-server.com
User yourusername
Port 22
IdentityFile ~/.ssh/id_ed25519
ForwardAgent yesUsage: With this config, you can simply use git clone github-work:company/repo.gitor ssh myserver instead of typing full commands.