Start the Agent and Add a Key
Run these on your own computer, not on the server:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
The first line starts the agent and sets two environment variables in your shell, SSH_AUTH_SOCK and SSH_AGENT_PID, which tell ssh where to find it. eval is needed because the agent prints those settings as shell commands. ssh-add then asks for the passphrase once. Now connect as usual:
ssh user@your_server_ip
No passphrase prompt appears. Many desktop sessions start an agent at login already; if ssh-add -l works in a new terminal, you do not need the eval line. Creating a key with a passphrase is covered in how to generate SSH keys.
What the Agent Holds
The agent keeps decrypted private keys in memory and never hands them out. When ssh needs to prove who you are, it sends the server's challenge to the agent, and the agent signs it. Programs that can reach the agent's socket can ask for signatures, but cannot read the key itself. When the agent process stops, the keys are gone from memory, and the key file on disk stays encrypted with its passphrase. An agent started with eval can keep running after you close the terminal; ssh-agent -k in the same shell stops it.
Manage Keys with ssh-add
| Command | What it does |
|---|---|
ssh-add -l | Lists key fingerprints |
ssh-add -L | Prints the public keys |
ssh-add -t 1h | Adds a key for one hour |
ssh-add -c | Asks before each use |
ssh-add -d key | Removes one key |
ssh-add -D | Removes all keys |
A lifetime limits how long an unlocked key sits in memory, which helps on a laptop that you leave unattended. ssh-agent -t 1h sets a default lifetime for every key added to that agent. The -c option shows its prompt through an askpass program (ssh-askpass by default, or the one named in SSH_ASKPASS), so it needs a graphical session with one installed.
Load Keys Automatically from the SSH Config
Instead of running ssh-add by hand, let ssh add a key to the agent the first time you use it. In ~/.ssh/config:
Host *
AddKeysToAgent yes
IdentityFile ~/.ssh/id_ed25519
With AddKeysToAgent yes, the first connection asks for the passphrase and stores the key in the running agent; later connections use it. AddKeysToAgent confirm adds it so that each use must be confirmed, and ask asks before adding it. The option does not start an agent; one must be running. More settings are in the SSH config file.
Agent Forwarding and Its Risk
Agent forwarding lets a server you are logged in to use your local agent, for example to pull from a Git repository with your key. The risk: while you are connected, anyone with root access on that server, or anyone who has broken into it, can use the forwarded socket to log in to other servers as you. They cannot copy the key, but they do not need to. Forward the agent only to servers you fully trust, and never to shared or untrusted ones. With that understood, the option is:
ssh -A user@your_server_ip
Keep ForwardAgent off in the Host * section and turn it on only for specific hosts that need it.
ProxyJump: The Safer Way Through a Jump Host
When you forward the agent only to hop through a gateway server to an internal one, use a jump host instead:
ssh -J user@your_jump_host user@your_server_ip
ssh opens a tunnel through the jump host and authenticates to the final server directly from your computer. The jump host only passes encrypted traffic and never gets access to your agent. To make it permanent:
Host internal
HostName your_server_ip
User user
ProxyJump user@your_jump_host
Other forwarding types are in SSH tunneling.
Could Not Open a Connection to Your Authentication Agent
ssh-add prints this when no agent is reachable from the current shell, usually because SSH_AUTH_SOCK is not set. Causes and fixes:
- No agent running: start one with
eval "$(ssh-agent -s)"in the same shell. - A new shell or tmux window: the variable was set in another shell. Run
echo $SSH_AUTH_SOCKto check. - Running with sudo: with its default settings sudo starts commands in a reduced environment without that variable, so
sudo ssh-addfinds no agent. Use the agent as your own user.
If the agent has keys but the server still refuses them, see SSH permission denied (publickey).