Skip to content

Commit 1eb574e

Browse files
ssh: fixed ssh-agent creation
1 parent 20d3f92 commit 1eb574e

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

  • .oh-my-shell/shellrc/plugins/ssh

.oh-my-shell/shellrc/plugins/ssh/post.sh

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
# src: https://unix.stackexchange.com/a/217223/45954
22
# src: https://unix.stackexchange.com/a/90869/45954
33

4-
# Add "AddKeysToAgent yes" to hosts (stored in your ~/.ssh/config) whose keys should be kept by the ssh-agent, when first unlocked
5-
# Don't use: ssh-add -l > /dev/null || ssh-add
4+
# Overview:
5+
# 0. ssh client configured to store keys into a ssh-agent
6+
# Add "AddKeysToAgent yes" to hosts (stored in your ~/.ssh/config) whose keys should be kept by the ssh-agent, when first unlocked
7+
# Don't use this in scripts as it would load all keys: ssh-add -l > /dev/null || ssh-add
8+
# 1. ssh client needs a ssh private key
9+
# 2. ssh client store ssh private key into ssh-agent
10+
# - this script helps store the private key into an ssh-agent you configured
611

712
# Create ssh-agent setup script
813
SSH_AGENT_SETUP_SCRIPT="/tmp/$USER.ssh_agent_setup.sh"
914

1015
cat <<EOF >$SSH_AGENT_SETUP_SCRIPT
1116
USER_SSH_AGENT_ENV="\$1"
17+
USER_SSH_AGENT_ENV_NAME="\$(basename \$1)"
1218
1319
mkdir -p "\$(dirname \$USER_SSH_AGENT_ENV)"
1420
1521
# Try to load current ssh-agent
1622
if test -f \$USER_SSH_AGENT_ENV; then
17-
source \$USER_SSH_AGENT_ENV
23+
source \$USER_SSH_AGENT_ENV
1824
else
19-
SSH_AGENT_PID='missing'
25+
SSH_AGENT_PID='missing'
2026
fi
2127
28+
# Find ssh-agent process
2229
agentPids="\$(pgrep -u $USER 'ssh-agent')"
2330
if echo "\$agentPids" | grep -q "\$SSH_AGENT_PID"; then
2431
# Configured agent is also a running agent
@@ -27,7 +34,15 @@ cat <<EOF >$SSH_AGENT_SETUP_SCRIPT
2734
2835
# Configured agent is gone. Recreating a new agent"
2936
# Start new ssh-agent
30-
eval \$(ssh-agent) >/dev/null
37+
# SSH_USE_STRONG_RNG might block on computer lacking enough entropy
38+
# (such as device without a hardware random generator)
39+
export SSH_USE_STRONG_RNG=1
40+
41+
if test "\$USER_SSH_AGENT_ENV_NAME" = "default"; then
42+
eval \$(ssh-agent -t 3600) >/dev/null
43+
else
44+
eval \$(ssh-agent) >/dev/null
45+
fi
3146
3247
# Remove eventual previous ssh-agent environment configuration
3348
command rm -f "\$USER_SSH_AGENT_ENV" 2>/dev/null
@@ -41,11 +56,14 @@ EOF
4156
# Create a single ssh-agent instance across all terminal sessions, for each predefined agent
4257
# src: https://stackoverflow.com/a/32592488/219728
4358

44-
USER_SSH_AGENT_DIR="$HOME/.ssh/.agent"
45-
USER_SSH_AGENT_ENV_0="$USER_SSH_AGENT_DIR/env_default"
46-
USER_SSH_AGENT_ENV_1="$USER_SSH_AGENT_DIR/env_unsecure"
59+
USER_SSH_AGENT_DIR="$HOME/.ssh/agent"
60+
USER_SSH_AGENT_ENV_0="$USER_SSH_AGENT_DIR/env/default"
61+
USER_SSH_AGENT_ENV_1="$USER_SSH_AGENT_DIR/env/unsecure"
4762

4863
for f in "$USER_SSH_AGENT_ENV_0" "$USER_SSH_AGENT_ENV_1"; do
64+
# Create missing folders
65+
mkdir -p "$(dirname $f)"
66+
4967
# Run ssh agent setup script
5068
flock --exclusive --unlock \
5169
"${f}.lock" \
@@ -62,3 +80,5 @@ for f in "$USER_SSH_AGENT_ENV_0" "$USER_SSH_AGENT_ENV_1"; do
6280
. "$f"
6381
fi
6482
done
83+
84+
rm -f "$SSH_AGENT_SETUP_SCRIPT"

0 commit comments

Comments
 (0)