How to Add SSH Key to GitHub

Setting up SSH keys for GitHub can seem like a daunting task, but it’s crucial for secure, efficient access to your repositories. If you’ve ever struggled with HTTPS prompts or credential management, understanding how to add an SSH key to GitHub is a game-changer.
In this guide, I’ll walk you through the process step-by-step—generating SSH keys, configuring them, and finally, adding them to your GitHub account. You’ll also discover how to ensure seamless SSH authentication, manage your keys, and optimize security.
By the end of this article, you will not only grasp how to add SSH keys to GitHub but also understand how this setup can enhance your workflow, ensuring smoother code commits and repository access. Let’s dive in and make GitHub access both secure and hassle-free.
How To Add SSH Key To GitHub: Quick Workflow
To add an SSH key to your GitHub account, follow these detailed steps:
Step 1: Generate an SSH Key Pair
- Open a Terminal or Command Prompt:
- On Linux or macOS, open the terminal.
- On Windows, use Git Bash or Command Prompt.
- Create the SSH Key:
Run the following command to generate a new SSH key pair:ssh-keygen -o -t rsa -C "[your_email@example.com]"
Replace
[your_email@example.com]
with your actual email address. When prompted, press Enter to accept the default file location (usually~/.ssh/id_rsa
). You can also choose to enter a passphrase for added security or leave it blank by pressing Enter twice.
Step 2: Copy the Public Key
- Locate and Copy the Public Key:
After generating the key, you need to copy the public key to your clipboard. You can display it using:cat ~/.ssh/id_rsa.pub
Then, select and copy the entire output.
Step 3: Add the SSH Key to GitHub
- Log into GitHub:
Go to GitHub and log into your account. - Navigate to SSH Keys Settings:
- Click on your profile icon in the upper right corner.
- Select Settings from the dropdown menu.
- In the left sidebar, click on SSH and GPG keys.
- Add a New SSH Key:
- Click on the New SSH key button.
- In the “Title” field, give your key a descriptive name (e.g., “My Laptop SSH Key”).
- Paste your copied public key into the “Key” field.
- Click on Add SSH key to save it.
Step 4: Test Your Connection
- Verify Your SSH Connection:
To ensure that your SSH key is set up correctly, run the following command in your terminal:ssh -T git@github.com
If everything is configured properly, you should see a message confirming your successful authentication.
Preparing Your System for SSH Setup
Checking for Existing SSH Keys
Commands to verify existing keys on the system
Before creating new SSH keys, you need to check if you already have some. Open your terminal and type:
ls -al ~/.ssh
This command lists files in the .ssh
directory. Look for files like id_rsa
and id_rsa.pub
. If these exist, you already have keys.
Importance of identifying and managing existing keys
It’s crucial to know what keys you have to avoid conflicts. Mismanaged keys can lead to security risks or access issues. Properly identifying existing keys lets you manage your GitHub account more efficiently, ensuring smooth operations when pushing or cloning repos.
Creating the .ssh Folder
Using mkdir to organize SSH-related files
If the .ssh
directory doesn’t exist, you’ll need to create it. Use the following command:
mkdir -p ~/.ssh
This command creates the directory while ensuring that no errors occur if the directory is already present. Organizing your SSH keys here keeps everything neat and manageable.
Navigating and verifying the folder structure
Navigate into the newly created directory with:
cd ~/.ssh
Then, verify its content by typing:
ls
Generating SSH Keys

Generating an RSA Key Pair
Explanation of RSA encryption and its security advantages
RSA encryption is a widely-used algorithm known for its robustness against cyber threats. It provides high security by leveraging cryptographic key pairs—a public key for encryption and a private key for decryption. This makes operations like pushing code to GitHub both secure and efficient.
Using the ssh-keygen command
To generate an RSA key pair, open your terminal and run:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- Flags such as -b for key size and -t for type
The -b 4096
flag specifies the key size, enhancing security with larger keys. The -t rsa
flag sets the key type to RSA, which is the default.
- Setting optional passphrases for enhanced security
After running the command, you’ll be prompted to enter a passphrase. This is optional but highly recommended. It adds an extra layer of security, ensuring that even if your private key is compromised, the passphrase is still required for access.
Storing and Managing SSH Keys
Default locations for public and private keys
SSH keys are typically stored in the .ssh
directory in your home folder. The public key file is named id_rsa.pub
, while the private key file is id_rsa
. These default locations make it easy for SSH to find and use your keys.
Differentiating between public (id_rsa.pub) and private (id_rsa) keys
- Public Key (
id_rsa.pub
)This key can be shared freely and uploaded to GitHub. It allows systems to encrypt data that only your private key can decrypt. - Private Key (
id_rsa
)This key must be kept secure on your local machine. Never share it with anyone. It’s used to decrypt messages encrypted by the public key and to sign operations like pushing code.
Best practices for keeping private keys secure
- Store privatelyKeep your private key (
id_rsa
) on your local machine and never share it online. - Use a passphraseAdding a passphrase, as mentioned earlier, adds an additional security layer.
- Regularly rotate keysTo further secure your operations, consider rotating your SSH keys periodically. This reduces the risk of unauthorized access in case an existing key is somehow compromised.
Adding SSH Keys to Your GitHub Account
Accessing GitHub Settings
Logging into GitHub and navigating to the “SSH and GPG Keys” section
Start by logging into your GitHub account. At the top-right corner, click on your profile picture and select Settings. In the sidebar, scroll down to SSH and GPG keys. This section allows you to manage and add new SSH keys for authentication.
Adding a New SSH Key
Steps to paste the public key into GitHub
First, generate your public key if you haven’t already. Open your terminal and run:
cat ~/.ssh/id_rsa.pub
This command will display your public key. Copy the entire key. Now, head back to the SSH and GPG keys section on GitHub and click on New SSH key.
- Naming the key for easy identificationIn the title field, give your key a name you’ll remember, like My Laptop Key.
- Copying the key using cat or lessPaste the key that you copied from the terminal into the “Key” field on GitHub.
Confirming the addition with GitHub account password
Finally, click Add SSH key. GitHub will prompt you to confirm the action by entering your account password. After confirmation, your SSH key is added, providing secure access to your repositories. Now, you are all set for SSH-based interactions.
Using SSH Keys for GitHub Operations
Configuring Repositories for SSH
Replacing HTTPS URLs with SSH URLs in existing repositories
Switching from HTTPS to SSH? Simple. Navigate to your repository’s settings on GitHub. Copy the SSH URL. Now, open your terminal and go to your local repository.
Run:
git remote set-url origin git@github.com:username/repo.git
You’ve now configured your repo to use SSH.
Updating local repository settings
Ensure your local configuration aligns. Check your .git/config
file to verify the remote URL is now SSH. This reinforces that future Git commands like push
and pull
use the secure SSH key for authentication.
Cloning Repositories via SSH
Steps to copy the SSH URL from a repository
Head to the repository on GitHub. Click the green Code button and select the SSH option. Copy the URL provided.
Cloning with git clone and handling authenticity checks
Run the following in your terminal:
git clone git@github.com:username/repo.git
First time? You’ll be asked to verify the repository’s authenticity. Type yes
to continue. The repository will clone using your SSH key.
Performing Git Operations
Using SSH to push and pull changes securely
SSH enhances every Git operation—pushing code, pulling changes, everything gets a security boost. Simplify with:
git push origin main
git pull origin main
No more password prompts. Just efficient workflows.
Common troubleshooting tips for SSH-related errors
Facing Permission denied
errors? Check if your SSH key was added correctly using ssh-add -l
. Key missing? Add it again with:
ssh-add ~/.ssh/id_rsa
Advanced Tips and Best Practices
Using SSH for Commit Signing
How to sign commits with SSH keys for identity verification
Git commit signing with SSH keys adds an extra layer of authenticity to your commits. To start, generate a new signing key:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/git_signing_key
Now add this key to your SSH agent:
ssh-add ~/.ssh/git_signing_key
Configuring Git to use SSH signing keys
Tell Git to use your new signing key. Open your global Git config file:
git config --global user.signingkey ~/.ssh/git_signing_key
git config --global commit.gpgSign true
This ensures all commits are signed, verifying your identity automatically.
Maintaining SSH Key Security
Regularly rotating keys for improved security
Keep your SSH keys fresh. Rotate them every 6-12 months. Generate a new key pair and replace the old one on all platforms, including GitHub settings.
Avoiding sharing or misplacing private keys
Security 101: Never share your private key. Keep it in your ~/.ssh
directory, and ensure permissions are set to prevent unauthorized access:
chmod 600 ~/.ssh/id_rsa
Troubleshooting SSH Issues
Addressing permission denied errors
Facing Permission denied
errors? First, check if your SSH key was added to the agent:
ssh-add -l
No keys? Add it manually:
ssh-add ~/.ssh/id_rsa
Resolving issues with outdated key algorithms (e.g., SHA-2 requirements)
Some servers now require SHA-2. If you encounter issues, update your ssh-keygen
command to use modern algorithms:
ssh-keygen -t rsa -b 4096 -m PEM
FAQ on How To Add SSH Key To GitHub
What is an SSH key and why do I need it for GitHub?
SSH keys are encryption keys used to secure communication between your local machine and GitHub. They eliminate the need for a password and enhance overall security. By using SSH keys, you can authenticate seamlessly when pushing or cloning repositories.
How do I generate an SSH key on my computer?
Open your terminal. Type ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
. Press Enter to accept default file location. Enter a passphrase for added security, or press Enter to skip. Your SSH key pair (public and private keys) is now generated.
Where do I find my generated SSH key?
By default, your generated SSH keys are stored in the ~/.ssh
directory on your machine. Look for the files named id_rsa
(private key) and id_rsa.pub
(public key). You’ll need the public key for adding it to GitHub.
How do I add the SSH key to the SSH agent?
Start the SSH agent with eval "$(ssh-agent -s)"
. Then, add your SSH key by running ssh-add ~/.ssh/id_rsa
. This ensures your key is available for SSH operations, making authentication automatic whenever needed.
How do I add my SSH key to my GitHub account?
Copy your public key content using cat ~/.ssh/id_rsa.pub
. Go to GitHub, navigate to Settings > SSH and GPG keys. Click New SSH key, paste your key, give it a title, and save it. Your SSH key is now linked to your GitHub account.
What should I do if my SSH key isn’t working?
First, check if the SSH agent is running and your key has been added with ssh-add -l
. Ensure the key is correctly copied to GitHub. If issues persist, use ssh -T git@github.com
to test the connection and diagnose problems.
Can I use multiple SSH keys for different GitHub accounts?
Yes, you can manage multiple SSH keys by specifying different configurations in your ~/.ssh/config
file. For each account, add an entry like:
Host github-username
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_username
How do I remove an SSH key from GitHub?
To remove an SSH key, go to your GitHub Settings > SSH and GPG keys. Find the key you want to delete, and click the Delete button next to it. Confirm the action, and the key will be removed.
Is it safe to share my public SSH key?
Yes, the public SSH key is meant to be shared. It’s safe to upload to GitHub or other servers you’re accessing. Never share your private SSH key as it should always remain confidential and secure on your local machine.
How often should I rotate my SSH keys?
Rotating your SSH keys periodically is good practice to maintain security. Ideally, rotate them every 6-12 months or if you suspect they may have been compromised. Update GitHub and any other services with the new public key.
Conclusion
Adding an SSH key to GitHub is a straightforward process that significantly enhances security and convenience. After following this guide on how to add SSH key to GitHub, you should now feel confident in executing each step, from generating your SSH key to configuring it in both your local machine and GitHub account.
Key Takeaways:
- Create and locate your SSH keys using commands in your terminal.
- Add the SSH key to the SSH agent to make authentication smoother.
- Copy and paste your public key to GitHub under the SSH and GPG keys section.
By completing these steps, you achieve a more secure and efficient way to manage your repositories. The benefits include seamless Git commands, secure access, and revoked need for password prompts when interacting with your repositories.
Now, you can focus more on your coding tasks and less on tedious authentication issues. This process ensures your projects are securely managed and easily accessible through GitHub.
If you liked this article about how to add SSH key to GitHub, you should check out this article about how to delete a repository in GitHub.
There are also similar articles discussing how to clone a GitHub repository, how to use GitHub Copilot, how to pull from GitHub, and how to delete a branch in GitHub.
And let’s not forget about articles on how to merge branches in GitHub, how to commit to GitHub, how to change GitHub username, and how to host a website on GitHub.
- How CRM Enrichment Transforms Your B2B Strategy - February 24, 2025
- How to Organize Apps on iPad for Better Access - February 23, 2025
- Unlocking Success with Innovative Node Development Services - February 23, 2025