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.