How to Create SSH Keys on Linux and Connect to GitHub
Setting up SSH keys for GitHub is one of the most secure and convenient ways to authenticate with your repositories. This guide will walk you through creating an SSH key pair on Linux and configuring it for use with GitHub.
What You'll Learn
- How to generate an SSH key pair on Linux
- How to add your SSH key to the SSH agent
- How to add your public key to your GitHub account
- How to test your SSH connection to GitHub
Prerequisites
- A Linux system with terminal access
- A GitHub account
- Basic familiarity with the command line
Step 1: Check for Existing SSH Keys
Before creating a new SSH key, let's check if you already have one:
ls -la ~/.ssh
If you see files named id_rsa and id_rsa.pub (or id_ed25519 and id_ed25519.pub), you already have SSH keys. You can either use the existing key or create a new one following the steps below.
Step 2: Generate a New SSH Key
We'll use the Ed25519 algorithm for our SSH key, as it's more secure and performant than the older RSA algorithm:
ssh-keygen -t ed25519 -C "your_email@example.com"
Replace your_email@example.com with your actual GitHub email address.
When prompted:
- Enter file location: Press Enter to accept the default location (
~/.ssh/id_ed25519) - Enter passphrase: Choose a strong passphrase for additional security (recommended)
- Confirm passphrase: Re-enter the same passphrase
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/username/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Alternative: Using RSA Keys
If your system doesn't support Ed25519, use RSA with at least 4096 bits:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Step 3: Add Your SSH Key to the SSH Agent
First, start the SSH agent in the background:
eval "$(ssh-agent -s)"
Then add your SSH private key to the SSH agent:
ssh-add ~/.ssh/id_ed25519
If you used RSA, replace id_ed25519 with id_rsa.
Step 4: Copy Your Public Key
Display and copy your public key to your clipboard:
cat ~/.ssh/id_ed25519.pub
The output will look similar to this:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your_email@example.com
Select and copy the entire key, including the ssh-ed25519 prefix and your email address.
Step 5: Add Your SSH Key to GitHub
- Log in to your GitHub account
- Click on your profile picture in the top right corner
- Select Settings from the dropdown menu
- In the left sidebar, click SSH and GPG keys
- Click New SSH key or Add SSH key
- In the "Title" field, add a descriptive label for your key (e.g., "Personal Linux Laptop")
- In the "Key" field, paste your public key
- Click Add SSH key
- If prompted, confirm your GitHub password
Step 6: Test Your SSH Connection
Test your SSH connection to GitHub:
ssh -T git@github.com
You should see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
If this is your first time connecting, you may see a warning about the authenticity of the host. Type yes to continue.
Step 7: Configure Git to Use SSH
If you haven't already, configure Git with your GitHub credentials:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
Using Your SSH Key
Now you can clone repositories using SSH URLs instead of HTTPS:
# SSH URL format
git clone git@github.com:username/repository.git
# Instead of HTTPS
git clone https://github.com/username/repository.git
For existing repositories, you can change the remote URL:
git remote set-url origin git@github.com:username/repository.git
Troubleshooting
Permission Denied Error
If you get a "Permission denied" error, check:
1. Your SSH key is added to the SSH agent:
ssh-add -l
2. Your public key is correctly added to GitHub
3. You're using the correct SSH URL format
SSH Agent Not Running
If the SSH agent isn't running, start it manually:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Wrong Permissions on SSH Files
Fix SSH file permissions if needed:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
Security Best Practices
- Use passphrases: Always protect your private key with a strong passphrase
- Keep private keys private: Never share your private key (
id_ed25519) with anyone - Regular key rotation: Consider generating new keys periodically
- Use different keys: Consider using separate SSH keys for different services
- Monitor key usage: Regularly review your SSH keys in GitHub settings
Conclusion
You've successfully created an SSH key pair and configured it for use with GitHub. This setup provides a secure, password-free way to interact with your GitHub repositories. Remember to keep your private key secure and consider using SSH agent forwarding or SSH config files for more advanced setups.
For more advanced SSH configuration options, check out the SSH config documentation and GitHub's SSH troubleshooting guide.
Need help with Git workflows? Check out our guide on Git best practices for developers.
No comments:
Post a Comment