SSH Cheatsheet

Copying Keys

Use this SSH reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

ssh-copy-id (Easiest)

# Copy default key (~/.ssh/id_*.pub)
ssh-copy-id user@host

# Copy a specific key
ssh-copy-id -i ~/.ssh/mykey.pub user@host

# Non-default port
ssh-copy-id -p 2222 -i ~/.ssh/mykey.pub user@host

ssh-copy-id appends the public key to ~/.ssh/authorized_keys on the remote, creating the file and setting permissions if needed.

Manual Method (When ssh-copy-id Isn't Available)

# One-liner: pipe public key over ssh
cat ~/.ssh/id_ed25519.pub | ssh user@host 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

Or expanded:

# 1. Copy the public key content to clipboard / a variable
PUB=$(cat ~/.ssh/id_ed25519.pub)

# 2. Log in with password and append it
ssh user@host "echo '$PUB' >> ~/.ssh/authorized_keys"

Verify It Worked

# After copying, test key-based auth
ssh -o PasswordAuthentication=no user@host echo OK

If it prints OK without asking for a password, the key is installed.

Copy Keys for Multiple Hosts

for host in web1 web2 web3; do
    ssh-copy-id -i ~/.ssh/deploy.pub deploy@$host
done

Deploy Keys (Read-Only / Repo-Scoped)

Used in CI/CD to grant access to one repository only:

# Generate a dedicated key
ssh-keygen -t ed25519 -f ~/.ssh/github_deploy -N "" -C "deploy-ci"

# Add ~/.ssh/github_deploy.pub to the repo's Deploy Keys (read-only)
# Then configure .ssh/config:
Host github-deploy
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_deploy
    IdentitiesOnly yes
# Clone using the alias
git clone git@github-deploy:org/repo.git

Authorized Keys on the Remote — Quick Reference

# View what keys are authorized
cat ~/.ssh/authorized_keys

# Remove a specific key (find by comment)
grep -v "pablo@laptop" ~/.ssh/authorized_keys > /tmp/ak && mv /tmp/ak ~/.ssh/authorized_keys

# Disable a key without deleting (prepend #)
# Edit the file and comment out the unwanted line

Common Problems

SymptomCauseFix
Still prompting for passwordWrong permissionschmod 700 ~/.ssh; chmod 600 ~/.ssh/authorized_keys
Permission denied (publickey)Key not in authorized_keysRe-run ssh-copy-id
Key offered but rejectedServer has AuthorizedKeysFile overriddenCheck /etc/ssh/sshd_config for AuthorizedKeysFile
ssh-copy-id not foundmacOS / some distrosUse the manual one-liner above
Key copied but agent has different keyIdentitiesOnly noAdd IdentitiesOnly yes + correct IdentityFile to config