SkillPilotAI

ssh-agent, ssh-add, ssh-copy-id

ssh-agent, ssh-add and ssh-copy-id put the keys made by ssh-keygen to daily use: you type the passphrase once per session, and the server needs no password after the public key is installed.

142.1 What they do and where they run

  • ssh-agent: background process that keeps unlocked private keys in memory; ssh finds it through SSH_AUTH_SOCK.
  • ssh-add: loads keys into the agent, lists, tests, removes and locks them.
  • ssh-copy-id: appends your public key to the server's ~/.ssh/authorized_keys.
  • Linux/macOS have all three; on Windows the agent is a service and there is no ssh-copy-id.

142.2 How it works

ssh-copy-id, then agent login
  1. 1.ssh-copy-id logs in once with the password and appends id_ed25519.pub to authorized_keys

  2. 2.ssh-add asks the passphrase once and loads the unlocked key into the agent

  3. 3.ssh sends a login request to bastion (TCP 22, publickey auth)

  4. 4.Server replies with a random challenge

  5. 5.ssh hands the challenge to the agent; the agent returns only a signature

  6. 6.Server verifies the signature with the public key: login, no password

The private key never leaves the laptop

142.3 ssh-agent: start and stop

  • ssh-agent -s prints Bourne-shell commands that set SSH_AUTH_SOCK and SSH_AGENT_PID, so run it inside eval.
  • -k kills the agent (eval "$(ssh-agent -s -k)"); -c prints C-shell style commands.
  • ssh-agent <command> runs the command as the agent's child; the agent exits with it.
  • Lab: Docker containers laptop and bastion on 192.168.162.0/24.
Terminal
ssh-agent -s > /tmp/s.out; cat /tmp/s.outeval "$(cat /tmp/s.out)" >/dev/nullecho SOCK=$SSH_AUTH_SOCK PID=$SSH_AGENT_PIDeval "$(ssh-agent -s -k)"ssh-agent -c > /tmp/c.out; cat /tmp/c.outssh-agent sh -c \  'echo child sees $SSH_AUTH_SOCK; ssh-add -l'sleep 11; pgrep -x ssh-agent ||  echo 'no ssh-agent left'
Output
$ ssh-agent sh -c 'echo child sees $SSH_AUTH_SOCK; ssh-add -l'child sees /tmp/ssh-XXXXXXbHgPdm/agent.56The agent has no identities.$ sleep 11; pgrep -x ssh-agent || echo 'no ssh-agent left'no ssh-agent left

142.4 ssh-agent: lifetime, socket and debug

  • -t 3 keeps every key for only 3 seconds; after 4 seconds ssh-add -l is empty.
  • -D runs in the foreground (for systemd and other service managers); -d also prints debug messages.
  • -a /path starts the agent on a fixed socket (srw------- = owner only).
  • Other options: -E md5|sha256 fingerprint hash, -P allowed PKCS#11 provider paths, -O for forwarded clients and FIDO.
Terminal
eval "$(ssh-agent -s -t 3)"ssh-add ~/.ssh/id_ed25519; ssh-add -lsleep 4; ssh-add -leval "$(ssh-agent -s -k)"timeout 2 ssh-agent -D -a /tmp/fg.socktimeout 3 ssh-agent -d -a /tmp/dbg.sockenv -u SSH_ASKPASS -u SSH_ASKPASS_REQUIRE \  ssh-agent -s -a /tmp/agent.sock
Output
$ env -u SSH_ASKPASS -u SSH_ASKPASS_REQUIRE ssh-agent -s -a /tmp/agent.sockSSH_AUTH_SOCK=/tmp/agent.sock; export SSH_AUTH_SOCK;SSH_AGENT_PID=101; export SSH_AGENT_PID;echo Agent pid 101;$ ls -l /tmp/agent.sock | cut -c1-10srw-------

142.5 ssh-agent on Windows (service)

  • The service is called OpenSSH Authentication Agent (ssh-agent); default is Stopped and Disabled.
  • Enable it in an admin PowerShell with Set-Service -StartupType Automatic, then Start-Service.
  • On Windows, keys added with ssh-add survive a reboot.
PowerShell
Get-Service ssh-agent |  Select Name,Status,StartType# Syntax only (admin PowerShell):# Set-Service ssh-agent -StartupType Automatic# Start-Service ssh-agent# Stop-Service ssh-agent
Output
Name       Status StartType----       ------ ---------ssh-agent Stopped  Disabled

142.6 ssh-add: adding keys

  • Empty agent: ssh-add -l gives exit code 1.
  • ssh-add without arguments finds the default key (id_ed25519) and asks the passphrase once.
  • -t 30 adds a key for 30 seconds, -q hides the success message.
  • -c adds a key that needs confirmation through askpass before every use (no askpass = operation refused).
  • -C only certificates, -k only plain keys, -h destination constraint for forwarded keys, --apple-use-keychain on macOS.
Terminal
ssh-add -lssh-addssh-add -t 30 ~/.ssh/id_workssh-add -D >/dev/nullssh-add -q ~/.ssh/id_ed25519ssh-add -c ~/.ssh/id_workssh-add -v -d ~/.ssh/id_work
Output
$ ssh-add -D >/dev/nullAll identities removed.$ ssh-add -q ~/.ssh/id_ed25519Enter passphrase for /root/.ssh/id_ed25519:  ****$ ssh-add -l | wc -l1$ ssh-add -v -d ~/.ssh/id_workIdentity removed: /root/.ssh/id_work ED25519 (work@n142)

142.7 ssh-add: list, test, remove and lock

CommandWhat it does
ssh-add -l / -LList fingerprints / full public keys
ssh-add -t 30 keyAdd a key for 30 seconds only
ssh-add -c keyAsk for confirmation (askpass) before every use
ssh-add -T key.pubTest that the agent's key can really sign (silent on success)
ssh-add -d key / -DRemove one key / all keys
ssh-add -x / -XLock / unlock the agent with a password
ssh-add -s / -e lib.soAdd / remove PKCS#11 token keys
exit 0 / 1 / 2Success / command failed / no agent found
Terminal
ssh-add -lssh-add -l -E md5ssh-add -L | cut -c1-60ssh-add -T ~/.ssh/id_ed25519.pubssh-add -d ~/.ssh/id_work; ssh-add -lssh-add -x; ssh-add -lssh-add -X; ssh-add -lssh-add -D; ssh-add -lSSH_AUTH_SOCK=/tmp/nothing.sock ssh-add -l
Output
$ ssh-add -d ~/.ssh/id_work; ssh-add -lIdentity removed: /root/.ssh/id_work ED25519 (work@n142)256 SHA256:drrfI471zC4zu//zqbkSqUiqd+1+RVW79IuoDK14vpk laptop@n142 (ED25519)$ SSH_AUTH_SOCK=/tmp/nothing.sock ssh-add -lError connecting to agent: No such file or directory(exit 2)

142.8 ssh-copy-id: installing your public key

  • -n is a dry run: shows which key would be added, copies nothing.
  • -i key.pub: the real run asks the password once and adds the key.
  • Running it again first tries a key login and skips keys that already exist.
  • -f forces the add (skips the check), so authorized_keys gets duplicate lines.
  • -p 2222 for a non-standard port; -o passes any ssh option; -t changes the target file; -s uses SFTP mode.
  • Windows has no ssh-copy-id: pipe the key through ssh as below.
Terminal
ssh-copy-id -n -i ~/.ssh/id_ed25519.pub dev@bastionssh-copy-id -i ~/.ssh/id_ed25519.pub dev@bastionssh-copy-id -i ~/.ssh/id_ed25519.pub dev@bastion   # again: skippedssh dev@bastion 'hostname; whoami'ssh-copy-id -f -i ~/.ssh/id_ed25519.pub dev@bastionssh-copy-id -p 2222 \  -o StrictHostKeyChecking=accept-new \  -i ~/.ssh/id_ed25519.pub ops@bastionssh -p 2222 ops@bastion whoami
Output
$ ssh dev@bastion 'hostname; whoami'bastiondev$ ssh -p 2222 ops@bastion whoamiops
PowerShell
type $env:USERPROFILE\.ssh\id_ed25519.pub |  ssh user@192.168.1.10 "mkdir -p ~/.ssh &&  cat >> ~/.ssh/authorized_keys"

142.9 Agent forwarding (ssh -A)

Agent forwarding through a bastion
  1. 1.Laptop: ssh -A dev@bastion

  2. 2.A forwarded agent socket is created on bastion (SSH_AUTH_SOCK set)

  3. 3.From bastion: ssh -p 2222 ops@target

  4. 4.Target's sign request travels through bastion to the laptop's agent

  5. 5.Laptop agent signs and sends the signature back

  6. 6.Target allows the login; the key never left the laptop

Terminal
ssh dev@bastion 'echo sock=$SSH_AUTH_SOCK; ssh-add -l'ssh -A dev@bastion 'echo sock=$SSH_AUTH_SOCK; ssh-add -l'ssh -o ForwardAgent=yes dev@bastion \  'ssh -o StrictHostKeyChecking=accept-new \   -p 2222 ops@192.168.162.20 whoami'
Output
$ ssh -o ForwardAgent=yes dev@bastion 'ssh -o StrictHostKeyChecking=accept-new -p 2222 ops@192.168.162.20 whoami'Warning: Permanently added '[192.168.162.20]:2222' (ED25519) to the list of known hosts.ops

Risk: while the session is open, root on bastion can use your key through the forwarded socket:

Output
[laptop]$ ssh -A -f dev@bastion sleep 15[bastion root]$ ls /tmp/ssh-*/agent.118[bastion root]$ SSH_AUTH_SOCK=$(ls /tmp/ssh-*/agent.*) ssh-add -l256 SHA256:drrfI471zC4zu//zqbkSqUiqd+1+RVW79IuoDK14vpk laptop@n142 (ED25519)

142.10 Comparison: agent forwarding vs ProxyJump

Pointssh -A (ForwardAgent)ssh -J (ProxyJump)
How the hop worksYou run ssh on the bastionLaptop tunnels through bastion to the target
Key exposureRoot on bastion can use your agentBastion never gets agent access
Use whenTrusted host, need git/ssh from itJust reaching an inner server

142.11 Common mistakes

  • Running ssh-agent -s without eval: the variables are printed but never set.
  • Error connecting to agent (exit 2): no agent in this shell or SSH_AUTH_SOCK points to the wrong socket.
  • Using ssh-copy-id -f repeatedly: duplicate lines in authorized_keys.
  • Enabling ForwardAgent for every host: use it only on trusted hosts, prefer ssh -J.
  • Leaving keys loaded forever: use ssh-add -t (e.g. 8h) and ssh-add -x when you leave the desk.

142.12 Interview tips

  • The agent keeps unlocked keys in memory and listens on a Unix socket named by SSH_AUTH_SOCK.
  • At login ssh hands the server's challenge to the agent; only a signature comes back, never the key.
  • With forwarding only sign requests travel, but a remote root user can use them while the session lives.
  • ssh-add exit codes: 0 success, 1 failed (e.g. no identities), 2 no agent.