Ssh agent needs restart for every terminal window

I have two ssh keys on my computer. Both have a password. Unfortunately, even though I set up a config file, I get asked for the password every single time I do anything with it.

➜  .ssh ls -lh
total 28K
-rw-r--r-- 1 lukas lukas  253 29. Mai 11:11 config
-rw------- 1 lukas lukas 3,4K 29. Mai 10:52 id_rsa_github_privat
-rw-r--r-- 1 lukas lukas  743 29. Mai 10:52 id_rsa_github_privat.pub
-rw------- 1 lukas lukas 3,4K 29. Mai 10:52 id_rsa_github_work
-rw-r--r-- 1 lukas lukas  751 29. Mai 10:52 id_rsa_github_work.pub
-rw------- 1 lukas lukas  828 29. Mai 11:04 known_hosts
-rw-r--r-- 1 lukas lukas   92 29. Mai 11:04 known_hosts.old
# Personal account
Host privat
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github_privat
  AddKeysToAgent yes

# Work account
Host work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github_work
  AddKeysToAgent yes

I get asked for the password every time, which seems to be a problem with the ssh agent. If I run this:

➜  ~ ssh-add -l
Could not open a connection to your authentication agent.

I get this message and I have to restart it:

➜  ~ eval "$(ssh-agent -s)"                       

Agent pid 206575
➜  ~ ssh-add -l
The agent has no identities.

Then of course I have to add the keys again.

It forgets everything every time I open a new terminal window.
I can add all of this junk to my zshrc, but then I would need to type in two passwords very time I open a terminal

Does anybody have a solution for this?

I have seen a couple of similar topics here and on stack overflow, but nothing worked here.

Maybe this is a bug with the gnome keychain? I dont know

I have never had the need - but if I develop a need - this would be an interesting read

https://www.baeldung.com/linux/ssh-agent-systemd-unit-configure

1 Like

There’s no real indication that you have actually set up the ssh agent properly.

Have you added this to your .zshrc?

if ! pgrep -u "$USER" ssh-agent > /dev/null; then
    ssh-agent -t 1h > "$XDG_RUNTIME_DIR/ssh-agent.env"
fi
if [ ! -f "$SSH_AUTH_SOCK" ]; then
    source "$XDG_RUNTIME_DIR/ssh-agent.env" >/dev/null
fi

Or configured and enabled the systemd service?

https://wiki.archlinux.org/title/SSH_keys#SSH_agents

4 Likes

Keychain [1] [2]

[1] Arch Linux - Package Search

[2] SSH keys - ArchWiki

1 Like