GIT SSH - gecko-8/devwiki GitHub Wiki

Up

Create an SSH Key

  1. Navigate to your .ssh folder, creating it if necessary. On Windows this should be under C:\Users\Jason\.ssh. On MacOS and Linux this will be a hidden folder at ~:/.ssh.
  2. Execute the following command to create the key.
    ssh-keygen -t rsa -b 4096 -C "[email protected]"
    
  3. When prompted, set a name however you like.
  4. When prompted, enter and confirm a passphrase for the key.

Edit .ssh\config File (if key name other than default)

  1. Enter the following into your .ssh\config file (following example is for Github)
    Host github.com
            User git
            Hostname github.com
            PreferredAuthentications publickey
            IdentityFile ~/.ssh/<your key name>
    

Set File Permission (Linux only)

Fix Permission Handling in WSL (Not needed for general Linux)

  1. Edit the ~/etc/wsl.conf
  2. Add the following
    [automount]
    enabled = true
    root = /
    options = "metadata"
    mountFsTab = true
    
  3. Restart the instance

Set Permissions

  1. Execute the commands
    chmod 600 <your key name>
    chmod 600 config
    

Start the SSH Agent

Default ssh-agent Included With Windows 10

  1. Open the Windows menu and type "Services"
  2. Find the OpenSSH Authentication Agent entry
  3. Right-click it and choose Properties
  4. Set Startup Type to Automatic
  5. Click Okay

Linux

Should already be running. You can check with the following command

eval "$(ssh-agent -s)"

Register the Key

  1. Open your console
  2. Navigate to your .ssh folder
  3. Enter the following command:
    ssh-add <Your key name>
    
    For example, if your key name is git_rsa, execute (note no extension)
    ssh-add git_rsa
    
  4. Enter the passphrase when prompted

Autostart Agent and Add Key on Login (Linux)

  1. Open your ~/.bashrc file.
  2. Add the following to the end of it (be sure to replace with your key name).
    # Load identities
    SSH_ENV="$HOME/.ssh/agent-environment"
    
    function start_agent {
        echo "Initialising new SSH agent..."
        /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
        echo succeeded
        chmod 600 "${SSH_ENV}"
        . "${SSH_ENV}" > /dev/null
        /usr/bin/ssh-add ~/.ssh/<your SSH key name>;
    }
    
    # Source SSH settings, if applicable
    
    if [ -f "${SSH_ENV}" ]; then
        . "${SSH_ENV}" > /dev/null
        #ps ${SSH_AGENT_PID} doesn't work under cywgin
        ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
            start_agent;
        }
    else
        start_agent;
    fi
    
    Source: https://stackoverflow.com/questions/18880024/start-ssh-agent-on-login
  3. Restart the instance

Register the Key on Your GIT Provider

Github

  1. Open the Github console
  2. Open your account settings
  3. Click SSH and GPG keys
  4. Under SSH keys click New SSH Key
  5. Enter a name
  6. In the Key field, copy and paste the contents of your .pub file found in your .ssh folder.

Gitlab

  1. Open the Gitlab console
  2. Open your account settings
  3. Click SSH Keys
  4. In the Key field, copy and paste the contents of your .pub file found in your .ssh folder.
  5. Enter a Title in the title field.
  6. You can leave the Expires At field empty.
  7. Click Add Key

AWS CodeCommit

  1. Sign-in to the AWS console with your user login (don't use the root account for this).
  2. Navigate to the IAM page.
  3. Click on Users in the left-hand menu.
  4. Click on your user entry.
  5. Select the Security Credentials tab.
  6. Under SSH Keys for AWS CodeCommit click Upload SSH Public Key.
  7. Copy and paste the contents of your .pub file found in your .ssh folder.
  8. Click the Upload SSH Public Key button.