In this guide, we’ll explain you in detail what is an ssh-agent
, how it can be used as a local SSH key store to perform SSH key based authentication, what is meant by SSH agent forwarding, how SSH agent forwarding works, what are the security risks associated with using ssh-agent
forwarding and what alternate solutions exist to avoid using the SSH agent forwarding feature.
What is an SSH Agent?
An SSH agent is a background program that securely manages your SSH private keys and handles authentication on your behalf. It allows you to unlock your keys once (using their passphrase), and then use them multiple times without re-entering the passphrase.
Think of it as a secure key carrier that holds your decrypted keys so you don’t have to retrieve and re-decrypt them every time you need to open a locked door.
How to Use an SSH Agent
Here are the basic steps to start an agent and add your keys to it:
Start the Agent:
Open a terminal and run the following command. This starts the agent process and sets the necessary environment variables for your current shell.
eval $(ssh-agent -s)
Add Your Private Key:
Use the ssh-add command to add your private key to the agent. If your key has a passphrase, you will be prompted to enter it now. This is the only time you’ll need to enter it for this session.
ssh-add ~/.ssh/id_rsa
Replace id_rsa
in the above command with the name of your private key file, if it’s different.
Verify the Key:
You can check which keys are currently loaded in the agent’s memory with the following command:
ssh-add -l
Support in Various OSes:
In MacOS: ssh-agent works with MacOS Keychain and can store your SSH key passphrase securely.
In Linux: Tools like keychain
or gnome-keyring
can persist SSH agent sessions across logins.
How an SSH Agent Keeps Your Keys Safe
The agent enhances security in two key ways:
- Protects Your Passphrase:
- When you add a key to the agent, the passphrase is used once to decrypt the key.
- The decrypted key is then stored in a secure, isolated area of your computer’s memory.
- Your passphrase is never stored and does not have to be entered or transmitted again. This is much safer than using a key without a passphrase.
- Keeps the Private Key Isolated:
- The agent handles all authentication requests by signing them internally.
- The actual private key file itself is never read again after being added to the agent,
- The decrypted key never leaves the agent’s memory.
- This prevents the key from being exposed to other programs or malicious actors on your system.
The actual key file is not used again.
All authentication is done by signing requests inside the agent, keeping the key secure from other processes.
SSH private key is used for authentication purpose only (signing a message with its signature) and not for encrypting or decrypting the traffic tranmissed through the SSH connection. So, the keys can stay safe inside the agent and the SSH communication protocol would still work.
What is Agent Forwarding?
Agent forwarding is an SSH feature that allows you to use your local machine’s SSH agent to authenticate to a remote server, and then from that remote server, authenticate to yet another server, and so on.
This is extremely useful for securely jumping from one server to another without ever having to place your private key file on any of the intermediate servers.
How Agent Forwarding Works
Initial Connection: When you connect to a remote server using the -A flag, your local SSH client requests agent forwarding.
ssh -A user@remote-server
Agent Socket Creation: Upon a successful connection, the SSH daemon on remote-server creates a temporary, unique unix-socket file. This socket acts as a secure communication channel back to your local machine.
Authentication Forwarding: When you, from remote-server, attempt to connect to a third server (third-server), the SSH client on remote-server sends the authentication request through that temporary unix-socket.
Local Agent Action: This request travels back over the secure SSH connection to your local machine. Your local SSH agent receives the request, signs it with your private key (which is still only in your local memory), and sends the signed response back.
Final Authentication: The response is forwarded from remote-server to third-server, which verifies the signature and grants you access.
Crucially, the private key itself is never sent to remote-server. Only the authentication request and the signed response travel back and forth, ensuring your key remains safe and isolated on your local machine.
Agent Forwarding - Security Risks and Alternate Solutions
- A root user on the remote server could potentially access the agent forwarding socket and use your key to authenticate as you.
- Only use agent forwarding on machines you trust completely.
- Do not turn on agent forwarding by default in your SSH config file by setting
ForwardAgent
toyes
. Instead usessh-agent -A
to turn on agent forwarding selectively for one-off sessions. - Better yet, don’t use the agent forwarding feature at all. Instead, setup and use a
bastion host
orjump host
as an SSH proxy to jump and access all internal hosts in your org.