skip to content
 

Users of key-based ssh authentication often find it useful to set up an SSH agent to securely manage their ssh keys. It can therefore be convenient to have an ssh agent automatically start and load keys when one starts a login session. To help acheive this, you can add the following code to your .bashrc file (N.B. some of the lines may wrap in your web browser so you may find it better to copy/paste the following into a local text editor):

 

# Define a variable to store agent-related environment variables
SSH_ENV="$HOME/.ssh/environment"

# function to start a new ssh agent and add keys 
function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
     chmod 600 "${SSH_ENV}"
     . "${SSH_ENV}" > /dev/null
     # See man ssh-add for the list of default keys that ssh-add will add to the running agent.
     /usr/bin/ssh-add
     # An example of adding a non-default key
     # /usr/bin/ssh-add /home/alt36/.ssh/id_rsa_example
}

if [ -f "${SSH_ENV}" ]; then
     . "${SSH_ENV}" > /dev/null
     # check the agent PID is actually running, and start if not
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || start_agent
else
     start_agent
fi

System status 

System monitoring page

Can't find what you're looking for?

Then you might find our A-Z site index useful. Or, you can search the site using the box at the top of the page, or by clicking here.