5 ms·
With regards to using ssh-agent, this is what I have in my .bash_profile: export SSH_ENV=$HOME/.ssh/environment_${HOSTNAME} function start_agent {
by ansible 7y ago
With regards to using ssh-agent, this is what I have in my .bash_profile:
export SSH_ENV=$HOME/.ssh/environment_${HOSTNAME}
function start_agent {
echo -n "Initialising new SSH agent... "
touch ${SSH_ENV}
chmod 600 ${SSH_ENV}
/usr/bin/ssh-agent -s | sed 's/^echo/#echo/' > ${SSH_ENV}
echo "succeeded."
. ${SSH_ENV} > /dev/null
}
# Don't use Gnome keyring for SSH
if [[ $SSH_AUTH_SOCK =~ /run/user ]]; then
unset SSH_AUTH_SOCK
unset SSH_AGENT_PID
fi
# Source SSH settings, if applicable
ssh-add -l > /dev/null 2>&1
if [ $? -eq 0 -o $? -eq 1 ]; then
echo "SSH Agent found."
else
echo -n "Looking for SSH Agent... "
if [ -f "${SSH_ENV}" ]; then
. ${SSH_ENV} > /dev/null
ssh-add -l > /dev/null 2>&1
if [ $? -eq 0 -o $? -eq 1 ]; then
echo "found pid: ${SSH_AGENT_PID}."
else
echo "not found."
start_agent;
fi
else
start_agent;
fi
ssh-add -l
fi
This starts up a new ssh-agent if it can't find one, but uses the same agent if it is already running on this machine (I remote into a bunch of systems). So even if I use a bunch of screen sessions, or remote in to a machine multiple times, it will always try to use the same agent.
- jpeeler 7y agoHave you considered using https://github.com/funtoo/keychain https://github.com/funtoo/keychain? Edit: I see it's been mentioned elsewhere here.
- ansible 7y agoWell, that's interesting. And a whole bunch of code. From a quick skim, it is basically solving the same problem in a different style. I didn't want or need to wrap ssh-agent. I'm fine with using it and ssh-add by themselves. I just wanted to automatically find and re-use an existing agent rather than spawning a new one.
- Spivak 7y agoWhy not use the GNOME keyring as your SSH agent? It's a full agent that you don't really need to think about.
- ansible 7y agoI don't run a GUI (or even have the necessary libraries installed) on some of the remote systems I access. Sometimes I'm the one remote, logging back into my workstation. I've previously had Gnome keyring helpfully pop up a window so that I can unlock a SSH key... meanwhile I'm just sitting there (SSH'ed into my workstation) wondering why my outbound SSH command is just hanging.