Managing Multiple SSH Keys
Managing Multiple SSH Keys on Linux: A Beginner’s Complete Guide
What Are SSH Keys and Why Do You Need Multiple Ones?
SSH (Secure Shell) keys are like digital keys that allow you to securely connect to remote servers and services without typing passwords every time. Think of them as a more secure alternative to passwords - they come in pairs: a public key (which you share) and a private key (which you keep secret).
You might need multiple SSH keys when you:
- Work with different GitHub accounts (personal and work)
- Connect to various AWS instances
- Access multiple servers or cloud services
- Separate keys for security reasons (different keys for different purposes)
Step 1: Understanding the SSH Directory Structure
Before we create keys, let’s understand where SSH stores everything on your Linux system:
# Navigate to your SSH directory
cd ~/.ssh
ls -la
You’ll typically see files like:
id_rsaandid_rsa.pub(your default private and public keys)config(configuration file we’ll create)known_hosts(stores server fingerprints)
Step 2: Creating Your First SSH Key Pair
Let’s start by creating SSH keys for different services. We’ll begin with a GitHub key:
# Create a new SSH key for GitHub
ssh-keygen -t rsa -b 4096 -C "your-email@example.com" -f ~/.ssh/id_rsa_github
# When prompted:
# - Enter a secure passphrase (recommended) or press Enter for no passphrase
# - Confirm the passphrase
The -f flag specifies the filename, making it easy to identify which key is for which service.
Step 3: Creating Additional Keys for Different Services
Now let’s create keys for other services:
# For AWS
ssh-keygen -t rsa -b 4096 -C "aws-access" -f ~/.ssh/id_rsa_aws
# For a work GitHub account
ssh-keygen -t rsa -b 4096 -C "work-email@company.com" -f ~/.ssh/id_rsa_github_work
# For a personal server
ssh-keygen -t rsa -b 4096 -C "personal-server" -f ~/.ssh/id_rsa_personal_server
After creating these keys, your ~/.ssh directory will contain:
id_rsa_github (private key)
id_rsa_github.pub (public key)
id_rsa_aws (private key)
id_rsa_aws.pub (public key)
id_rsa_github_work (private key)
id_rsa_github_work.pub (public key)
id_rsa_personal_server (private key)
id_rsa_personal_server.pub (public key)
Step 4: Setting Proper Permissions
SSH is very strict about file permissions. Let’s set them correctly:
# Set permissions for private keys (readable only by you)
chmod 600 ~/.ssh/id_rsa_*
# Set permissions for public keys
chmod 644 ~/.ssh/id_rsa_*.pub
# Set directory permissions
chmod 700 ~/.ssh
Step 5: Creating the SSH Config File
This is where the magic happens. The SSH config file tells your system which key to use for which service:
# Create or edit the config file
nano ~/.ssh/config
Add the following configuration:
# GitHub Personal Account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github
IdentitiesOnly yes
# GitHub Work Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_work
IdentitiesOnly yes
# AWS Instance
Host aws-server
HostName your-aws-instance-ip-or-domain
User ec2-user
IdentityFile ~/.ssh/id_rsa_aws
Port 22
# Personal Server
Host personal-server
HostName your-server-ip-or-domain
User your-username
IdentityFile ~/.ssh/id_rsa_personal_server
Port 22
Step 6: Adding Public Keys to Services
Now you need to add your public keys to the respective services:
For GitHub:
# Display your GitHub public key
cat ~/.ssh/id_rsa_github.pub
Copy this output and add it to your GitHub account at Settings → SSH and GPG keys → New SSH key.
For AWS:
# Display your AWS public key
cat ~/.ssh/id_rsa_aws.pub
Add this to your AWS EC2 Key Pairs or directly to the server’s ~/.ssh/authorized_keys file.
For your personal server:
# Copy the public key to your server
ssh-copy-id -i ~/.ssh/id_rsa_personal_server.pub username@your-server-ip
Step 7: Using Your SSH Keys
Now that everything is set up, here’s how to use your different keys:
Connecting to GitHub:
# Personal GitHub (uses default github.com host)
git clone git@github.com:username/repository.git
# Work GitHub (uses custom github-work host)
git clone git@github-work:company/repository.git
Connecting to servers:
# Connect to AWS instance
ssh aws-server
# Connect to personal server
ssh personal-server
# Or connect directly using the key
ssh -i ~/.ssh/id_rsa_aws ec2-user@your-aws-ip
Step 8: Managing the SSH Agent
The SSH agent manages your keys in memory, so you don’t have to enter passphrases repeatedly:
# Start the SSH agent
eval "$(ssh-agent -s)"
# Add your keys to the agent
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_aws
ssh-add ~/.ssh/id_rsa_github_work
ssh-add ~/.ssh/id_rsa_personal_server
# List loaded keys
ssh-add -l
# Remove all keys from agent
ssh-add -D
Step 9: Testing Your Connections
Always test your SSH connections to ensure everything works:
# Test GitHub personal
ssh -T git@github.com
# Test GitHub work
ssh -T git@github-work
# Test AWS server
ssh aws-server
# Test with verbose output for troubleshooting
ssh -v aws-server
Step 10: Troubleshooting Common Issues
Issue 1: “Permission denied (publickey)”
Solution: Check if your public key is added to the server and your private key has correct permissions:
chmod 600 ~/.ssh/id_rsa_your_key
Issue 2: Wrong key being used
Solution: Add IdentitiesOnly yes in your SSH config to prevent SSH from trying all keys.
Issue 3: Can’t connect to GitHub with work account
Solution: Make sure you’re using the correct host alias:
# Wrong
git clone git@github.com:company/repo.git
# Correct
git clone git@github-work:company/repo.git
Step 11: Advanced Tips and Best Practices
Organizing Your Keys
Create a naming convention that makes sense:
id_rsa_github_personalid_rsa_github_workid_rsa_aws_productionid_rsa_aws_development
Backup Your Keys
# Create a secure backup of your SSH directory
tar -czf ssh_backup.tar.gz ~/.ssh
# Store this backup in a secure location
Use Different Key Types
For enhanced security, consider using Ed25519 keys:
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_ed25519_github
Automate SSH Agent Loading
Add this to your ~/.bashrc or ~/.zshrc:
# Auto-start SSH agent
if [ -z "$SSH_AUTH_SOCK" ]; then
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_aws
fi
Step 12: Quick Reference Commands
Here’s a handy reference for daily use:
# List all keys in SSH directory
ls -la ~/.ssh/
# Test connection with specific key
ssh -i ~/.ssh/id_rsa_specific_key user@host
# Add key to SSH agent
ssh-add ~/.ssh/id_rsa_key_name
# View SSH config
cat ~/.ssh/config
# Test GitHub connection
ssh -T git@github.com
# Clone with specific SSH config host
git clone git@custom-host:user/repo.git
Conclusion
You now have a complete setup for managing multiple SSH keys on Linux! This setup allows you to:
- Seamlessly switch between different GitHub accounts
- Connect to various AWS instances with different keys
- Maintain security by using separate keys for different purposes
- Easily manage and organize your SSH connections
Remember to keep your private keys secure, use strong passphrases, and regularly backup your SSH configuration. With this setup, you’ll never have to worry about SSH key conflicts or security issues when working with multiple services.
The key to success with multiple SSH keys is organization and consistent naming conventions. As you add more services and servers, simply follow the same pattern: create the key pair, add the configuration to your SSH config file, and test the connection.