Are you tired of constantly typing in your username and password every time you want to push changes to your repository? If so, it’s time to add your public SSH key to your GitHub account!
But what is an SSH key, you might ask? An SSH key is a unique identification that is used to securely authenticate a connection between two computers. It consists of a pair of keys, a private key and a public key. The private key is kept on your computer and the public key is shared with the remote server. When you connect to the server, it checks the public key against your private key to verify your identity.
Now, let’s see how to add your public SSH key to your GitHub account.
Step 1: Generate an SSH key
First, we need to generate an SSH key. Open up your terminal and type in the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This will generate an SSH key with the specified algorithm (rsa), key size (4096 bits), and comment (your email address). It will also prompt you to enter a file to save the key and a passphrase (optional).
Step 2: Add the public key to your GitHub account
Next, copy the public key from the specified file and add it to your GitHub account.
cat ~/.ssh/id_rsa.pub
This will output your public key. Copy the entire key, including the ssh-rsa
and your_email@example.com
at the beginning and end.
Now, go to your GitHub account settings and click on the “SSH and GPG keys” tab. Click on the “New SSH key” button and paste your public key into the “Key” field. Give it a memorable title and click “Add SSH key”.
Step 3: Test the connection
To test the connection, run the following command:
ssh -T git@github.com
If everything is set up correctly, you should see a message saying “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”
Bonus tip:
Did you know that you can use an SSH key to authenticate with multiple GitHub accounts? All you need to do is add a different public key for each account and specify which key to use in your ~/.ssh/config
file.
Host github.com-user1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user1
Host github.com-user2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_user2
With this setup, you can simply use ssh -T github.com-user1
to authenticate with your first account and ssh -T github.com-user2
to authenticate with your second account.
Key points to remember:
- An SSH key consists of a pair of keys, a private key and a public key.
- The private key is kept on your computer and the public key is shared with the remote server.
- To add your public SSH key to your GitHub account, go to your account settings and click on the “SSH and GPG keys” tab.
- You can use an SSH key to authenticate with multiple GitHub accounts
5 tips
- Faster authentication: With an SSH key, you don’t have to enter your username and password every time you push changes to your repository. This saves time and reduces the risk of typos.
- Two-factor authentication: You can set up two-factor authentication with an SSH key, providing an extra layer of security for your account.
- Multiple accounts: As mentioned in the bonus tip, you can use an SSH key to authenticate with multiple GitHub accounts. This allows you to switch between accounts without having to log out and log back in.
- Automation: You can use an SSH key to authenticate with remote servers and automate tasks using scripts. This can save time and reduce the risk of human error.
- Collaboration: You can share your public SSH key with team members to allow them to push changes to your repository. This makes collaboration easier and more efficient.
Challenge:
Now that you know how to add an SSH key to your GitHub account, try setting up an SSH key on your own. Follow the steps outlined in this blog and test the connection by running the ssh -T git@github.com
command.