.bash_logout

27th February 2017 at 3:04pm
Bash Security TechnicalNotes

Useful when used in conjunction with ssh-add and ReusingSSHAgents.

If you use ssh-agent, then a useful addition to your .bash_logout file may well be something like this:

# When an ssh-agent is active, kill it if this is your only login.
if [[ -n "$SSH_AGENT_PID" ]] ;  then
    TTY="${TTY:-$(/usr/bin/tty)}"
    _killagent=1
    while read -r _user _tty _ ; do
       if [[ "$_user" = "$USER" && ! "$_tty" = "${TTY#/dev/}" ]] ; then
           unset _killagent
           break
       fi
    done < <(who)
    if [[ -n "${_killagent:-}" ]] ; then
        $(ssh-agent -k)
    fi
fi