github ssh protocol & https password caching


I am a Software Engineer and Clinical Social Worker based in San Francisco, CA | contact me or follow me.

Share

This post has information on how to establish ssh (secure shell) protocol and how to set up caching your password with https protocol for working with github.com. I gathered this information to help myself in my first week of class at bootcamp School in San Francisco in my beginning stages as a Software Engineering student. I was tired of repeatedly entering my username and password for working with github from inside my virtual machine using vagrant, virtual box and ubuntu. So I followed these steps myself, and found that other’s needed help with this process because github’s tutorial / help pages were incomplete; each step is explained on separate URLs. Therefore, I decided to merge all the steps and add some of my own tips from what worked for me, including tips about how to quickly re-add your SSH key to the SSH agent in the event that you are again repeatedly prompted to enter your password after logging out of terminal or another virtual machine through SSH.  Caching your password with HTTPS is another much faster way to avoid having to re-enter your password multiple times, and so that is also explained in this article.

Note: be sure to substitute USERNAME, REPOSITORY, YOUR NAME, and YOUR EMAIL with your personal github username, the repository of your choice, your name, and your e-mail used for your github account.  Also, note that all commands are to be executed in the terminal emulator of your choice.

special thanks to Kimberly Wong for her contributions, you can follow her on github here.

HTTPS protocol

Since Caching your password with HTTPS protocol is a much faster process and also uses the more common HTTPS protocol, which is what most github users begin with, I have outlined these steps first. It should be noted that the caching method is designed to function for minutes or hours at a time, as a per session support feature. This method outlined here is somewhat of a hack as it sets the caching time to 99999999 seconds, which is 1157 days or 3.17 years.

(1) Add or Set remote url through command line

add remote URLs (add)

if you are adding a remote url for the first time, and you have not already established SSH, then you may simply use the following code after establishing your Github account:

$ git remote add origin https://github.com/USERNAME/REPOSITORY.git

set remote URLs from SSH to HTTPS (set-url)

  1. Change the current working directory to your local project.
  2. List your existing remotes in order to get the name of the remote you want to change.
    $ git remote -v
    origin  git@github.com:USERNAME/REPOSITORY.git (fetch)
    origin  git@github.com:USERNAME/REPOSITORY.git (push)
    
  3. Change your remote's URL from SSH to HTTPS with the $ git remote set-url command.
    $ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git
    
  4. Verify that the remote URL has changed, with the same previous command: git remote -v

(2) Set username and e-mail in your .gitconfig file

The following 2 commands set your github username and e-mail address for your system's .gitconfig file. To complete these codes manually, use the code listed in the section below, labeled "Manual Process."

$ git config --global user.name "YOUR NAME"
$ git config --global user.email "YOUR EMAIL"

(3) Add Caching to your .gitconfig file

To simply add caching with the default timeout of 15 minutes type:

$ git config --global credential.helper cache

The following code adds an extra step of a cache timeout of 3.17 years:

$ git config --global credential.helper 'cache --timeout=99999999'

If you would like to manually perform these steps, simply add the following code your .gitconfig file:
Manual Process:

[user]
        name = YOUR NAME
        email = YOUR EMAIL
[credential]
        helper = cache --timeout=99999999

SSH protocol

(1) Add or Set remote url through command line

if you are adding a remote url for the first time, and you have not already established HTTPS, then you may simply use the following code after establishing your Github account:

add remote URLs (add)

$ git remote add origin git@github.com:USERNAME/REPOSITORY.git

set remote URLs from HTTPS to SSH (set-url)

  1. Change the current working directory to your local project.
  2. List your existing remotes in order to get the name of the remote you want to change.
    $ git remote -v
    origin https://github.com/USERNAME/REPOSITORY.git (fetch)
    origin https://github.com/USERNAME/REPOSITORY.git (push)
  3. Change your remote's URL from HTTPS to SSH with the $ git remote set-url command.
    $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
  4. Verify that the remote URL has changed, with the same previous command: git remote -v

(2) Generate a new SSH key through command line

Before adding a new SSH key to the ssh-agent to manage your keys, you should check for existing SSH keys.

  1. Paste the text below, substituting in your GitHub email address.
  2. $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    

    This creates a new ssh key, using the provided email as a label.

    Generating public/private rsa key pair.
    
  3. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
    Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
    
  4. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".
    Enter passphrase (empty for no passphrase): [Type a passphrase]
    Enter same passphrase again: [Type passphrase again]
    

Adding your SSH key to the ssh-agent** (see below note)

  1. To add the ssh-key to the ssh-agent, the ssh-agent must be running. On OS X Leopard or later, the ssh-agent runs automatically for you. For more information, see "Working with ssh key passphrases." If the ssh-agent isn't already running, start the ssh-agent.
    # start the ssh-agent in the background if it's not already running
    $ eval "$(ssh-agent -s)"
    # your output will look like this: Agent pid 59566
    
  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.
    $ ssh-add ~/.ssh/id_rsa
    

(3) add new SSH key to github.com account through preferred browser

  • Copy the SSH key to your clipboard.If your SSH key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any newlines or whitespace. The below command copies the contents of the id_rsa.pub file to your clipboard.
    $ pbcopy < ~/.ssh/id_rsa.pub
    

    Tip: If pbcopy isn't working, you can locate the key in the ~/.ssh/ folder, open the key file id_rsa.pub in vim or other text editor and copy it to your clipboard.  Emacs did not function for me, and so I used vim to copy and paste into the github.com settings.

  • In the upper-right corner of any page, click your profile photo, then click Settings.
  • In the user settings sidebar, click SSH and GPG keys.
  • Click New SSH key or Add SSH key.
  • In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal Mac, you might call this key "Personal MacBook Air".
  • Paste your key into the "Key" field.
  • Click Add SSH key.
  • If prompted, confirm your GitHub password.

(4) **Note: adding SSH key to SSH agent

You may have to repeat the 2 commands from the above step "Adding your SSH key to the ssh-agent**" process after logging out of your Terminal or opening up a new window. I have them listed here again with example output from my terminal using vagrant, virtual box, and ubuntu.

$ eval "$(ssh-agent -s)"
Agent pid 2257
$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/vagrant/.ssh/id_rsa:
Identity added: /home/vagrant/.ssh/id_rsa (/home/vagrant/.ssh/id_rsa)

information gathered from: help.github.com

Posted in code, github and tagged , , , , .