Configure a single ssh-agent and gpg-agent and use them everywhere

Using this recipe you’ll get a single ssh-agent and a single gpg-agent running for your user and the correct environmental variables will be set everywhere. Just add this code to your ~/.bashrc:

# GPG Agent
if [ -x $(which gpg-agent) ] && [ -d "${HOME}/.gnupg" ]; then
    oldumask=$(umask)
    umask 0077
    pgrep -U $LOGNAME gpg-agent >/dev/null 2>&1 || gpg-agent --daemon --write-env-file "${HOME}/.gnupg/gpg-agent-info" >/dev/null 2>&1
    umask $oldumask
    GPG_TTY=$(tty)
    export GPG_TTY
    if [ -r "${HOME}/.gnupg/gpg-agent-info" ]; then
       . "${HOME}/.gnupg/gpg-agent-info"
       export GPG_AGENT_INFO
    fi
fi

# SSH Agent
if [ -x $(which ssh-agent) ] && [ -d "${HOME}/.ssh" ]; then
    pgrep -U $LOGNAME ssh-agent >/dev/null 2>&1 || ssh-agent | sed 's/^echo/#echo/' > "${HOME}/.ssh/environment"
    if [ -r "${HOME}/.ssh/environment" ]; then
        . "${HOME}/.ssh/environment"
    fi
fi
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s