Sub topic 9
Using `ssh-agent` to Avoid Retyping Your Passphrase
Prerequisites: you should already have an SSH key pair installed on the server and be able to log in with ssh -i ~/.ssh/id_ed25519. This chapter focuses on the client-side ssh-agent, which caches an unlocked private key for a limited period.
What `ssh-agent` Does
A private key protected by a passphrase is safer if someone copies the key file. However, SSH normally asks for that passphrase each time it reads the key.
ssh-agent is a background program that holds an unlocked private key in memory. Your SSH client asks the agent to authenticate instead of reading and unlocking the private key for every connection. You enter the passphrase once for the agent session.
The private key file remains encrypted on disk. The agent does not send the private key to the server. It uses the key to sign an authentication request, and the server checks that signature against the public key in ~/.ssh/authorized_keys.
sequenceDiagram
participant Shell
participant Agent as ssh-agent
participant SSH as ssh client
participant Server
Shell->>Agent: ssh-add private key
Agent-->>Shell: Request passphrase
Shell->>Agent: Passphrase
SSH->>Agent: Sign authentication request
Agent-->>SSH: Digital signature
SSH->>Server: Signature
Server-->>SSH: Login acceptedStarting an Agent and Loading a Key
Start an agent in the current shell with:
eval "$(ssh-agent -s)"The command prints environment variables such as SSH_AUTH_SOCK and SSH_AGENT_PID. These variables tell SSH where to find the agent. eval applies the values to your current shell.
Load your private key:
ssh-add ~/.ssh/id_ed25519Enter the key's passphrase when prompted. Confirm that the agent has the key:
ssh-add -lThe output lists the fingerprints of loaded keys. A fingerprint is a short identifier derived from a public key.
Now connect normally:
ssh [email protected]SSH automatically contacts the agent when the agent socket is available. You do not need to provide -i if SSH can find the key in its default location or your client configuration names it.
Controlling Lifetime and Keys
A key loaded into an agent remains available until you remove it, stop the agent, or the key reaches its configured lifetime. Limit the lifetime when using a shared or portable computer:
ssh-add -t 1h ~/.ssh/id_ed25519This loads the key for one hour. You can also require confirmation before each use:
ssh-add -c ~/.ssh/id_ed25519List loaded keys:
ssh-add -lRemove one key:
ssh-add -d ~/.ssh/id_ed25519Remove all keys from the current agent:
ssh-add -DThe agent process continues until it exits or you stop it:
ssh-agent -k| Command | Purpose |
|---|---|
| `ssh-add -l` | List loaded key fingerprints |
| `ssh-add -D` | Remove every key from the agent |
| `ssh-agent -k` | Stop the agent started by the current shell |
Troubleshooting the Agent
If SSH asks for the passphrase every time, check whether the agent socket exists:
printf '%s\n' "$SSH_AUTH_SOCK"
ssh-add -lIf SSH_AUTH_SOCK is empty or ssh-add -l reports that it cannot connect to the agent, start the agent again in the same shell and reload the key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519If the agent has a key but SSH still does not use it, inspect the connection:
ssh -v [email protected]Look for messages about the agent, offered public keys, and rejected identities. A client configuration with IdentitiesOnly yes can prevent SSH from trying agent keys unless the matching IdentityFile is configured.
For desktop environments, an operating system keychain may start and manage an agent automatically. Avoid adding repeated ssh-agent commands to shell startup files until you check whether one is already running. Multiple agents can cause confusion because each shell may point to a different socket.
Exercise
You loaded ~/.ssh/id_ed25519 into an agent, but ssh-add -l says no identities are present. What should you check first?
Show answer
Check SSH_AUTH_SOCK and confirm that the shell is connected to the intended agent. Then start an agent in that shell and run ssh-add ~/.ssh/id_ed25519 again.