Skip to main content

Dev tools and workflow · Intermediate · 18 min

Manage several SSH keys and hosts with ~/.ssh/config

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · OpenSSH 10.2p1 · Git 2.53

You will have one SSH key per purpose, a tidy ~/.ssh/config.d folder that sends the right key to each GitHub account and server, a private server reached through a bastion, shared connections, and signed Git commits.

On this page

One SSH key and one Host line go a long way. Then a second GitHub account arrives with a new job, a client gives you a server you can only reach through a bastion, and SSH starts offering the wrong key, logging you in as the wrong GitHub user, or failing with Too many authentication failures.

The fix is a config that says exactly which key goes where. In this tutorial you make one key for each purpose (work GitHub, personal GitHub, servers), split ~/.ssh/config into small files in ~/.ssh/config.d, give each GitHub account its own alias, reach a private server through a bastion with ProxyJump, add shared defaults with Match, reuse connections with ControlMaster, and sign Git commits with one of the keys. At each stage ssh -G shows you the settings SSH will really use, without connecting.

The commands use OpenSSH 10.2, which Ubuntu 26.04 ships, and Git 2.53. macOS ships its own OpenSSH and Git, and every command here works there too. The upstream OpenSSH project has released newer versions (10.6 at the time of writing); nothing here needs them.

Every command runs on your own computer unless a step says otherwise.

Step 1 · Make one key for each purpose

A key that does everything is a key you cannot revoke without breaking everything. GitHub also refuses to attach one key to two accounts (it answers Key is already in use), so two GitHub accounts need two keys anyway. Make three: one per GitHub account and one for your servers.

On Ubuntu, install the SSH client and Git if they are missing. macOS has both already:

Bash
sudo apt install -y openssh-client git

Create the keys. -f gives each its own file name, so nothing overwrites your existing ~/.ssh/id_ed25519, and -C puts a label in the public key that tells you later what it is for. Replace my-laptop with a name for this computer:

Bash
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_github_work -C "github-work@my-laptop"
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_github_personal -C "github-personal@my-laptop"
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_servers -C "servers@my-laptop"

Set a passphrase for each one when asked. If ssh-keygen asks Overwrite (y/n)?, a key with that name already exists: answer n and pick another name.

Check it worked

Bash
ls -l ~/.ssh/id_ed25519_*
for key in ~/.ssh/id_ed25519_github_work.pub ~/.ssh/id_ed25519_github_personal.pub ~/.ssh/id_ed25519_servers.pub; do ssh-keygen -lf "$key"; done

You see six files: three private keys with permissions -rw------- and three .pub files. The loop prints three different fingerprints, each starting 256 SHA256: and ending with its label and (ED25519).

Step 2 · Split the config into a config.d folder

SSH reads its config from top to bottom and keeps the first value it finds for each setting. That one rule explains most surprises: a broad Host * block near the top beats the specific block below it. A folder of small, numbered files makes the order visible: specific hosts in low numbers, defaults in a high number.

Include pulls the files in. It must be the first line of ~/.ssh/config: a line placed under a Host block belongs to that block. The commands below make the folder, keep a backup of your current config if you have one, and write a new config with the Include line on top and your old contents underneath. Run them once; a second run adds a second Include line.

Bash
mkdir -p -m 700 ~/.ssh/config.d
if [ -f ~/.ssh/config ]; then cp -p ~/.ssh/config ~/.ssh/config.bak; fi
(umask 077; { printf 'Include config.d/*.conf\n\n'; cat ~/.ssh/config 2>/dev/null || true; } > ~/.ssh/config.new)
mv ~/.ssh/config.new ~/.ssh/config

A relative path in Include is read from ~/.ssh, and the matching files are read in name order, which is why the file names below start with numbers.

Check it worked

Bash
head -n 1 ~/.ssh/config
ls -ld ~/.ssh/config.d
ssh -G example.org > /dev/null && echo "config parses"

The first line is Include config.d/*.conf, the folder is drwx------, and the last command prints config parses. ssh -G reads the whole config and prints the result without connecting, so a typo shows up here as an error naming the file and line. Your old entries are still in ~/.ssh/config below the new line.

Step 3 · Give each GitHub account its own alias

Both accounts live at the same address, git@github.com, so SSH cannot tell them apart by host name. Give each one an alias. The alias is what you type, HostName is where SSH really connects, and IdentitiesOnly yes stops SSH from offering every key in your agent before the one you named. Without it, a server that allows six attempts (MaxAuthTries defaults to 6) can hang up before the right key is tried, and GitHub accepts the first key it knows, which may belong to the other account.

Bash
(umask 077; cat > ~/.ssh/config.d/10-github.conf <<'CONF'
# Work GitHub account
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github_work
    IdentitiesOnly yes

# Personal GitHub account
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github_personal
    IdentitiesOnly yes
CONF
)

Add each public key to its account: print it with cat ~/.ssh/id_ed25519_github_work.pub, then on GitHub, signed in as that account, open Settings, SSH and GPG keys, New SSH key, and paste it as an Authentication Key. Do the same for the personal key while signed in to the personal account.

Then use the alias in place of github.com in repository addresses. Replace the organisation, user and repository names with your own:

Bash
ssh -T git@github-work
ssh -T git@github-personal
git clone git@github-work:your-company/api.git ~/work-api
cd ~/your-existing-personal-repo
git remote set-url origin git@github-personal:YOUR-USERNAME/notes-demo.git

ssh -T answers Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. and exits with status 1, which is normal. Each alias should greet a different username. Plain github.com addresses keep working with whatever your config said before, so existing clones are not affected until you change their remote.

The alias picks the key; it does not pick your name and email on commits. Set those per folder with Git's includeIf, as shown in install and configure Git.

Check it worked

Bash
ssh -G github-work | grep -E '^(hostname|user|identityfile|identitiesonly) '
ssh -G github-personal | grep -E '^identityfile '

For github-work you see hostname github.com, user git, identitiesonly yes and exactly one identityfile line, ending in id_ed25519_github_work. For github-personal the only key is the personal one.

Step 4 · Reach a private server through a bastion

Many teams keep servers on a private network and allow SSH in through one hardened machine, the bastion. ProxyJump makes the hop for you: SSH logs in to the bastion, opens a tunnel from there to the private server, and runs the real session through it. The private key never leaves your computer, and the bastion needs no copy of it.

The example uses a bastion at 203.0.113.5 with the user jump, and a private server app1 at 10.0.0.20 with the user deploy. Replace the addresses and users with yours:

Bash
(umask 077; cat > ~/.ssh/config.d/20-servers.conf <<'CONF'
Host bastion
    HostName 203.0.113.5
    User jump

Host app1
    HostName 10.0.0.20
    User deploy
    ProxyJump bastion

# Settings shared by both servers
Host bastion app1
    IdentityFile ~/.ssh/id_ed25519_servers
    IdentitiesOnly yes
CONF
)

A Host line can list several names, so the last block gives both servers the same key without repeating it. Because SSH keeps the first value it finds, put any per-server override (a different port, say) in that server's own block above.

Both machines need the servers public key in ~/.ssh/authorized_keys for their user. ssh-copy-id reads your config, so it goes through the bastion for app1 as well:

Bash
ssh-copy-id -i ~/.ssh/id_ed25519_servers.pub bastion
ssh-copy-id -i ~/.ssh/id_ed25519_servers.pub app1

If password login is already off on those servers, add the key by hand as in log in to a Linux server with SSH keys.

Bash
ssh -G app1 | grep -E '^(hostname|user|proxyjump|identityfile) '

Check it worked

Bash
ssh bastion whoami
ssh app1 whoami

The first time, SSH asks you to confirm each server's fingerprint; compare it with the one your provider or administrator gives you before typing yes. Then the commands print jump and deploy. The ssh -G line before them shows proxyjump bastion for app1. For a chain of bastions, list them in order: ProxyJump bastion1,bastion2.

If app1 fails but bastion works, run ssh -v app1 true and look for a line about ProxyJump near the top, then for Permission denied: that tells you whether the hop or the final login failed.

Step 5 · Add shared defaults last, with Match

Settings for every host belong in the highest-numbered file, so they only fill gaps the specific files left. Match goes further than Host: Match host checks the host name SSH will connect to after HostName has been applied, so it can tell that github-work and github-personal are both github.com.

Bash
(umask 077; cat > ~/.ssh/config.d/90-defaults.conf <<'CONF'
# Read last: these only fill settings the files above left empty.
Host *
    ServerAliveInterval 30
    ServerAliveCountMax 4
    AddKeysToAgent yes
    IgnoreUnknown UseKeychain
    UseKeychain yes

# Share connections everywhere except GitHub (see the next step).
Match host *,!github.com
    ControlMaster auto
    ControlPath ~/.ssh/cm-%C
    ControlPersist 10m
CONF
)

What each line does:

  • ServerAliveInterval 30 with ServerAliveCountMax 4 sends a small keep-alive every 30 seconds and gives up after four missed replies. Mobile data and home routers often drop idle connections silently; this keeps the session alive, or ends a dead one after about two minutes instead of leaving a frozen terminal. It helps on prepaid mobile data and shared Wi-Fi, common for developers in Lagos, Nairobi and many other places.
  • AddKeysToAgent yes hands a key to ssh-agent the first time you unlock it, so you type each passphrase once per session.
  • UseKeychain yes is a macOS-only option that stores passphrases in the Keychain. The IgnoreUnknown line before it lets the same file work on Linux, where the option does not exist.
  • The Match block turns on connection sharing for every host whose real name is not github.com.

Check it worked

Bash
ssh -G app1 | grep -E '^(controlmaster|controlpersist|serveraliveinterval) '
ssh -G github-work | grep -E '^controlmaster '

app1 shows controlmaster auto, controlpersist 600 (ten minutes, in seconds) and serveraliveinterval 30. github-work shows controlmaster false: the Match saw through the alias.

Step 6 · Reuse one connection with ControlMaster

Every new SSH connection repeats the network round trips, the key exchange and the login. Through a bastion that happens twice. With ControlMaster auto, the first connection to a host stays open in the background as a master, listening on the socket named by ControlPath, and later connections to that host ride on it. ControlPersist 10m keeps the master alive for ten minutes after the last session closes. On a slow or high-latency link the second command returns almost at once.

Bash
time ssh app1 true
time ssh app1 true
ssh -O check app1

%C in ControlPath is a short hash of your local host name, the remote host, port, user and any ProxyJump host. It keeps the socket path under the length limit for socket files, which long host names can otherwise exceed on macOS.

The same hash is why GitHub is excluded. Both aliases connect as git to github.com on port 22, so they would get the same %C and the same socket. A push through github-personal would then reuse a master that logged in with the work key, and land in the wrong account.

Check it worked

The second time shows a clearly shorter real figure than the first. ssh -O check app1 prints Master running (pid=...), and ls ~/.ssh/cm-* lists the socket files. To close a master early, for example after changing the server's config, run ssh -O exit app1. A master keeps its original settings, so close it after editing that host's block.

Step 7 · Sign Git commits with an SSH key

Git can sign commits with an SSH key instead of a GPG key, and GitHub shows signed commits as Verified. Here you sign with the work key in a practice repository. Replace the name and email with the ones on your work GitHub account; GitHub only marks a commit as Verified when its email is a verified address on the account that holds the signing key.

Bash
mkdir -p ~/ssh-config-demo/signed
cd ~/ssh-config-demo/signed
git init -q
git config user.name "Your Name"
git config user.email "you@work.example"
git config gpg.format ssh
git config user.signingkey "$HOME/.ssh/id_ed25519_github_work.pub"
git config commit.gpgsign true

These settings apply to this repository only. To sign everywhere with one key, run the same git config lines with --global, or put them in the per-folder file from includeIf so work and personal repositories sign with their own keys.

To check signatures on your own computer, Git needs a list of keys you trust, called an allowed signers file. Each line holds an email, the namespace git, and a public key:

Bash
echo "you@work.example namespaces=\"git\" $(cut -d' ' -f1,2 ~/.ssh/id_ed25519_github_work.pub)" >> ~/.ssh/allowed_signers
git config gpg.ssh.allowedSignersFile "$HOME/.ssh/allowed_signers"
echo "Signed with an SSH key" > README.md
git add README.md
git commit -q -m "Add a signed README"

If the key has a passphrase and is not in your agent, Git asks for it when you commit.

On GitHub, add the same public key a second time under Settings, SSH and GPG keys, New SSH key, this time choosing Signing Key as the type. A key used for both logging in and signing appears twice on that page.

Check it worked

Bash
git log --show-signature -1

Above the commit you see a line like Good "git" signature for you@work.example with ED25519 key SHA256:..., and the fingerprint matches the one ssh-keygen -lf ~/.ssh/id_ed25519_github_work.pub printed in the first step. No principal matched means the email in the commit and in allowed_signers differ.

Step 8 · Clean up the practice pieces

Your keys, ~/.ssh/config and the files in ~/.ssh/config.d are the result of this tutorial, so keep them. Close any open shared connections and delete the practice repository. Warning: the last line deletes ~/ssh-config-demo.

Bash
cd ~
for host in app1 bastion; do
  if ssh -O check "$host" 2>/dev/null; then ssh -O exit "$host"; fi
done
rm -rf ~/ssh-config-demo

If you followed along only to practise, also delete ~/.ssh/config.d/10-github.conf and ~/.ssh/config.d/20-servers.conf, remove the you@work.example line from ~/.ssh/allowed_signers, delete any keys you will not use (rm ~/.ssh/id_ed25519_servers ~/.ssh/id_ed25519_servers.pub, for example) and remove their public keys from GitHub and from each server's authorized_keys. ~/.ssh/config.bak holds your config as it was before the second step.

Check it worked

Bash
ls ~/ssh-config-demo 2>/dev/null || echo "practice repository removed"
ssh -G app1 | grep -E '^proxyjump '
ssh -O check app1 || true

The first line prints practice repository removed and the second prints proxyjump bastion. ssh -O check app1 now fails with a Control socket connect(...): No such file or directory message, because the master has closed; || true stops that expected failure from ending the script. Your config still resolves app1 through bastion.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.