![]() ![]() ![]() $ ssh-add ~/.ssh/id_rsa // Adds the relevant ssh key 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 Remove all of them and add the one key you are about to use. Ssh-add -l will list all the SSH keys attached to the ssh-agent. Rather we manually ensure that the ssh-agent has only the relevant key attached at the time of any Git operation. ![]() This approach doesn’t require the SSH config rules. Use id_rsa as the key for any Git URL that uses Use the id_rsa_work_user1 key for any Git URL that uses One active SSH key in the ssh-agent at a time.The above configuration asks ssh-agent to: This is relevant when you clone a repository or when you set the remote origin for a local repository Make sure you’re consistent with what hostname notation you use. You can also use “ work_user1.g ” notation as well. “ g -work_user1” is a notation used to differentiate the multiple Git accounts. “ work_user1” is the GitHub user id for the work account. Make configuration entries for the relevant GitHub accounts similar to the one below in your ~/.ssh/config file: # Personal account, - the default config $ code config // Opens the file in VS code, use any editor $ touch config // Creates the file if not exists Edit it if it exists, or else we can just create it. The SSH config file will be available at ~/.ssh/config. Here we are actually adding the SSH configuration rules for different hosts, stating which identity file to use for which domain. Using the SSH configuration file (Step 4), and having only one active SSH key in the ssh-agent at a time (Step 5). This is the crucial part, and we have two different approaches: Make the ssh-agent use the respective SSH keys for the different SSH Hosts. Ensure ssh-agent is running using the command eval "$(ssh-agent -s)".Īdd the keys to the ssh-agent like so: ssh-add ~/.ssh/id_rsa To use the keys, we have to register them with the ssh-agent on our machine. Registering the new SSH Keys with the ssh-agent Click on New SSH key, provide a suitable title, and paste the key in the box belowįor the work accounts, use the corresponding public keys ( pbcopy Select SSH and GPG keys from the menu to the left.This is to get rid of the need for typing in the username and password every time you make a Git push.Ĭopy the public key pbcopy < ~/.ssh/id_rsa.pub and then log in to your personal GitHub account: We already have the SSH public keys ready, and we will ask our GitHub accounts to trust the keys we have created. Adding the new SSH key to the corresponding GitHub account We have two different keys created: ~/.ssh/id_rsa The below code will generate the SSH keys, and saves the public key with the tag to ~/.ssh/id_rsa_work_user1.pub $ ssh-keygen -t rsa -C -f "id_rsa_work_user1" Let’s use this default key pair for our personal account.įor the work accounts, we will create different SSH keys. A private key and public key ~/.ssh/id_rsa.pub will be created at the default ssh location ~/.ssh/. When asked for the location to save the keys, accept the default location by pressing enter. If ~/.ssh/id_rsa is available, we can reuse it, or else we can first generate a key to the default ~/.ssh/id_rsa by running: ssh-keygen -t rsa Generating the SSH keysīefore 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. This endeavor is meant to help us all out. I’m sure there are many of you who have been there, done that and many more of you who are just waiting for the next time the same thing occurs (myself included!). My laziness in not documenting the process and inability to remember the steps makes me spent a decent amount of time getting the bits and pieces from all over the web and then somehow making it work. Every single time I happen to change my Mac or need to Git push with a new work account, I end up surfing for the how to’s of something I have done over half a dozen times. The need to manage multiple GitHub accounts on the same machine comes up at some point in time for most developers. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |