How to add your SSH key to a server or GitHub

Topic: Accounts access

Summary

Install your existing SSH public key on a remote server (via ssh-copy-id or authorized_keys) or in GitHub so you can authenticate without a password. Includes verification and common pitfalls: permissions, wrong key, and ssh-agent.

Intent: How-to

Quick answer

  • Add the public key to the server with ssh-copy-id or by appending to ~/.ssh/authorized_keys; for GitHub use Settings → SSH and GPG keys.
  • Confirm the key is used with ssh -v or ssh -T [email protected] and that login works without a password.
  • If login fails, check key permissions (600/700), that the correct key is offered, and that the agent has the key loaded.

Prerequisites

Steps

  1. Choose the public key to add

    Identify the key pair you created (e.g. ~/.ssh/id_ed25519.pub) and display its contents with cat.

  2. Add the key to a remote server (ssh-copy-id or manual)

    Use ssh-copy-id for one-step install, or append the public key line to ~/.ssh/authorized_keys on the server with correct permissions.

  3. Add the key to GitHub

    In GitHub go to Settings → SSH and GPG keys, paste the public key, and save with a clear label.

  4. Verify server login

    Run ssh user@hostname (or ssh -i path/to/key) and confirm you are not prompted for a password.

  5. Verify GitHub SSH

    Run ssh -T [email protected] and confirm the success message; fix agent or key path if the wrong key is used.

Summary

You will install an existing SSH public key on a remote Linux/macOS server or in GitHub so that you can log in or push/pull without typing a password. This guide assumes you already have a key pair (e.g. from the create and secure SSH key guide). It covers both server and GitHub, verification, and common failures (permissions, wrong key, agent).

Prerequisites

  • An SSH key pair already created (private key e.g. ~/.ssh/id_ed25519, public ~/.ssh/id_ed25519.pub).
  • For a server: SSH access to the server (password or another key) at least once so you can add the new key.
  • For GitHub: A GitHub account and the public key contents ready to paste.
  • Terminal on Linux or macOS (Windows: use OpenSSH in PowerShell or WSL; same commands apply).

Steps

Step 1: Choose the public key to add

Use the key you created earlier. List and display it:

ls -la ~/.ssh/*.pub
cat ~/.ssh/id_ed25519.pub

Copy the full line (starts with ssh-ed25519 or ssh-rsa, ends with your comment). You will paste this into the server’s authorized_keys or into GitHub. Do not share the private key (id_ed25519 without .pub).

Step 2: Add the key to a remote server (ssh-copy-id or manual)

Option A — ssh-copy-id (recommended):

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostname

Replace user and hostname with your server user and host (or IP). Log in with your password when prompted. The command appends your public key to ~/.ssh/authorized_keys on the server and sets safe permissions.

Option B — Manual: If ssh-copy-id is not available, log in with password and run on the server:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "PASTE_THE_FULL_PUBLIC_KEY_LINE" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

Use a single line per key; no line breaks inside the key.

Step 3: Add the key to GitHub

  1. Open GitHub → Settings → SSH and GPG keys.
  2. Click New SSH key. Enter a title (e.g. “MacBook work”) and paste the full public key line into the Key field.
  3. Click Add SSH key. Confirm with your password or 2FA if asked.

You can add multiple keys (e.g. one per machine). GitHub will accept the first key that matches.

Step 4: Verify server login

From your machine:

ssh user@hostname

You should not be prompted for a password (only for the key passphrase if you set one). Confirm you are on the server:

whoami
exit

If you use a non-default key path:

ssh -i ~/.ssh/id_ed25519 user@hostname

Step 5: Verify GitHub SSH

Test that GitHub accepts your key:

ssh -T [email protected]

Expected: Hi username! You've successfully authenticated... (and the connection closes). If you see “Permission denied (publickey)”, GitHub is not receiving a valid key — see Troubleshooting.

Verification

  • Server: ssh user@hostname logs in without a password prompt; whoami on the server shows the correct user.
  • GitHub: ssh -T [email protected] prints the success message with your username.
  • Key in use: Run ssh -v user@hostname or ssh -vT [email protected] and look for “Offering public key” and “Authentication succeeded” to confirm which key was used.

Troubleshooting

“Permission denied (publickey)” on server
The server is not accepting your key. Check: (1) The public key was appended as one full line to ~/.ssh/authorized_keys on the server. (2) Server permissions: ~/.ssh must be 700, authorized_keys 600. (3) You are connecting as the user that owns that authorized_keys file. Use ssh -v user@hostname to see which key is offered and any error from the server.

“Permission denied (publickey)” on GitHub
GitHub did not receive a valid key. (1) Confirm the key was added in Settings → SSH and GPG keys and matches the key you use locally. (2) If you have multiple keys, ensure the right one is used: start the agent with eval "$(ssh-agent -s)", then ssh-add ~/.ssh/id_ed25519. (3) Test with ssh -vT [email protected] and check “Offering public key” and any rejection reason.

“WARNING: UNPROTECTED PRIVATE KEY FILE!”
Your private key has overly open permissions. Run chmod 600 ~/.ssh/id_ed25519 and chmod 700 ~/.ssh. SSH will refuse to use the key until permissions are fixed.

Wrong key is used (e.g. wrong GitHub account)
SSH tries keys in an order that may not match your intent. Use a specific key with ssh -i ~/.ssh/id_ed25519. For Git, configure the host in ~/.ssh/config with IdentityFile ~/.ssh/id_ed25519 so that host always uses that key.

Next steps

Continue to