Unix / Linux ssh-add Command Examples to Add SSH Key to Agent
ssh-add is a helper program for ssh-agent.
ssh-add adds RSA or DSA identity files to the ssh agent. For ssh-add to work properly, the agent should be running, and have the SSH_AUTH_SOCK environment variable set.
- Fix “Could not Open” Error (and Add Default RSA/DSA identities)
By default, when you try to execute the ssh-add command, you might get “Could not open a connection to your authentication agent.” error message as shown below.
1 | $ ssh-add |
1 | $ ssh-add |
- Display the entries loaded in ssh-agent
Use either -l or -L as shown below to display all the RSA and DSA entries that are currently loaded into the ssh-agent.
The following examples shows that there are two entries currently loaded to the ssh-agent.
1 | $ ssh-add -l |
- Delete all entries from ssh-agent
Use option -D as shown below to remove all the ssh entries from the ssh-agent.
1 | $ ssh-add -D |
1 | $ ssh-add -l |
- Delete specific entries from ssh-agent
Using -d option, you can specify exactly what entries you like to delete.
The following example will remove only the default RSA entry from the ssh-agent.
1 | $ ssh-add -l |
- Lock (or) Unlock the SSH Agent
You can lock the ssh agent as shown below using -x option. Once you lock the agent, you cannot add, delete, or list entries in the ssh agent without a password.
1 | $ ssh-add -x |
1 | $ ssh-add |