Setup SSH keys for GitHub and GitLab on Linux
1. Open a terminal and run:
# For GitHub
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_github
# For GitLab
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_gitlab
-tspecifies the algorithm. GitHub recommends ED25519 in their documentation.-Cadds a comment (often your email) to help identify the key.-fsets the file path and name to be created.- Ensure
~/.sshexists (mkdir -p ~/.ssh && chmod 700 ~/.ssh) if this is your first key.
2. Enter a secure passphrase when prompted (recommended).
A passphrase adds an extra layer of security on top of file permissions.
3. Start or use the SSH agent
Most desktop environments start an SSH agent for your session. If ssh-add -l works, you can skip starting a new agent.
Otherwise, in the current shell:
eval "$(ssh-agent -s)"
- Starts a background SSH agent process (if one is not already available to this shell)
- Sets
SSH_AUTH_SOCKandSSH_AGENT_PIDsossh-addcan talk to the agent
4. Create and modify ~/.ssh/config:
touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/config
Ctrl+Kcuts the current lineCtrl+Xexits; pressYthen Enter to save when prompted
Add this configuration (no macOS-only options; Linux OpenSSH uses the agent via AddKeysToAgent):
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_github
AddKeysToAgent yes
IdentitiesOnly yes
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_gitlab
AddKeysToAgent yes
IdentitiesOnly yes
5. Add keys to the SSH agent:
ssh-add ~/.ssh/id_github
ssh-add ~/.ssh/id_gitlab
If your passphrase is stored by your desktop keyring (GNOME/KDE, etc.), the agent may prompt once per session.
6. Copy public keys (one at a time) to the clipboard:
Wayland (common on recent GNOME/KDE):
wl-copy < ~/.ssh/id_github.pub
wl-copy < ~/.ssh/id_gitlab.pub
X11:
xclip -selection clipboard < ~/.ssh/id_github.pub
xclip -selection clipboard < ~/.ssh/id_gitlab.pub
Install tools if needed: wl-clipboard (Wayland) or xclip/xsel (X11), e.g. sudo apt install wl-clipboard on Debian/Ubuntu.
You can always print the key and copy manually: cat ~/.ssh/id_github.pub
Then paste into GitHub or GitLab: Settings → SSH Keys.
7. Test the connection:
ssh -T git@github.com
ssh -T git@gitlab.com
You should see a success message from each host.