« Back to SSH: Frequently Asked Questions

How to start the 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

Other questions related to SSH

Feedback

Is the described answer not working or incorrect, got another tip or question? Share your thoughts!