Utilizing GitHub accounts on Ubuntu with multiple SSH keys
Tags: github, ubuntu, ssh
Published: 2024-02-21 14:05:15

In the dynamic realm of software development, it's not uncommon for developers to find themselves juggling multiple GitHub accounts—one for work and another for personal projects. Navigating this dual identity on Ubuntu/Mac requires a thoughtful approach, particularly when it comes to managing SSH keys. This article serves as a practical guide to seamlessly use two or more GitHub accounts on Ubuntu/Mac by employing multiple SSH keys.

## The Dilemma
Managing work and personal GitHub accounts simultaneously poses a unique challenge. A key aspect of addressing this challenge is the need for separate SSH keys, ensuring secure and efficient authentication for each account.

## Understanding SSH Keys
SSH keys play a pivotal role in secure authentication. By utilizing distinct keys for different GitHub accounts, developers can maintain a clear separation between their professional and personal coding endeavours.


## Generating SSH Keys for Each GitHub Account

### Work Account(example):
Open the terminal and generate an SSH key using: 

`ssh-keygen -t rsa -b 4096 -C "your-email@work.com"`

Save the key to an appropriate directory. By default it is in the “~/.ssh” directory you can keep it that way.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvnmqqlt320ux055dq7e.png)
If you want to give a specific name to your key, it can be changed by renaming the file in which you want the key to be saved in. Renaming of keys can be used to properly identify different keys you saved.


### Personal Account:
Repeat the process with a different email for your personal GitHub account.



## Configuring SSH Config File
To manage multiple SSH keys effectively, edit your SSH config file. Add the following lines:
Once the Keys properly generated, they should be found in the “~/.ssh” directory. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwejp2b8ldbz7l16o6xz.jpg)

If all the key generated are available, then head to your different GitHub accounts on [GitHub.com](https://github.com)

Head  to the your profile settings and select “SSH and GPG keys” and click “New SSH key”

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6errbe7rvi5gxehzzea.jpg)

You need to add you generated public key that can be obtained from reading the public keys generated. With the command 

```
cat ~/.ssh/<your_key_name>.pub
```
 Copy and paste the result from the public key in the Key text area provided on your GitHub and click “Add SSH key”.
Repeat the step for both GitHub accounts and any accounts you wish to add this way. 



## Bash Code

To write the bash code go to the “.shh” folder and create a file to create a “config” text file using vim to properly separate the different public keys. 

1. Open your terminal
2. Go to the SSH folder using `cd ~/.ssh`.
3. Create a config file using the command “vim config”.
4. Insert the correct text below in the config file by replacing the data with your private data.
5. Check that the content of the config file is properly inserted using `cat ~/.ssh/config`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w8rslficm7kkoxooi6oy.png)

### Personal account
```
Host github.com-personal
            HostName github.com
            IdentityFile ~/.ssh/<Your private key>
```

### Work account
```
Host github.com-work
            HostName github.com
  	    IdentityFile ~/.ssh/<Your work key>
```

## Bash Testing
Testing the Configuration
### Work account

```
ssh -T git@github.com-work
```
### Personal account

```
ssh -T git@github.com-personal
```
While testing you might be prompted to enter a password to unlock the private key. Enter the password given to the key that you generate in the steps above to continue with the operation.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwl9siv1gw1s23s38tsb.png)
Here is what should be seen when the keys are properly distributed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ruvl10wxbb0n5129fsuf.png)




## Adding SSH Keys to the Authentication Agent

After configuring multiple SSH keys and testing the setup, you can enhance the workflow by using the `ssh-add` command. This command adds private keys to the SSH authentication agent, eliminating the need to enter the passphrase every time you use the keys.

Open a terminal and run the following commands for each key:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqnc9ujkfkrzot0fa70p.png)

Note: You can then list all the ssh-keys information to verify that your keys are well implemented. By doing the command

```
ssh-add -l
```


## Organizing Workspaces
Consider organizing your workspaces effectively, keeping work and personal projects in separate directories. This helps maintain a clear distinction between the two.

## Updating Git Configurations
Update your Git configurations to align with the new SSH key setup. Ensure consistency across all relevant settings for a smooth experience.







## Conclusion: A Seamless Workflow
By following these steps, you've successfully configured multiple SSH keys for your dual GitHub accounts on Ubuntu/Mac. This setup ensures a harmonious balance between your professional and personal coding pursuits, enhancing both security and workflow.