A tiny hook tool for bash/zsh to re-authenticate with ssh-agent.
This small command manages sockets for SSH authentication created by
ssh-agent. It automatically detects the valid socket and rewrite the symlink
to avoid disconnecting.
ssh-agent stores the path for sockets into $SSH_AUTH_SOCK. It is a path
below, for instance.
# example in macOS
$ echo $SSH_AUTH_SOCK
/private/tmp/com.apple.launchd.sa197Z7kVN/ListenersThis changes every time you login and ssh-agent detects the changes and makes
$SSH_AUTH_SOCK indicate validly. Usually that's enough.
But when you use terminal multiplexers -- tmux, screen, or so --, it breaks
this.
- Login your Mac.
$SSH_AUTH_SOCKis/some/path/to/Listeners.
- SSH into a Linux box.
$SSH_AUTH_SOCKwill be changed into/tmp/path/to/agent.foo.
- Launch
tmuxin the box. Also you can use SSH authentication intmuxbyssh-agent. - Detach
tmuxand logout the box. - SSH into the box again.
$SSH_AUTH_SOCKwill be changed into/tmp/path/to/agent.bar.
- Attach the existent
tmuxsession. $SSH_AUTH_SOCKwill be/tmp/path/to/ssh-agent.foo, not...bar. You can NOT use SSH authentcation in it.
Here is gosshauth. You can install this from release page or a command
below.
go get github.com/delphinus/gosshauthAnd you should set the hook for zsh/bash.
# for bash
if which gosshauth > /dev/null 2>&1; then
eval "$(gosshauth hook bash)"
fi# for zsh
if (( $+commands[gosshauth] )); then
eval "$(gosshauth hook zsh)"
fi# for fish
if type -q gosshauth
gosshauth hook fish | source
endNow you can use SSH authentication even if in the way above.
- Check
$SSH_AUTH_SOCK. gosshauthchecks the existence.
- If exists, that's all, done.
gosshauthglobs all socket-like files for/tmp/**/Listenersand/tmp/ssh*/agent.*.- The candidate that has the latest timestamp is the goal, maybe.
gosshauthrewrite the~/.ssh/auth_socksymlink to target the goal, and set$SSH_AUTH_SOCKto use it.
JINNOUCHI Yasushi <me@delphinus.dev>
The MIT License