|
| 1 | +--- |
| 2 | +title: Working in multiple Github Orgs |
| 3 | +description: Sometimes we need to work in the NJIA GitHub org as well as another department’s org, which means managing multiple SSH keys. |
| 4 | +--- |
| 5 | + |
| 6 | +When working in more than one GitHub organization, you need to be able to use multiple SSH keys at the same time. This guide walks through the basic setup, including when you might need to use SSO instead of a standard GitHub login. |
| 7 | + |
| 8 | +## Step 1: Generate Unique SSH Keys |
| 9 | + |
| 10 | +For security and easier management, use a separate SSH key pair for each account (for example, an NJIA GitHub account vs. a DHS GitHub account, or one using NJ SSO). |
| 11 | + |
| 12 | +- Open your terminal or Git Bash. |
| 13 | +- Run the following command, replacing "you(at)example.com" with your email address and `id_rsa_department` with a descriptive filename: |
| 14 | + |
| 15 | +``` |
| 16 | +ssh-keygen -t rsa -b 4096 -C "you(at)example.com" -f ~/.ssh/id_rsa_department |
| 17 | +``` |
| 18 | + |
| 19 | +- -t rsa: Specifies the key type (RSA is common; ed25519 is also a modern). |
| 20 | +- -b 4096: Specifies the number of bits for the key (RSA only, 4096 is a strong length). |
| 21 | +- -C: Adds a comment, typically your email, to help identify the key. |
| 22 | +- -f: Specifies the filename and location. Keep all keys within the ~/.ssh directory. |
| 23 | + |
| 24 | +When prompted for a passphrase, it's recommended to add a strong one for extra security. |
| 25 | + |
| 26 | +## Step 2: Add Public Keys to Accounts |
| 27 | + |
| 28 | +Copy the public key's content using: `cat ~/.ssh/id_rsa_department.pub | pbcopy` (if you want to display it, then manually copy, remove `| pbcopy`). |
| 29 | + |
| 30 | +Paste the output into the [SSH and GPG keys](https://github.com/settings/keys) section of your account's settings page. Make sure you’re logged into the correct account or organization context. |
| 31 | + |
| 32 | +## Step 3: Configure SSH to Use Specific Keys |
| 33 | + |
| 34 | +Create or edit the `~/.ssh/config` file to tell your SSH client which private key to use for which host: `touch ~/.ssh/config` (create) `code ~/.ssh/config` (open with VSCode). |
| 35 | + |
| 36 | +Add entries for each account. The Host name is an alias you will use in your commands. |
| 37 | + |
| 38 | +``` |
| 39 | +# Special Department GitHub account |
| 40 | +Host github-department |
| 41 | + HostName github.com |
| 42 | + User git |
| 43 | + IdentityFile ~/.ssh/id_rsa_department |
| 44 | + IdentitiesOnly yes |
| 45 | + UseKeychain yes # Mac only if you're using keychain |
| 46 | +``` |
| 47 | + |
| 48 | +- HostName: The actual domain (e.g., github.com). |
| 49 | +- IdentityFile: The path to the private key file. |
| 50 | +- IdentitiesOnly yes: Ensures the SSH client only tries the specified key, preventing authentication issues. |
| 51 | + |
| 52 | +## Step 4: Add Keys to the SSH Agent |
| 53 | + |
| 54 | +- Ensure the SSH agent is running: `eval "$(ssh-agent -s)"`. |
| 55 | +- Add your private key to the agent: `ssh-add ~/.ssh/id_rsa_department` |
| 56 | +- Verify the keys currently managed by the agent with `ssh-add -l` |
| 57 | +- Optional for Macs: add to keychain: `ssh-add --apple-use-keychain ~/.ssh/id_rsa_department` |
| 58 | + |
| 59 | +## Step 5: Test and use the keys |
| 60 | + |
| 61 | +To test the configuration, reference the host alias you created: `ssh -T git@github-department` |
| 62 | + |
| 63 | +When cloning, you can use the alias in the repo URL if needed, though in many cases Git may automatically select the correct key. |
| 64 | + |
| 65 | +To clone with your alias: `git clone git@github-department:org_name/repo_name.git` |
| 66 | + |
| 67 | +## Step 6: Authorize SSO on your key (if using SSO) |
| 68 | + |
| 69 | +Return to your [SSH and GPG keys](https://github.com/settings/keys) section and [authorize the SSH key for SSO](https://docs.github.com/en/enterprise-cloud@latest/authentication/authenticating-with-single-sign-on/authorizing-an-ssh-key-for-use-with-single-sign-on). |
| 70 | + |
| 71 | +You should now be able to clone your new repo with the SSH option. |
0 commit comments