Allison is coding...

Notes | SSH config

1. Generate SSH Keys for Multiple Accounts

Execute the key generation command for each account, specifying a unique file path to prevent overwriting. Leave the passphrase empty if automated access is preferred.

# Generate key for Account 1
ssh-keygen -t ed25519 -C "account1@email.com" -f ~/.ssh/id_ed25519_account1

# Generate key for Account 2
ssh-keygen -t ed25519 -C "account2@email.com" -f ~/.ssh/id_ed25519_account2

2. Configure Directory and File Permissions

Apply strict access permissions to the .ssh directory and the generated private keys to meet SSH security requirements.

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519_account1
chmod 600 ~/.ssh/id_ed25519_account2

3. Create and Configure the SSH Config File

Create or edit the configuration file at ~/.ssh/config to define routing rules based on unique host aliases.

touch ~/.ssh/config
chmod 600 ~/.ssh/config

Add the following configuration blocks to ~/.ssh/config:

# Profile for Account 1
Host github.com-account1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_account1
    IdentitiesOnly yes

# Profile for Account 2
Host github.com-account2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_account2
    IdentitiesOnly yes

4. Add the Public Keys to GitHub Accounts

Copy the contents of each public key (.pub) and add them to the respective GitHub settings page.

# Display public key content for Account 1 to copy
cat ~/.ssh/id_ed25519_account1.pub

# Display public key content for Account 2 to copy
cat ~/.ssh/id_ed25519_account2.pub
  1. Log into the target GitHub account.
  2. Navigate to Settings -> SSH and GPG keys.
  3. Click New SSH key.
  4. Provide a descriptive title, select Authentication Key as the Key type, paste the copied public key string into the Key field, and click Add SSH key

5. Verify the Connection

Test the authentication setup using the configured host aliases.

ssh -T git@github.com-account1
ssh -T git@github.com-account2

Successful authentication returns a message matching:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

6. Repository Level Configuration

Clone repositories or update existing remotes using the specific host alias instead of the standard github.com domain.

  • For new repositories:

      git clone git@github.com-account1:owner/repository.git
    
  • For existing repositories:

 git remote set-url origin git@github.com-account1:owner/repository.git

Configure local repository identity to ensure commit metadata matches the designated account.

git config local user.name "Account1 Name"
git config local user.email "account1@email.com"