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
- Step 1
1.ssh-copy-id logs in once with the password and appends id_ed25519.pub to authorized_keys
- Step 2
2.ssh-add asks the passphrase once and loads the unlocked key into the agent
- Step 3
3.ssh sends a login request to bastion (TCP 22, publickey auth)
- Step 4
4.Server replies with a random challenge
- Step 5
5.ssh hands the challenge to the agent; the agent returns only a signature
- Step 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.
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'$ 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 left142.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.
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$ 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.
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-agentName Status StartType---- ------ ---------ssh-agent Stopped Disabled142.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.
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$ 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
| Command | What it does |
|---|---|
| ssh-add -l / -L | List fingerprints / full public keys |
| ssh-add -t 30 key | Add a key for 30 seconds only |
| ssh-add -c key | Ask for confirmation (askpass) before every use |
| ssh-add -T key.pub | Test that the agent's key can really sign (silent on success) |
| ssh-add -d key / -D | Remove one key / all keys |
| ssh-add -x / -X | Lock / unlock the agent with a password |
| ssh-add -s / -e lib.so | Add / remove PKCS#11 token keys |
| exit 0 / 1 / 2 | Success / command failed / no agent found |
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$ 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.
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$ ssh dev@bastion 'hostname; whoami'bastiondev$ ssh -p 2222 ops@bastion whoamiopstype $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)
- Step 1
1.Laptop: ssh -A dev@bastion
- Step 2
2.A forwarded agent socket is created on bastion (SSH_AUTH_SOCK set)
- Step 3
3.From bastion: ssh -p 2222 ops@target
- Step 4
4.Target's sign request travels through bastion to the laptop's agent
- Step 5
5.Laptop agent signs and sends the signature back
- Step 6
6.Target allows the login; the key never left the laptop
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'$ 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.opsRisk: while the session is open, root on bastion can use your key through the forwarded socket:
[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
| Point | ssh -A (ForwardAgent) | ssh -J (ProxyJump) |
|---|---|---|
| How the hop works | You run ssh on the bastion | Laptop tunnels through bastion to the target |
| Key exposure | Root on bastion can use your agent | Bastion never gets agent access |
| Use when | Trusted host, need git/ssh from it | Just 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.