Using ssh-agent and ssh-add in Ubuntu Linux
When working with SSH keys in Ubuntu Linux, you may encounter an error when trying to list or add keys using
ssh-add
:
username@host:~$ ssh-add -l Could not open a connection to your authentication agent. username@host:~$
This error occurs because the SSH authentication agent (
ssh-agent
) is not running in the current shell session.
What is ssh-agent?
ssh-agent
is a background process that securely stores your private SSH keys in memory. Once your key is loaded into the agent, you can authenticate to remote systems without repeatedly entering your passphrase.
The
ssh-add
command is used to manage keys stored by the agent.
Starting the SSH Agent
To start the agent and configure your current shell to use it, run:
eval "$(ssh-agent -s)"
Example output:
username@host:~$ eval "$(ssh-agent -s)" Agent pid 71288 username@host:~$
The command starts
ssh-agent
and sets the required environment variables for the current terminal session.
Verifying the Agent Status
After starting the agent, check whether any SSH keys are currently loaded:
username@host:~$ ssh-add -l The agent has no identities. username@host:~$
This message confirms that the agent is running, but no SSH keys have been added yet.
Adding an SSH Key
To load your private key into the agent, use:
ssh-add ~/.ssh/id_rsa
Example:
username@host:~$ ssh-add ~/.ssh/id_rsa Enter passphrase for /home/username/.ssh/id_rsa: Identity added: /home/username/.ssh/id_rsa (/home/username/.ssh/id_rsa) username@host:~$
If your private key is protected with a passphrase, you will be prompted to enter it once. The key is then stored in the agent’s memory.
Listing Loaded Keys
To verify that your key has been successfully loaded, run:
username@host:~$ ssh-add -l 4096 SHA256:BPvjix+e19eq0QPSPkso+EAFq1trB/Z62MmMlzfEXY /home/username/.ssh/id_rsa (RSA) username@host:~$
The output shows:
- The key size (4096 bits)
- The key fingerprint
- The key file path
- The key type (RSA)
Your SSH key is now available for authentication.
Useful ssh-add Commands
List loaded keys:
ssh-add -l
List public key fingerprints:
ssh-add -L
Remove a specific key:
ssh-add ~/.ssh/id_rsa -d
Remove all loaded keys:
ssh-add -D
Common Troubleshooting
“Could not open a connection to your authentication agent”
Start the agent first:
eval "$(ssh-agent -s)"
“The agent has no identities”
The agent is running correctly, but no keys have been added. Load a key with:
ssh-add ~/.ssh/id_rsa
“No such file or directory”
Verify that the key exists:
ls -la ~/.ssh/
You may be using a different key name, such as:
~/.ssh/id_ed25519 ~/.ssh/id_ecdsa
Conclusion
The combination of
ssh-agent
and
ssh-add
simplifies SSH authentication by securely storing private keys in memory. If
ssh-add
reports that it cannot connect to the authentication agent, simply start the agent with:
eval "$(ssh-agent -s)"
Then add your private key:
ssh-add ~/.ssh/id_rsa
After the key is loaded, SSH connections can use it automatically throughout your terminal session, reducing the need to repeatedly enter your passphrase.