How to Generate SSH key for Git
SSH is a network protocol that helps to login from one computer to another securely. In this tutorial, you will read how to generate the SSH key for Git.
A SSH key is considered an access credential for the secure shell (<kbd class="highlighted">SSH</kbd>) network protocol. It is an encrypted and authenticated secure network protocol, applied for remote communication between machines operating on an unsecured network. This snippet will help you to find the <kbd class="highlighted">SSH key</kbd> on your PC if it exists, generate a new <kbd class="highlighted">SSH key</kbd> and learn how to add it to your <kbd class="highlighted">ssh-agent</kbd> or your GitHub/Bitbucket account .
Checking PC's SSH Keys
You can do that by following the steps below. Type ls -al ~/.ssh so as to see your ssh keys:
Checking PC's SSH Keys
ls -al ~/.sshBy default, the filename of the public key is one of the following:
id_ecdsa.pub
id_ed25519.pub
id_rsa.pubGenerate a new SSH key
If you don't have an <kbd class="highlighted">SSH key</kbd>, you should create it. Now let’s generate a new SSH key step by step.
Type the command below, using your GitHub's account email:
Generate a new SSH key using your GitHub's account email
ssh-keygen -t ed25519 -C "[your github's email]"After running this command, you will be offered to set the <kbd class="highlighted">SSH key</kbd> path, but we recommend you to use its default value by pressing the "Enter" button.
> Enter a file in which to save the key (/Users/you/.ssh/id_ed25519): [Press enter]After that it will offer you to insert a passphrase to protect your <kbd class="highlighted">SSH key</kbd>.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]Adding an SSH key to the ssh-agent
Now, when you already have an <kbd class="highlighted">SSH key</kbd>, you can add it into the <kbd class="highlighted">ssh-agent</kbd>.
- Be sure
<kbd class="highlighted">ssh-agent</kbd>is enabled:
eval "$(ssh-agent -s)"
eval "$(ssh-agent -s)"- Add your SSH key to the
<kbd class="highlighted">ssh-agent</kbd>.
Add your SSH key to the ssh-agent
ssh-add ~/.ssh/id_ed25519If you changed the default path while generating an <kbd class="highlighted">SSH key</kbd>, now you need to mention that path instead of ~/.ssh/id_ed25519 .
How To Add SSH Key To GitHub/Bitbucket Account
Usually, repositories are private, and the developers are required to write a username and a password to take any actions related to the remote repository. Adding your <kbd class="highlighted">SSH key</kbd> to the GitHub/Bitbucket account helps the system to identify you and you're not asked to write your username and password each time. Open your terminal and type the command below:
get your ssh key
cat ~/.ssh/id_ed25519.pubNow, it will print the <kbd class="highlighted">SSH key</kbd> in the terminal, so you can copy it.
How to Add SSH Key to the GitHub Account
- Log into your GitHub's account. In the top right corner of any page, click your profile photo, then click Settings.
- In the user settings sidebar, click on SSH and GPG keys.
- Click on the New SSH key button.
- Type the title and your
<kbd class="highlighted">SSH key</kbd>, and press the Add SSH key button.
How to Add SSH Key to the Bitbucket Account
- Log into your Bitbucket's account. In the left bottom corner of any page, click your profile photo, then click Personal Settings.
- In the Personal settings sidebar, click on SSH keys.
- Click on the Add key button.
- Type the title and your
<kbd class="highlighted">SSH key</kbd>, and press the Add key button.
Verifying the SSH Connection
After adding your key to the remote service, verify the connection works:
ssh -T [email protected](Replace github.com with [email protected] for Bitbucket.) You should see a success message confirming authentication.
Why we need SSH key (for Linux and OSX)
SSH keys are an access credential used in SSH protocol (Secure Shell) which is a network protocol that helps to login from one computer to another securely, as well as to manage networks, operating systems, and configurations. If you use Git and want to clone anything from remote repositories, you have to choose one of these two ways: HTTPS or SSH. If you use HTTPS, you have to type your account access every time you communicate with the remote repository, or change your configs and fill your account data (access). Another modern way is to use the SSH authentication method. It is used in many Version Control Systems to have command line access into your servers, etc. SSH key pairs can be used for authentication instead of passwords. Each key pair consists of a private key and a corresponding public key. When you use SSH key for Git, you inform Git that this PC is authenticated for that GitHub account, and it will never ask you about any access again because you have already given it your SSH key.