fix(ssh-agent): add honor-existing to reuse a running agent (#14049)

This commit is contained in:
Robby Russell
2026-09-10 13:47:34 +02:00
committed by GitHub
parent d5fa35e453
commit ee2acd0e44
2 changed files with 29 additions and 5 deletions
+12
View File
@@ -21,6 +21,18 @@ To enable **agent forwarding support** add the following to your zshrc file:
zstyle :omz:plugins:ssh-agent agent-forwarding yes
```
### `honor-existing`
If your session already provides an ssh-agent — a desktop keyring, `gpg-agent`,
KeePassXC, or an agent forwarded over SSH — the plugin normally ignores it and
starts one of its own, which leaves you with two agents. Set this to reuse the
running agent instead, and only start a new one when `$SSH_AUTH_SOCK` is empty
or does not answer:
```zsh
zstyle :omz:plugins:ssh-agent honor-existing yes
```
### `helper`
To set an **external helper** to ask for the passwords and possibly store
+17 -5
View File
@@ -1,16 +1,28 @@
# Get the filename to store/lookup the environment from
ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
# Test if $SSH_AUTH_SOCK points at a listening agent
function _is_agent_running() {
local REPLY
[[ -n "$SSH_AUTH_SOCK" && -S "$SSH_AUTH_SOCK" ]] || return 1
zmodload zsh/net/socket 2>/dev/null || return 1
zsocket "$SSH_AUTH_SOCK" 2>/dev/null || return 1
exec {REPLY}>&- # close the probe connection
return 0
}
function _start_agent() {
# Reuse an agent the session already provides, if enabled
if zstyle -t :omz:plugins:ssh-agent honor-existing && _is_agent_running; then
return 0
fi
# Check if ssh-agent is already running
if [[ -f "$ssh_env_cache" ]]; then
. "$ssh_env_cache" > /dev/null
# Test if $SSH_AUTH_SOCK is visible
zmodload zsh/net/socket
if [[ -S "$SSH_AUTH_SOCK" ]] && zsocket "$SSH_AUTH_SOCK" 2>/dev/null; then
return 0
fi
_is_agent_running && return 0
fi
if [[ ! -d "$HOME/.ssh" ]]; then
@@ -124,4 +136,4 @@ if ! zstyle -t :omz:plugins:ssh-agent lazy; then
fi
unset agent_forwarding ssh_env_cache
unfunction _start_agent _add_identities
unfunction _start_agent _add_identities _is_agent_running