How to generate ssh key for git
If you are interested in git, you probably know how powerful git is. In this article you can find:
- Why we need SSH key
- How to find my pc's SSH key
- How to generate new SSH key
- How to connect my pc with my github account with SSH key
Why we need SSH key ( for Linux and OSX)
As you know, if you use git and want to clone anything from remote repositories, you have to choose one of these 2 ways: HTTPS or SSH. If you use HTTPS way, you have to type your account's access every time you communicate with remote repository, or change your configs and fill your account's data (access),but there is one way too, which is more modern way. That is SSH authentication method.It's used in many Version Control Systems (as git), to have command line access into your servers etc... SSH key pairs can be used instead of passwords for authentication, and each key pair consists of a private key and a corresponding public key. When you use SSH key for git, you say git that this PC is authenticated for that github's account, and never ask me again about any access, because I have already given you my SSH key (for this pc) and told you which access has this SSH key (this PC).
Now lets find out how we can checkout PC's SSH keys.
Checking My PC's SSH Keys
Step 1: Open Terminal
Step 2: Type ls -al ~/.ssh to see your ssh keys
ls -al ~/.ssh # Your .ssh files lists directory, if they exist
By default, the filenames of the public keys are one of the following:
id_dsa.pub id_ecdsa.pub id_ed25519.pub id_rsa.pub
Generate a new SSH key.Add to the ssh-agent
Step 1: Open Terminal
Step 2: Type this below, using your GitHub's account email
ssh-keygen -t rsa -b 4096 -C "[your github's email]" # Creates a new ssh key # Generating public/private rsa key pair.
Step 3: It will show up this text and you can hit Enter
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
Step 4: In this section you can hit Enter too or type secure passphrase (more about passphrase).
> Enter passphrase (empty for no passphrase): [Type a passphrase] > Enter same passphrase again: [Type passphrase again]
Adding SSH key to the ssh-agent
Now lets find out how we can add SSH key to ssh-agent.
Before adding, check your ssh keys or generate new key.
Step 1: Be sure ssh-agent is enabled
# start the ssh-agent in the background eval "$(ssh-agent -s)" > Agent pid 59566
Step 2: Add your SSH key to the ssh-agent.If you used an existing SSH key rather than generating a new SSH key, you'll need to replace id_rsa in the command with the name of your existing private key file.
How To Add SSH Key To Github Account
Step 1: Login into your Github's account.In the top right corner of any page, click your profile photo, then click Settings.
Step 2: In the user settings sidebar, go to SSH and GPG keys.
Step 3: Click New SSH key.
Step 4: Type Title and your SSH Key
You can get your ssh key by typing below.
cat .ssh/id_rsa.pub # something like this # ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5uy0aRcQkRDwBK0c+MRw4lnVwQW9qYKtMRdjHdHUwPYqV9c8jax ....
Step 5: then hit Add SSH key
Now you have added your PC's SSH key to your Github's account.