Search code examples
bashsshubuntu-12.04ssh-keysssh-agent

Could not open connection to authentication agent even though SSH_AUTH_SOCK defined


First I start the ssh-agent with

eval `ssh-agent -s`

I confirm that SSH_AUTH_SOCK is set by running echo $SSH_AUTH_SOCK, which encouragingly returns /tmp/ssh-oCsFfHvR3345/agent.3345.

Yet when I run sudo ssh-add ~/.ssh/id_rsa, I get

Could not open a connection to your authentication agent.

Moreover, ssd-add -l returns The agent has no identities.


Solution

  • If you're running SSH commands through sudo the environment variables won't be set.

    From man sudoers:

    By default, the env_reset option is enabled. This causes commands to be executed with a new, minimal environment. On AIX (and Linux systems without PAM), the environment is initialized with the contents of the /etc/environment file. The new environment contains the TERM, PATH, HOME, MAIL, SHELL, LOGNAME, USER, USERNAME and SUDO_* variables in addition to variables from the invoking process permitted by the env_check and env_keep options. This is effectively a whitelist for environment variables.

    You can check which environment variables are available to your sudo commands by running sudo env.

    The above would explain the behaviour you describe but you really should be running ssh commands as a regular (non-super) user.