How to setup GIT with SSH Authenication

1. Generate Key-Pair

Windows / Linux / Mac is using same commands.

  • generate Key-Pair
ssh-keygen -t <<Algorithnm>>-C "<<Your_Email>>"

ssh-keygen -t ed25519 -C "your_email@github.com"
  • Enter file in which to save the key. if you want to use default filename + location press enter, otherwise provide full path + filename
Enter file in which to save the key (C:\Users\Chatr/.ssh/id_ed25519): 

#If you want to change path. Please provide full path
Enter file in which to save the key (C:\Users\Chatr/.ssh/id_ed25519): C:\Users\Chatr/.ssh/id_ed25519_ping_github
  • Enter passphrase (empty for no passphrase)
    Note passphrase = password of ssh key pair

Add your key to the SSH agent

Windows

  • Start Service OpenSSH First (Required Run Terminal / PowerShell Console as Administrator, Check the current status of ssh-agent
Get-Service | ?{$_.Name -like '*ssh-agent*'} | select -Property Name, StartType, Status
  • Enable the Service if it is disabled
Set-Service -Name ssh-agent -StartupType Manual
  • Start the Service
Start-Service ssh-agent
  • Add your key, if your key has a passphrase, it will be prompt password.
#PATTERN
ssh-add <<path to your private key >>

#SAMPLE
ssh-add id_ed25519_ping_github

PS C:\Users\Chatr\.ssh> ssh-add id_ed25519_ping_github
Enter passphrase for id_ed25519_ping_github:
Identity added: id_ed25519_ping_github (pingkunga@github.com)

Linux / Mac

  • Start Service OpenSSH
eval "$(ssh-agent -s)"
  • Add your key, if your key has a passphrase, it will be prompt password.
# Linux
ssh-add ~/.ssh/<<YOUR_KEY_NAME>>

# Mac
ssh-add -K ~/.ssh/<<YOUR_KEY_NAME>>

Provide GitHub with your public key

  • GitHub Profile > SSH and GPG Keys > New SSH Key
  • Read public key file Windows by using Get-Content / Linux, mac using cat
#Windows (at .ssh folder)
C:\Users\Chatr\.ssh> Get-Content .\id_ed25519_ping_github.pub

#Linux Mac (at .ssh folder)
cat .\id_ed25519_ping_github.pub
  • Paste content in public key
  • GitHub Account with 2FA. Enter number from Authenticator App such as Google Authenticator / Microsoft Authenticator
  • SSH Key show in SSH and GPG Keys section

Support with multiple user / host via SSH Config

  • SSH Config Path
Host <<Unique Name>>
  Hostname <<url>>
  User <<username>>
  IdentityFile <<SSH Private Key Path>>

if you have multiple user. you can spread by customzing host in SSH Config, In example. I have 3 git accounts 2 GitHub and 1 in GitLab. I have modified SSH Config like this.

Host github.com 
  Hostname github.com
  User pingkunga 
  IdentityFile ~/.ssh/id_ed25519_ping_github 

Host github.com-debuggingsoft
  Hostname github.com
  User debuggingsoft
  IdentityFile ~/.ssh/id_ed25519_ds_github 

Host gitlab.local
  Hostname dev.ds.local
  User chatri.ng
  IdentityFile ~/.ssh/id_ed25519_cn_gitlab 

Reference


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.