Using Git with SSH Keys
In this article, you will learn about SSH, SSH keys and their usage in your Git repository.
What is SSH?
SSH stands for Secure Shell. SSH is a network protocol which is used for remote communication between machines. Additionally, SSH describes a set of tools used to interact with the SSH protocol. SSH is used for e.g. remote file transfer and remote operating system access.
What is an SSH key?
An SSH key is an access credential for the SSH network protocol. SSH uses key pairs to establish a secure connection between machines. These pairs contain of a private and a public key. Before you can use SSH, you have to create an SSH key.
How to create an SSH key?
For Windows users, I recommend using the GitBash, to have a unix shell available. To create an SSH key, you need to follow these steps:
- Create a new ssh key with the provided email as a label:
$ ssh-keygen -t ed25519 -b 4096 -C "firstname.lastname@example.org"
- After that, you will be prompted to enter a file name in which the key should be stored. You can choose a name and file location or press Enter to accept the default.
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
- You then have the possibility to secure your key with an additional layer by adding a passphrase. This passphrase will then have to be re-entered every time the key is used.
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
- The SSH key needs to be added to the ssh-agent
- Start the ssh-agent:
$ eval "$(ssh-agent -s)"
Agent pid 7933
- Add the key to the ssh-agent:
$ ssh-add ~/.ssh/id_rsa
- Add the SSH key to your repository
- Copy the SSH key to your Clipboard:
$ pbcopy < ~/.ssh/id_rsa.pub
- Add the public ssh key to your repository:
You now have to go to the settings of your repository and look for the security settings/ssh keys. This differs from repository to repository. You then have to click that you want to add a new key, select a descriptive title (to identify what the key is for, e.g. “personal Laptop”, “work Laptop”) and paste your key into the key field. Save. Done.