4

Whenever I enter my password, in e.g git, ssh, I want it to be remembered.

So I have conifgured ssh-agent

I also added AddKeysToAgent yes to ~/.ssh/config

If I run ssh-add now every thing is fine. But without it, it doesn't work (I have to enter my password everytime)

I know I could add ssh-add to an init script but that's not what I want since I don't need it every day. I thought that's what AddKeysToAgent yes is for?

I start ssh-agent as a systemd service with SSH_AUTH_SOCK=$XDG_RUNTIME_DIR/ssh-agent.socket.

update

For testing I removed the service and SSH_AUTH_SOCK. Rebooted and ran eval $(ssh-agent) then logged in ssh pi@my-ip entered password and did it again. Still the password prompt. So it doesn't seem to be an issue with systemd.

update 2

Moving AddKeysToAgent yes to the first line fixed it with eval.

4
  • Is the SSH_AUTH_SOCK environment variable in your environment and does it contain the pathname of a socket file that exists?
    – Kusalananda
    Apr 2, 2020 at 9:52
  • yeah that's the correct path, the socket also exists, otherwise I asume ssh-add wouldn't work? Apr 2, 2020 at 11:18
  • so did you get it to work with systemd service?
    – Yar
    Oct 21, 2020 at 19:50
  • jep. as a said in my answer there was an issue with the format of ~/.ssh/config Oct 22, 2020 at 12:52

2 Answers 2

7

ok so the issue was the format of ~/.ssh/config. It was

Host awesomehost.tld
    User user
    IdentityFile ~/.ssh/id_rsa
AddKeysToAgent yes

It needs to be either

Host awesomehost.tld
    User user
    IdentityFile ~/.ssh/id_rsa
Host *
    AddKeysToAgent yes

or

AddKeysToAgent yes
Host awesomehost.tld
    User user
    IdentityFile ~/.ssh/id_rsa
1
  • thanks the IdentifyFile line was missing for me... Nov 25, 2022 at 22:50
1

As an alternative to starting the systemd service, you potentially could add the following code to your shell startup script (such as ~/.bashrc):

if ! pgrep -u "$USER" ssh-agent > /dev/null; then
    ssh-agent > "$XDG_RUNTIME_DIR/ssh-agent.env"
fi
if [[ ! "$SSH_AUTH_SOCK" ]]; then
    eval "$(<"$XDG_RUNTIME_DIR/ssh-agent.env")"
fi

I have also struggled to get the ssh-agent going via the (user's) systemd service, the relevant section of the ssh-agent part of the ssh-keys article on the Arch Wiki might be of help. The above method worked like a charm so I never had any reason to look for an alternative.

Hope this can be of help to you.

1
  • you could just source the .env file. No need to use eval and a command substitution. However, you may also want to check whether $XDG_RUNTIME_DIR is set to something valid, and then handle the case when it is not (as when logging in from a remote system using ssh?)
    – Kusalananda
    Apr 2, 2020 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .