« Back to SSH: Frequently Asked Questions

How to start the SSH agent?

This article has last been updated at .

How to start the SSH agent?

Run the eval command inside the shell together with the ssh-agent.

eval $(ssh-agent)

The ssh-agent command is started manually using eval $(ssh-agent). This will initiate the SSH agent and make it available for clients, such as ssh, to use it.

To confirm that the agent is running is by looking at the SSH_AUTH_SOCK environment variable.

Automatic start of SSH agent

Gnome Keyring SSH Agent

When using Gnome, it typically comes with its SSH agent as part of Keyring. This will automatically load any files in ~/.ssh when both the secret and public key is available.

Systemd service unit

Create a unit file (/etc/systemd/system/ssh-agent.service):

[Unit]
Description=SSH key agent

[Service]
Type=simple
Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket
ExecStart=/usr/bin/ssh-agent -D -a $SSH_AUTH_SOCK

[Install]
WantedBy=default.target

Then enable it.

systemctl enable --now ssh-agent.service

Start via login profile

When using bash, add the following snippet to your profile ~/.bash_profile to active it upon login.

if [ -z "$SSH_AUTH_SOCK" ] ; then
  eval $(ssh-agent -s)
  ssh-add
fi

This checks for the presence of the environment variable. If it is empty (-z), then it will start the agent and add keys.

Relevant commands in this article

Like to learn more about the commands that were used in this article? Have a look, for some there is also a cheat sheet available.

  • ssh-agent
  • ssh-add

See the full list of Linux commands for additional system administration tools.

Other questions related to SSH

Feedback

Small picture of Michael Boelen

This article has been written by our Linux security expert Michael Boelen. With focus on creating high-quality articles and relevant examples, he wants to improve the field of Linux security. No more web full of copy-pasted blog posts.

Discovered outdated information or have a question? Share your thoughts. Thanks for your contribution!

Mastodon icon

Related articles

Like to learn more? Here is a list of articles within the same category or having similar tags.