Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

set muiltiple ssh keys for different github accounts on one computer

# navigating to the ssh directory, run the following command.
cd .ssh/

# Generate SSH key for each GitHub account
ssh-keygen -t rsa -C "your_name@email.com"
id_rsa_personal
ssh-keygen -t rsa -C "your_name@organization_email.com"
id_rsa_work
ls ~/.ssh
cat id_rsa_personal.pub
Copy the SSH key and then sign in to your GitHub account.

Follow the steps below to add an SSH key to your GitHub account:

On your GitHub, navigate to Settings.
Choose SSH and GPG keys - Gnu Privacy Guard (GPG) is an encryption technique that allows secure information sharing among parties.
Hit on the New SSH Key button, give it a significant Title and paste the Key.
Finally, click the Add SSH key button.
open your git config fle if it exists
~/.ssh/config
If it exists, you can edit it, or else it can be created using this command:

touch config
nano config
in nano config
# Personal account, - the default config
Host github.com-"type your personal github username"
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal
   
# Work account
Host github.com-"type your organization github username"  
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work
   
  hit Ctrl X to save and close nano
  Done!!
Comment

How to manage multiple GitHub accounts on a single machine with SSH keys

#1. Generating the SSH keys
# Before generating an SSH key, we can check to see if we have any existing SSH keys: ls -al ~/.ssh This will list out all existing public and private key pairs, if any.

$ ssh-keygen -t rsa
$ ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"
# We have two different keys created:

$ ~/.ssh/id_rsa
$ ~/.ssh/id_rsa_work_user1
# 2. Adding the new SSH key to the corresponding GitHub account
#  Copy the public key pbcopy < ~/.ssh/id_rsa.pub and then log in to your personal GitHub account:
#3 . Registering the new SSH Keys with the ssh-agent
#To use the keys, we have to register them with the ssh-agent on our machine. Ensure ssh-agent is running using the command eval "$(ssh-agent -s)".
# Add the keys to the ssh-agent like so:

$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa_work_user1

# 4. Creating the SSH config File

$ cd ~/.ssh/
$ touch config           # Creates the file if not exists
$ code config            # Opens the file in VS code, use any editor
# copy and paste content change it to your info
# Personal account, - the default config
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa
   
# Work account-1
Host github.com-work_user1    
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_work_user1

#5. One active SSH key in the ssh-agent at a time
#This approach doesn’t require the SSH config rules. Rather we manually ensure that the ssh-agent has only the relevant key attached at the time of any Git operation.
#ssh-add -l will list all the SSH keys attached to the ssh-agent. Remove all of them and add the one key you are about to use.

#If it’s to a personal Git account that you are about to push:

$ ssh-add -D            #removes all ssh entries from the ssh-agent
$ ssh-add ~/.ssh/id_rsa  # Adds the relevant ssh key
 # or 
$ ssh-add -D
$ ssh-add ~/.ssh/id_rsa_work_user1

#To list the config name and email in the local Git directory, do git config user.name and git config user.email. If it’s not found, update accordingly.

git config user.name "User 1"   // Updates git config user name
git config user.email "user1@workMail.com"
# 6. While Cloning Repositories
# Repositories can be cloned using the clone command Git provides:

$ git clone git@github.com:personal_account_name/repo_name.git
$ git clone git@github.com-work_user1:work_user1/repo_name.git
#7. For Locally Existing Repositories
# If we have the repository already cloned:

$ git remote set-url origin git@github.com-worker_user1:worker_user1/repo_name.git
$ git remote add origin git@github.com-work_user1:work_user1/repo_name.git 
# the initial commit to the GitHub repository:

git add .
git commit -m "Initial commit"
git push -u origin master
# We are done!
Comment

PREVIOUS NEXT
Code Example
Shell :: delete tag beautifulsoup 
Shell :: install tomcat on mac brew 
Shell :: pip upgrade package 
Shell :: uninstall your phone app windows 10 
Shell :: linux how to move file to another directory 
Shell :: npm uninstall package 
Shell :: apk add build-essential 
Shell :: to clear npx cache 
Shell :: powershell suppress error 
Shell :: convertir a sudoers user centos 
Shell :: linux find installation location 
Shell :: wsl ubuntu git status and the file are not staged 
Shell :: conda install flake8 
Shell :: bash how to run remote command 
Shell :: remove docker machine 
Shell :: find change permissions to subdirectories 
Shell :: ssh transfer file 
Shell :: git grep in commits 
Shell :: install aws cli v2 mac 
Shell :: bash merge pdf 
Shell :: install torch 1.7.1 
Shell :: kill running port in ubuntu using procees id 
Shell :: git get current branch 
Shell :: git check if stash exists 
Shell :: how to unzip using tar 
Shell :: delete auto purge 
Shell :: gcloud shell ssh 
Shell :: ubuntu wsl go to desktop 
Shell :: export variable bash 
Shell :: wordpress mit ssh installieren 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =