SSH Key Setup Guide
Complete step-by-step SSH key setup guide for Windows, Mac, and Linux. Learn to generate, configure, and troubleshoot SSH keys for GitHub, GitLab, and secure server access.
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 '[email protected]'If your system doesn't support Ed25519, use RSA instead:
Generate 4096-bit RSA key (older systems)
ssh-keygen -t rsa -b 4096 -C '[email protected]'Option B: Using Git Bash
If you have Git for Windows installed, open Git Bash and use the same commands above.
โก 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
Step 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.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 [email protected]
๐ฆ 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 [email protected]
โ๏ธ 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 [email protected]Test GitLab Connection
Should return: Welcome to GitLab, username!
ssh -T [email protected]Clone a Repository
Clone using SSH instead of HTTPS
git clone [email protected]: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 [email protected]This 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.