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
            

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)"
            

4. Create and modify ~/.ssh/config:


                touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/config
            

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
            

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.