💾 Archived View for nox.im › snippets › manage-multiple-ssh-keys-per-host captured on 2024-05-10 at 10:54:38. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-09-28)
-=-=-=-=-=-=-
Private and corporate github accounts and repositories may be set up with different SSH keys. This post outlines briefly how I configure SSH to make the switching transparent.
Multiple different SSH key pairs may have been imported or created with
ssh-keygen -t rsa -C "email@example.org"
Create a local SSH config
touch ~/.ssh/config
Assuming we have two github accounts, a private one and a corporate one, I'd set the default to the keypair `id_rsa` here for the default `github.com` host.
Corporate repositories will use the `github.com-corpo` repository url.
Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa Host github.com-corpo HostName github.com IdentityFile ~/.ssh/id_ed25519
We'd then clone repositories using the identifier host:
git clone git@github.com-corpo:some-corporation/some-repo.git
Or update the repository config under `.git/config` then gets the remote url set as follows:
[remote "origin"] url = git@github.com-corpo:some-corporation/some-repo.git fetch = +refs/heads/*:refs/remotes/origin/*
With the above setup Go private dependencies may fail on `go mod tidy` with:
go: finding module for package github.com/corpo/repository/api go: downloading github.com/corpo/repository v0.0.0-20220309104613-82b02c89f961 github.com/corpo/my-other-repository/package imports github.com/corpo/repository/api: github.com/corpo/repository@v0.0.0-20220309104613-82b02c89f961: verifying module: github.com/corpo/repository@v0.0.0-20220309104613-82b02c89f961: reading https://sum.golang.org/lookup/github.com/corpo/repository@v0.0.0-20220309104613-82b02c89f961: 410 Gone server response: not found: github.com/corpo/repository@v0.0.0-20220309104613-82b02c89f961: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /tmp/gopath/pkg/mod/cache/vcs/f7223b0c74f927514a7dcf6437bf4284cb35c4f028686b6b2cd906e7379439d9: exit status 128: fatal: could not read Username for 'https://github.com': terminal prompts disabled
Ensure your `~/.gitconfig` has the overrides in the order of matching from more details to less, e.g.:
[url "git@github.com-corpo:corpo"] insteadOf = https://github.com/corpo [url "git@github.com:corpo"] insteadOf = https://github.com/corpo [url "git@github.com:"] insteadOf = https://github.com/
Then mark the repository path as private, here on the entire corporation:
export GOPROXY=direct export GOPRIVATE=github.com/corpo
Now `go mod tidy` should succeed.