8/21/25

How to Create SSH Keys on Linux and Connect to GitHub

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
[Screenshot placeholder: Terminal showing output of ls -la ~/.ssh command]

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.

[Screenshot placeholder: Terminal showing ssh-keygen command execution]

When prompted:

  1. Enter file location: Press Enter to accept the default location (~/.ssh/id_ed25519)
  2. Enter passphrase: Choose a strong passphrase for additional security (recommended)
  3. Confirm passphrase: Re-enter the same passphrase
📋 Copy
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:

📋 Copy
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:

📋 Copy
eval "$(ssh-agent -s)"
[Screenshot placeholder: Terminal showing SSH agent starting with process ID]

Then add your SSH private key to the SSH agent:

📋 Copy
ssh-add ~/.ssh/id_ed25519

If you used RSA, replace id_ed25519 with id_rsa.

[Screenshot placeholder: Terminal showing successful key addition to SSH agent]

Step 4: Copy Your Public Key

Display and copy your public key to your clipboard:

📋 Copy
cat ~/.ssh/id_ed25519.pub
[Screenshot placeholder: Terminal displaying the public key content]

The output will look similar to this:

📋 Copy
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

  1. Log in to your GitHub account
  2. Click on your profile picture in the top right corner
  3. Select Settings from the dropdown menu
[Screenshot placeholder: GitHub profile dropdown menu]
  1. In the left sidebar, click SSH and GPG keys
  2. Click New SSH key or Add SSH key
[Screenshot placeholder: GitHub SSH keys settings page]
  1. In the "Title" field, add a descriptive label for your key (e.g., "Personal Linux Laptop")
  2. In the "Key" field, paste your public key
  3. Click Add SSH key
[Screenshot placeholder: GitHub add SSH key form filled out]
  1. If prompted, confirm your GitHub password

Step 6: Test Your SSH Connection

Test your SSH connection to GitHub:

📋
ssh -T git@github.com
[Screenshot placeholder: Terminal showing SSH connection test]

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