How to Generate SSH key for Git
- Checking PC's SSH Keys
- Generate a new SSH key
- Adding an SSH key to the ssh-agent
- How To Add SSH Key To GitHub/Bitbucket Account
- Why we need SSH key (for Linux and OSX)
A SSH key is considered an access credential for the secure shell (SSH) 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 on your PC if it exists, generate a new SSH key and learn how to add it to your ssh-agent 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:
ls -al ~/.ssh
By default, the filename of the public key is one of the following:
id_ecdsa.pub id_ed25519.pub id_rsa.pub
Generate a new SSH key¶
If you don't have an SSH key, 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:
ssh-keygen -t rsa -b 4096 -C "[your github's email]"
After running this command, you will be offered to set the SSH key 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_rsa): [Press enter]
After that it will offer you to insert a passphrase to protect your SSH key.
> 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 SSH key, you can add it into the ssh-agent.
- Be sure ssh-agent is enabled:
eval "$(ssh-agent -s)"
- Add your SSH key to the ssh-agent.
ssh-add ~/.ssh/id_rsaIf you changed the default path while generating an SSH key, now you need to mention that path instead of ~/.ssh/id_rsa.
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 SSH key to the GitHub/Bitbucket account helps the system to identify you and your are not asked to write your username and password each time.
Open your terminal and type the command below:
Now, it will print the SSH key 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 SSH key, 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 Add key, and press the Add key button.
Now you have added your PC's SSH key to your GitHub's/Bitbucket's account.
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.