SSH agent (ssh-agent) is a background program that holds unlocked SSH private keys and signs authentication requests for the SSH client. Instead of entering your private-key passphrase for every SSH, SFTP or Git connection, you unlock the key once and let the agent use it while it is available.
Already running an agent? You can skip the startup command and go straight to checking or adding a key. You need an existing private key; if you have none, follow SSH key generation steps first.
ssh-add -lssh-agent holds keys; ssh-add loads, lists and removes them. ssh-add -l checks whether your current shell can reach an agent. The agent does not give remote servers your private key, but processes that can access its socket may request signatures.
SSH agent quick start
These are the shortest working paths for a typical local OpenSSH setup. Substitute your actual private-key filename if it is not id_ed25519; never pass a .pub file to ssh-add.
Linux and macOS (Terminal)
ssh-add -l # Check for an existing agent
# Only if no agent is reachable:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -l # Verify the key was loadedssh-add -l can say “The agent has no identities” even when the agent is running; in that case, just run ssh-add ~/.ssh/id_ed25519. Most macOS desktops and many Linux desktop sessions already supply an agent. Do not start another agent on every terminal launch without checking first.
Windows 10/11 (PowerShell)
# Run the next three commands in PowerShell as Administrator:
Get-Service ssh-agent
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent
# Then use a normal PowerShell window:
ssh-add $env:USERPROFILE\.ssh\id_ed25519
ssh-add -lThis uses the built-in Windows OpenSSH Authentication Agent service, not the per-shell Unix eval method. On an organization-managed PC, service settings may be controlled by policy. See the Windows-specific instructions below.
What does ssh-agent do, exactly?
An SSH client authenticates with a public/private key pair. An agent holds the unlocked private-key identity in memory and performs a signing operation when an authorized client asks. The server verifies the signature using the public key. The agent does not encrypt the SSH traffic: encryption uses separate session keys negotiated by the SSH connection.
ssh, Git, SFTPThe private key is not sent to the remote host. A local process with access to your agent can still ask it to sign requests, so agent-socket access matters.
Why not just use the key file directly?
Without an agent, OpenSSH can read an encrypted private-key file and prompt for the passphrase on each new connection. With an agent, the passphrase unlocks the key when you add it; subsequent supported connections can reuse the agent. If a key has no passphrase, you may not need an agent just to avoid prompts, although agents also support hardware-backed identities and other workflows.
What do SSH_AUTH_SOCK and SSH_AGENT_PID mean?
On Unix-like systems, SSH_AUTH_SOCK points to the local Unix-domain socket through which clients communicate with the agent. SSH_AGENT_PID, when provided by a manually started agent, identifies its process. An agent can exist without your current shell knowing its socket path.
echo "$SSH_AUTH_SOCK"
ssh-add -lSecurity distinction: the agent protocol does not provide a normal command to export an already loaded private key. But it does offer signing operations: malware running as your account, or an untrusted remote machine when you enable forwarding, may abuse that capability. See security guidance.
How to set up ssh-agent on each operating system
Linux: use the session agent or start one manually
Many graphical desktop environments start an agent at login; headless shells and minimal environments may not. First run ssh-add -l. If it says it cannot connect to an authentication agent, start one for the current shell:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519What does eval "$(ssh-agent -s)" do?
ssh-agent -s starts an agent and prints shell statements that set values such as SSH_AUTH_SOCK. $(...) captures that output, and eval evaluates it in the current shell. Otherwise the new agent can run while your shell does not know how to reach it.
$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-XXXXXXXXXX/agent.1234; export SSH_AUTH_SOCK;
SSH_AGENT_PID=1235; export SSH_AGENT_PID;
echo Agent pid 1235;This output is illustrative: modern OpenSSH builds and operating systems may choose different socket locations. It is also safe to launch a command under the agent with ssh-agent your-command when that workflow suits you.
macOS: SSH agent and Keychain
On macOS, the login session normally provides an agent. To add a passphrase-protected key and save its passphrase in Apple's Keychain, use Apple's version of ssh-add:
/usr/bin/ssh-add --apple-use-keychain ~/.ssh/id_ed25519
ssh-add -lThe first command stores the key's passphrase in Keychain; the private key itself remains in its configured location. For automatic use, put a host-specific block in ~/.ssh/config (replace the hostname and path):
Host github.com
IgnoreUnknown UseKeychain
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519IgnoreUnknown UseKeychain helps configurations shared with OpenSSH builds that do not recognize Apple's UseKeychain setting. If you get illegal option -- apple-use-keychain, check which ssh-add; a Homebrew or other non-Apple build may not support it. /usr/bin/ssh-add is the Apple binary. If your key has no passphrase, use ordinary ssh-add instead of the Keychain flag. Older macOS releases used -K; current macOS uses --apple-use-keychain.
For a longer macOS setup checklist and errors, see below.
Windows: start the OpenSSH Authentication Agent service
Open an elevated PowerShell terminal for service configuration. Then add the key from a regular PowerShell terminal:
# Administrator PowerShell
Get-Service -Name ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent
# Normal PowerShell
ssh-add $env:USERPROFILE\.ssh\id_ed25519
ssh-add -l
If Git for Windows still asks for your passphrase, it may be calling its bundled SSH executable instead of the Windows OpenSSH client that talks to this service. Check where.exe ssh in PowerShell and consider configuring Git to use the system SSH client:
git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"That Git setting affects all repositories for your user; apply it only if using Windows OpenSSH is intentional. Microsoft's OpenSSH guide documents the service setup.
ssh-add command cheat sheet
ssh-agent starts or manages an agent process; ssh-add manages identities in a running agent. The uppercase/lowercase flags matter.
| Command | Purpose |
|---|---|
ssh-add ~/.ssh/id_ed25519 | Add an existing private key (not the .pub file). |
ssh-add -l | List key fingerprints and types; also checks reachability. |
ssh-add -L | Print loaded public keys, not private keys. |
ssh-add -t 1h ~/.ssh/id_ed25519 | Add a key with a one-hour lifetime. |
ssh-add -c ~/.ssh/id_ed25519 | Require confirmation for use (needs a working confirmation program). |
ssh-add -d ~/.ssh/id_ed25519 | Remove one identity from the agent. |
ssh-add -D | Remove all identities; does not delete key files. |
ssh-add -x / ssh-add -X | Lock / unlock the agent with an agent lock password. |
ssh-agent -k | Stop a manually started Unix agent identified by your environment. |
Example output from a test identity (fingerprint deliberately shortened):
$ ssh-add -l
256 SHA256:EXAMPLE... test-key (ED25519)
“The agent has no identities.” means the agent is reachable but empty; “Could not open a connection to your authentication agent” means it is unreachable. If you need to copy a public key, ssh-add -L prints it without exposing the private key. Fingerprints and public keys can identify your environment, so redact them in shared screenshots.
SSH_AUTH_SOCK: how to diagnose a missing or stale socket
SSH_AUTH_SOCK is the agent's contact address, not a key or a password. On Unix-like systems, a shell can lose track of a working agent, particularly after reconnecting to SSH, entering a container or reattaching to tmux.
printf 'socket: %s\n' "$SSH_AUTH_SOCK"
test -n "$SSH_AUTH_SOCK" && test -S "$SSH_AUTH_SOCK" && echo 'socket exists'
ssh-add -l| Result | Interpretation / next step |
|---|---|
| Variable empty | Current shell does not know the agent socket. Recover the session's agent environment or start an agent if none exists. |
Socket path exists, ssh-add -l fails | Socket might be inaccessible, stale or belong to a different agent implementation; check the current environment. |
| Agent has no identities | The socket works; add your key with ssh-add. |
| Fingerprints listed | Agent works and has loaded identities. |
SSH_AUTH_SOCK and tmux
tmux may retain a stale environment after an SSH reconnect. Check the session's value with tmux show-environment SSH_AUTH_SOCK. New panes may inherit updated tmux environment values, but an already-running shell does not automatically update its variables. Confirm the correct socket from your new login rather than copying an old path blindly.
For a focused checklist, see How to fix “Could not open a connection to your authentication agent”.
Key lifetime, removing keys and stopping the agent
ssh-agent normally keeps an added identity available until it is removed or the agent stops. That is not always the same thing as closing one terminal window. On Windows, an agent managed as a service has different lifecycle behavior.
# Load a key for one hour
ssh-add -t 1h ~/.ssh/id_ed25519
# Remove only this key
ssh-add -d ~/.ssh/id_ed25519
# Clear every identity (key files remain on disk)
ssh-add -D
# Stop a Unix agent started by this shell
ssh-agent -kssh-agent -t 1h sets a default lifetime for identities added to that particular agent. By contrast, ssh-add -t 1h sets the lifetime while adding a key. ssh-add -c can require interactive confirmation on each use, provided an appropriate confirmation program works in your environment.
SSH agent forwarding: ssh -A, ssh -a and safer alternatives
Agent forwarding lets a program on a remote machine request signatures from your local agent over an existing SSH connection. It is useful when a remote server must access another host or Git repository without storing a private key on that server. But a privileged or compromised remote host could abuse the forwarded agent while the connection is live.
| Option | Effect |
|---|---|
ssh -A user@host | Enable forwarding for that connection. |
ssh -a user@host | Disable forwarding for that connection, even if config would enable it. |
ForwardAgent yes | Enable forwarding from SSH config for matching hosts; avoid broad Host * defaults. |
ssh -J bastion.example.com [email protected] | Connect through a jump host without giving it a forwarded agent (when this fits the network setup). |
Important: ssh -a only controls forwarding. It does not stop your local SSH client from offering public keys or using a local agent to authenticate to the first host. To control which keys that first connection offers, use explicit IdentityFile and IdentitiesOnly yes settings when appropriate.
Keep forwarding disabled unless a particular task requires it. For a bastion-to-internal-server connection, try ProxyJump (ssh -J) first; the final SSH authentication can stay on your local machine. Never enable ForwardAgent yes globally just for convenience.
Disable an inherited ForwardAgent setting
# Even if ~/.ssh/config enables forwarding for this host:
ssh -a [email protected]The lowercase flag does not delete any identity from the agent. It only prevents forwarding your agent connection to that remote host.
Can a remote server steal your SSH key?
Standard forwarding does not transmit your raw private key, but anyone on the remote host who gains access to the forwarded socket could ask your agent to sign authentication requests while it is reachable. That can be enough to impersonate you. Consider agent confirmations (ssh-add -c), time limits, destination-constrained keys where supported, or skipping forwarding entirely. If the local machine is compromised, the agent cannot make that machine trustworthy.
macOS Keychain troubleshooting
| Symptom | What to check |
|---|---|
illegal option -- apple-use-keychain | Use Apple's /usr/bin/ssh-add rather than a Homebrew/OpenSSH binary without Keychain integration. |
Bad configuration option: UseKeychain | Check which ssh and whether your OpenSSH build supports Apple's setting; IgnoreUnknown UseKeychain can help cross-platform configs. |
| Passphrase requested after restart | Check that the passphrase was stored with --apple-use-keychain, the right key is configured, and Keychain is unlocked. |
| Keychain set up but agent empty | Remember that storing a passphrase in Keychain and loading a key into the agent are separate actions; inspect with ssh-add -l. |
See GitHub's platform-specific SSH setup guide for current macOS conventions.
Common ssh-agent problems and fixes
“Could not open a connection to your authentication agent”
On Linux/macOS, inspect SSH_AUTH_SOCK and ssh-add -l. If there is no session agent, start one using eval "$(ssh-agent -s)". If another agent already exists (macOS, GNOME, 1Password, etc.), recover its correct socket instead of starting duplicates. On Windows, check Get-Service ssh-agent.
“The agent has no identities”
The agent is reachable. Add the correct private key, e.g. ssh-add ~/.ssh/id_ed25519, then run ssh-add -l again. An agent is not the same thing as a server account's authorized_keys file.
SSH keeps requesting the passphrase
Confirm you loaded the same key the SSH client actually uses. Check ssh -v user@your-server, your IdentityFile setting, and (on Windows) whether Git invokes a different SSH binary. For macOS, also check Keychain settings.
“Too many authentication failures” after adding keys
Your client may be offering more agent identities than the server allows. Configure the intended IdentityFile and IdentitiesOnly yes for the relevant host; see our step-by-step fix.
Agent cannot be stopped: SSH_AGENT_PID not set
ssh-agent -k needs a process ID for the particular Unix agent you want to stop. A desktop-session agent or one started in another shell may not export that variable here. Avoid killing arbitrary agent processes; clear the identities with ssh-add -D if your goal is simply to unload keys.
SSH agent FAQ
Is ssh-agent required to use SSH keys?
No. OpenSSH can read a private-key file directly and ask for its passphrase. The agent adds convenience and agent-backed workflows.
Does ssh-agent store the passphrase or the private key?
OpenSSH's agent keeps the usable private-key identity in memory after you unlock it; it does not normally need to retain the passphrase separately. macOS Keychain is an additional OS feature that can remember passphrases.
Does ssh-agent run on the server?
Usually it runs on the client machine. The server's SSH daemon (sshd) verifies authentication. Forwarding exposes a connection to the client's agent on the remote host; it does not move the agent's private keys there.
What is the difference between ssh-agent and ssh-add?
ssh-agent is the running authentication-agent program or service. ssh-add is the utility for adding, listing and removing identities in that agent.
Should I put eval "$(ssh-agent -s)" in .bashrc?
Not unconditionally. It can create a new agent for every shell, leaving redundant processes. First determine whether your OS or login session already provides one, and use a session-level or conditional setup if needed.
Can I use ssh-agent with GitHub?
Yes. Add the private key to your local agent and register the public key with your GitHub account. On Windows, make sure Git uses the SSH executable that can communicate with your agent.
Official documentation and further reading
- OpenSSH ssh-agent(1) — agent lifecycle, socket, options and forwarding
- OpenSSH ssh-add(1) — key operations, lifetimes, confirmations
- GitHub Docs — macOS, Windows and Linux setup
- Microsoft Learn — Windows ssh-agent service
- Smallstep's explanation — forwarding risk and ProxyJump
Want terminal, SFTP and code editing in one workspace per server? ShadeTerm works over standard SSH without installing a proprietary agent on your servers.
Explore ShadeTerm →Conclusion
Use ssh-add -l to check for an agent, ssh-add to load a passphrase-protected key, and ssh-add -t when you want a time limit. On Linux/macOS, only start a new agent if you need one; on Windows, use the OpenSSH Authentication Agent service. Keep agent forwarding off by default and prefer ssh -J for jump-host access when possible.