When using GitHub, GitLab, or remote servers, SSH is commonly used for authentication instead of passwords. Over time, you may run into multiple concepts: id_rsa, ~/.ssh/config, and ssh-agent. This is a short guide to help you remember how they all fit together.
๐ 1. SSH Key (id_rsa and id_rsa.pub)
An SSH key comes in a pair:
Private key:
id_rsa(keep secret, never share)Public key:
id_rsa.pub(safe to upload)
The public key is added to services like GitHub. When you connect, your computer proves it owns the matching private key.
๐ Think of it like:
Public key = lock you place on a door
Private key = the key that opens it
๐ 2. Can one key be used for multiple sites?
Yes. You can use the same id_rsa.pub on:
GitHub
GitLab
Servers
However:
โ Pros
Simple setup
One key to manage
โ Cons
If the private key is compromised, all accounts are affected
Harder to separate work/personal access
๐ Best practice: use one key per service or identity
โ๏ธ 3. ~/.ssh/config (SSH control center)
This file tells SSH how to behave for different hosts.
Instead of typing:
ssh -i ~/.ssh/id_rsa_github git@github.comYou can just do:
ssh github.comExample config:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_gitlabWhat it does:
Chooses the correct SSH key automatically
Sets usernames and host settings
Lets you create shortcuts for servers
๐ Think of it as a routing table for SSH connections
๐ 4. SSH Agent (ssh-agent)
SSH keys can be protected with a passphrase. Thatโs secure, but typing it every time is inconvenient.
The SSH agent solves this.
How it works:
You add your key once:
ssh-add ~/.ssh/id_rsaThe agent stores it temporarily in memory
Future SSH/Git commands reuse it automatically
Key idea:
It does NOT store your file
It keeps the unlocked key in RAM only
๐ Think of it as a temporary unlocked keyring
๐ 5. How everything works together
When you run:
git pushHereโs what happens:
SSH checks
~/.ssh/config
โ decides which key to use
2. SSH asks ssh-agent
โ โDo you already have this key unlocked?โ
3. Agent provides the key (if loaded)
4. Connection succeeds without asking for password again
๐ง Simple mental model
SSH keys โ prove your identity
~/.ssh/configโ decides which key to use whereSSH agent โ remembers unlocked keys so you donโt retype passphrases
๐ Recommended setup
For most developers:
Use separate SSH keys per account/service
Use
~/.ssh/configto manage them cleanlyUse
ssh-agentso you donโt keep entering passphrases
๐ Final takeaway
SSH feels complex at first because it has multiple layers, but itโs actually just:
Identity (keys) + Rules (config) + Convenience (agent)
Once set up, it becomes invisible and โjust worksโ in the background.