WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Set Up SSH Keys for GitHub

Edited 1 week ago by ExtremeHow Editorial Team

GitGitHubSSH KeysWindowsMacLinuxSecurityAuthenticationConfigurationRepositoryRemote Access

How to Set Up SSH Keys for GitHub

This content is available in 7 different language

Setting up SSH keys for GitHub is an essential skill for anyone working with Git repositories on the platform. SSH keys provide a secure way of authenticating with GitHub servers without having to type your username and password every time. Furthermore, it improves security by keeping your credentials secure and encrypted. The process involves creating a public and a private key pair and then configuring them with GitHub. This comprehensive guide will walk you through each step, simplify complex actions, and ensure your key setup is both functional and secure.

What is SSH?

SSH stands for Secure Shell. It is a cryptographic network protocol used to secure data communications, remote command-line logins, and other secure network services between two networked computers. SSH keys are an access credential in the SSH protocol. A pair of SSH keys consists of a private key and a public key. The private key is known only to you, while the public key can be shared with anyone, including the GitHub server.

Understanding SSH keys

SSH keys are like your digital identity card in the world of computer networking. They help you log in to a server or push code to a repository securely. An SSH key pair consists of:

Generating SSH keys

To use SSH keys with GitHub, you first need to create a new SSH key. Follow these steps:

Step 1: Check for existing SSH keys

Before creating a new SSH key, it is a good idea to check if you already have an existing key on your machine. Open your terminal and type:

> ls -al ~/.ssh

This command lists all the files present in your .ssh directory. Check for files named id_rsa and id_rsa.pub. These are the default names for the SSH key pair.

Step 2: Create new SSH key

If you don't have a key pair, or you want to create a new one, enter the following command:

> ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

The meaning of each part of the order is as follows:

After running this command, you will be asked to enter a file in which to save the keys. You can press Enter to accept the default location (this will overwrite existing keys in the default location), or specify a new location. Next, you will be asked for a passphrase, which is an additional layer of security. This is optional, but highly recommended.

Adding SSH keys to SSH-Agent

The next step is to add your SSH keys to SSH-Agent, which will handle your keys securely. To start SSH-Agent you need to:

> eval $(ssh-agent -s)

This command will run the agent and assign your existing session to it. Now add your SSH private key to the agent using the following command:

> ssh-add ~/.ssh/id_rsa

If you saved the file elsewhere, replace ~/.ssh/id_rsa with your file location.

Adding an SSH key to your GitHub account

Step 1: Copy the SSH key

Now you need to add the generated SSH public key to your GitHub account. First, you should copy the SSH key to the clipboard using the following command:

> clip < ~/.ssh/id_rsa.pub

If you are not using Windows, you can use

> cat ~/.ssh/id_rsa.pub

This will print your public key in the terminal. Select the text and copy it to your clipboard.

Step 2: Add to GitHub

1. Log in to your GitHub account.
2. In the upper-right corner of any page, click your profile photo, then click Settings.
3. In the left sidebar, click SSH and GPG keys.
4. Click New SSH Key or Add SSH Key.
5. In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal Mac, you could call this key "Personal MacBook."
6. Paste your key into the “Key” field.
7. Click Add SSH Key.
8. Confirm your GitHub password if prompted.

Verify SSH key configuration

After adding the SSH key to your GitHub account, you need to verify that everything is working correctly. Use the following command to test your connection:

> ssh -T git@github.com

You will see a success message like this:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

If you see a message confirming your identity, your SSH configuration is correct. If you get an error, recheck all the steps to make sure the public key is correctly added to your GitHub account and that your SSH-agent is managing your keys correctly.

Conclusion

Setting up SSH keys for GitHub may seem daunting at first, but it's a vital skill for anyone using a version control system. By following the steps described in this guide, you can securely connect to your GitHub repository without having to input your credentials again and again. Ensuring proper configuration of SSH keys not only streamlines your workflow but also enhances the security of your development environment. Remember that it's important to keep your private key safe and not share it with anyone, while you add your public key to any platform you want to authenticate against.

If you find anything wrong with the article content, you can


Comments