
How to Set Up SSH Keys for GitHub on Ubuntu
Table of Contents
- Step 1: Generate SSH Key
- Step 2: Add SSH Key to SSH Agent
- Step 3: Copy the Public Key
- Step 4: Add the SSH Key to GitHub
- Step 5: Test the SSH Connection
- Conclusion
GitHub enjoys widespread popularity as a platform for hosting and collaborating on software projects. To securely interact with your GitHub repositories from your Ubuntu machine, you can use SSH keys for authentication. SSH keys provide a more secure and convenient way to connect to your GitHub account without needing to enter your username and password each time. In this guide, we’ll walk you through the process of setting up SSH keys for GitHub on an Ubuntu system.
Step 1: Generate SSH Key
The first step is to generate an SSH key pair if you don’t already have one. Open a terminal and run the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Replace “your_email@example.com”
with the email linked to your GitHub account. You can use the default location for your SSH key or specify a different path.
Step 2: Add SSH Key to SSH Agent
To use your SSH key, it needs to be added to the SSH agent. Initiate the SSH agent by running the following command:
eval "$(ssh-agent -s)"
Then, insert your private key into the SSH agent:
ssh-add ~/.ssh/id_rsa
If your SSH key is in a different location or has a different name, make sure to replace ~/.ssh/id_rsa with the correct path.
Step 3: Copy the Public Key
Now, you need to copy the contents of your public key. You can do this by using the cat command to display the key in the terminal:
cat ~/.ssh/id_rsa.pub
Make sure to copy the entire key, including the ssh-rsa at the beginning.
Step 4: Add the SSH Key to GitHub
With the public key copied, it’s time to add it to your GitHub account:
- Log in to your GitHub account.
- Navigate to your profile picture in the upper-right corner and choose “Settings.”
- Access SSH and GPG keys on the left sidebar
- Click the “New SSH key” button.
- Give your key a descriptive title (e.g., “Ubuntu SSH Key”).
- Paste the copied public key into the “Key” field.
- Save your changes by clicking the “Add SSH key” button.
Step 5: Test the SSH Connection
To ensure that your SSH key is set up correctly, you can test your SSH connection to GitHub:
ssh -T git@github.com
You should receive a message like this:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
username
should be your GitHub username.
Conclusion
Congratulations! You’ve successfully set up SSH keys for your GitHub account on your Ubuntu machine. You can now securely interact with your GitHub repositories without the need for a password, making your development workflow smoother and more secure.