浏览代码

Add tmux ssh agent support with automated clearing some delay after the last detach

Taddeus Kroes 2 周之前
父节点
当前提交
abcf7c0be3
共有 5 个文件被更改,包括 35 次插入 和 0 次删除
  1. 10 0
      bashrc
  2. 6 0
      install.sh
  3. 10 0
      scripts/tmux-clear-ssh-agent.sh
  4. 2 0
      tmux.conf
  5. 7 0
      tmux.ssh-agent.conf

+ 10 - 0
bashrc

@@ -84,6 +84,7 @@ fi
 # ls shortcuts
 alias l='ls -F'
 alias ll='ls -AlhF'
+alias lla='ls -alhF'
 
 # ssh shortcuts for multivac
 alias mvv='mosh mv -p 8051 -- screen -Uxr'
@@ -97,6 +98,7 @@ alias json='python -mjson.tool'
 alias g='grep'
 alias h='help'
 alias dc='docker compose'
+alias pc='podman compose'
 
 # git shortcuts
 alias gs='git status'
@@ -151,6 +153,14 @@ export EDITOR=vim
 
 export PATH=$HOME/.local/bin:$PATH:/sbin:/usr/sbin
 
+#
+# SSH agent
+#
+
+if [ -S "$XDG_RUNTIME_DIR/openssh_agent" ]; then
+    export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/openssh_agent"
+fi
+
 #
 # local, installation-specific config
 #

+ 6 - 0
install.sh

@@ -1,6 +1,9 @@
 #!/usr/bin/env bash
 set -euo pipefail
 
+INSTALL_ALL=0
+if [ "$1" = "-a" ]; then INSTALL_ALL=1 fi
+
 DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 
 if ! which git >/dev/null; then
@@ -17,6 +20,9 @@ ln -sf $DIR/bashrc ~/.bashrc
 
 # tmux
 ln -sf $DIR/tmux.conf ~/.tmux.conf
+if [ $INSTALL_ALL -eq 1 ]; then
+    ln -sf $DIR/tmux.ssh-agent.conf ~/.tmux.ssh-agent.conf
+fi
 
 # vim
 if which vim >/dev/null; then

+ 10 - 0
scripts/tmux-clear-ssh-agent.sh

@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+# Wipe the keys after a specified delay, unless there are still tmux clients
+# attached.
+set -e
+#exec > >(tee -a /tmp/tmux-timer.log) 2>&1
+sleep "${1-0}"
+if [ $(tmux list-clients | wc -l) -eq 0 ]; then
+    export SSH_AUTH_SOCK="/run/user/$(id -u)/openssh_agent"
+    exec ssh-add -D
+fi

+ 2 - 0
tmux.conf

@@ -25,3 +25,5 @@ bind h select-pane -L
 bind j select-pane -D
 bind k select-pane -U
 bind l select-pane -R
+
+source-file -q ~/.tmux.ssh-agent.conf

+ 7 - 0
tmux.ssh-agent.conf

@@ -0,0 +1,7 @@
+# clear SSH agent shortly after detaching, unless reattached in the meantime
+set-hook -g client-detached 'run-shell "~/config/scripts/tmux-clear-ssh-agent.sh 180 &"'
+set-hook -g session-closed 'run-shell "~/config/scripts/tmux-clear-ssh-agent.sh 60 &"'
+set-hook -g client-attached 'run-shell "pkill -f tmux-clear-ssh-agent.sh || true"'
+
+# ensure the SSH_AUTH_SOCK is updated when re-attaching
+set -g update-environment "SSH_AUTH_SOCK"