How to Use the Linux ssh-agent to Manage Multiple SSH Keys Without Passwords

The SSH Password Problem

Secure Shell (SSH) is the standard method for remotely managing Linux servers. Most administrators quickly move away from using standard typed passwords and switch to SSH Keys (cryptographic key pairs) for enhanced security.

However, when you generate an SSH key, best security practices dictate that you encrypt the private key on your hard drive with a “passphrase”. This means every time you want to connect to a remote server, you must type in your local passphrase to decrypt the key before it can be used.

If you manage dozens of servers and are constantly logging in and out, typing your complex passphrase fifty times a day is incredibly tedious. Some users get so frustrated that they generate SSH keys without passphrases, creating a massive security vulnerability if their laptop is ever stolen.

The solution is ssh-agent. It is a background program that securely holds your decrypted private keys in your computer’s RAM. You type your passphrase exactly once when you log in to your workstation, and ssh-agent automatically handles authentication for the rest of your session.

Step 1: Starting the ssh-agent

On many modern Linux desktop environments (like Ubuntu with GNOME), ssh-agent is already started automatically when you log in. If you are on a minimal system or it isn’t running, you must start it manually.

Open your terminal and run:

eval "$(ssh-agent -s)"

This command starts the agent in the background and sets the necessary environment variables (like SSH_AUTH_SOCK) so that your terminal knows how to talk to it. The output will display the Agent’s Process ID (e.g., Agent pid 15234).

Step 2: Adding Your Keys to the Agent

Now that the agent is running, you need to load your private SSH keys into its memory.

Use the ssh-add command followed by the path to your private key file. If you used the default location, it will be in your ~/.ssh directory.

ssh-add ~/.ssh/id_rsa

(Note: If you use a modern ED25519 key, the path would be ~/.ssh/id_ed25519)

The terminal will prompt you: “Enter passphrase for /home/user/.ssh/id_rsa:”.

Type your passphrase and press Enter. You should see a confirmation message stating Identity added.

Step 3: Managing Multiple Keys

The true power of ssh-agent is managing multiple identities. Perhaps you have one key for your personal GitHub account, another for your company’s production servers, and a third for an external client.

You simply run the ssh-add command for each individual key:

ssh-add ~/.ssh/github_key
ssh-add ~/.ssh/corporate_server_key

Once loaded, ssh-agent sits in the background. When you type ssh [email protected], the SSH client automatically asks the agent for the correct key. The agent provides it instantly, and you are logged in without ever seeing a password prompt.

Step 4: Viewing and Removing Keys

To see a list of all the decrypted keys currently held in the agent’s memory, use the -l (list) flag:

ssh-add -l

If you are stepping away from your desk or want to instantly lock down your terminal, you can flush all keys from the agent’s memory using the -D (delete all) flag:

ssh-add -D

The next time you try to connect to a server, you will be forced to type your passphrase again to re-add the key.

Automating the Process

To avoid typing eval "$(ssh-agent -s)" every time you open a new terminal window, you can add that command to your ~/.bashrc or ~/.zshrc file. However, this will spawn a new agent process for every window. A better approach for advanced users is to configure a single agent instance or use a modern wrapper like keychain to manage the ssh-agent process for you automatically across all terminal sessions.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.