Skip to content

Instantly share code, notes, and snippets.

@albertpark
Last active May 14, 2020 01:33
Show Gist options
  • Save albertpark/4f301d6f6ec45ae84d3e58209903a97e to your computer and use it in GitHub Desktop.
Save albertpark/4f301d6f6ec45ae84d3e58209903a97e to your computer and use it in GitHub Desktop.

SSH Getting Started for a Repository

This guide all started because of the infamous Permission Denied (publickey) when trying to push code to my repository. Before we can start using repositories intensively there are prequisties that we must complete in order to get our work flowing.

We will be using git bash (Use the Windows search. To find it, type "git bash") or the Mac Terminal. You can also use the default Windows Command Prompt by installing git from git-scm.
Tip: You can use any *nix based command prompt

1. SSH Agent

First thing is first, we must first identify if the ssh-agent is enabled and running properly. If this is not an issue go to step 2.

Linux
In order to start the ssh-agent you will need to run the following command.

[~/] $ eval `ssh-agent -s`
Agent pid 2501669

Windows 10
For Windows we will be using the powershell utility.

> Get-Service ssh-agent

And then check the output of status is not running.

Status   Name               DisplayName
------   ----               -----------
Stopped  ssh-agent          OpenSSH Authentication Agent

Then check that the service has been disabled by running

> Get-Service ssh-agent | Select StartType
StartType
---------
Disabled

I suggest setting the service to start manually. This means that as soon as you run ssh-agent, it'll start the service. You can do this through the Services GUI or you can run the command in admin mode:

 > Get-Service -Name ssh-agent | Set-Service -StartupType Manual

Alternatively, you can set it through the GUI if you prefer.

Make sure the ssh-agent is enabled and running.

2. Find the SSH Public Key

You need to generate a new ssh identity in order to add them to the agent. First we will go to the .ssh directory to create a ssh identity.

[~/] $ cd ~/.ssh
[~/.ssh] $

For Windows it will be

C:\Users\[YOUR_USER_NAME]\.ssh\

There should be these two files in this directory.

id_rsa
id_rsa.pub

We are specifically looking for id_rsa.pub file as this contains the public key for this ssh identity. If these files exist you may skip to step 6.

3. Create a SSH Identity

Important! It is crucial that you do not change the default identity name. The identity names must be id_rsa and id_rsa.pub in order for the public repositories to recognize them.

[~/] $ ssh-keygen -t rsa -C "your_email@example.com"
 Generating public/private rsa key pair.
 Enter file in which to save the key (~/.ssh/id_rsa):
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in ~/.ssh/id_rsa.
 Your public key has been saved in ~/.ssh/id_rsa.pub.
 The key fingerprint is:
 SHA256:CDmvEjzFM11l7gzPQYXAtpb4ODm/nLAr4LVIA8/VAGs your_email@example.com
 The key's randomart image is:
 +---[RSA 2048]----+
 |  ..   .oo+o.    |
 |   o.o .o+.      |
 |  E Oo.o.oo      |
 |.o ..*o.+* .     |
 | ++.  o=S =      |
 |  *o..= .        |
 | o.=...+         |
 |  o.o  +..       |
 |     .o.+.       |
 +----[SHA256]-----+

Here is a successful message when generating a new ssh key with a passphrase.
Keep in mind the passpharse is optional with a minimum length of greather than 4 characters.

4. Add the Identity to the SSH Agent

Now, we want to add this to our agent so it can identify the workstation we are using.

[~/] $ ssh-add
Enter passphrase for ~/.ssh/id_rsa:
Identity added: ~/.ssh/id_rsa (~/.ssh/id_rsa)

5. Display SSH Identities

The follow command will display list of all the ssh identities attached to the agent of the workstation you will be using.

[~/] $ ssh-add -l
2048 SHA256:CDmvEjzFM11l7gzPQYXAtpb4ODm/nLAr4LVIA8/VAGs ~/.ssh/id_rsa (RSA)

Once we see there is the ssh identity we need to get the public key.

The agent has no identities.

On the other hand, if you see this message something went wrong and try again from step 3.

6. Linking to a Repository

Open the id_rsa.pub file in a text editor and copy the content exactly to paste it into your favorite repostory site under the Account Settings > SSH Keys. NOTE: It is recommended to give the SSH key a descriptive name, such as the name of the workstation.

All done. Now you can start pushing or pulling code from your repository from a command prompt or terminal.


Extra: Remove SSH Identities If you ever wish to remove your attached ssh identity from the agent you can run the following command.

[~/] $ ssh-add -d
Identity removed: ~/.ssh/id_rsa (uname@uworkstation)

Alternatively, if you want to remove all of the identities there is a quick command for that.

[~/] $ ssh-add -D
All identities removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment