Skip to content

Instantly share code, notes, and snippets.

@tstellanova
Created September 7, 2014 15:54
Show Gist options
  • Save tstellanova/76ee01c1599d9a9433cf to your computer and use it in GitHub Desktop.
Save tstellanova/76ee01c1599d9a9433cf to your computer and use it in GitHub Desktop.
Start ssh-agent at startup on ubuntu
# typically you'd add this at the end of eg .bashrc
# setup ssh-agent
SSH_ENV=$HOME/.ssh/environment
# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add
}
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
@tstellanova
Copy link
Author

Use this in your .bashrc instead:

/usr/bin/keychain --lockwait 0 --clear $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOSTNAME-sh

Ensure that you have keychain installed

@maivdigital
Copy link

maivdigital commented Jan 5, 2021

This one works for me on Win 10 machine.
Inside of .bashrc insert this code

#!/bin/bash
eval `ssh-agent -s`
# Add SSH key
ssh-add ~/.ssh/id_rsa

@BtbN
Copy link

BtbN commented Jan 6, 2021

That will start a new agent for every single shell.
It will also never clean it up.

@pdhixenbaugh
Copy link

setup ssh-agent

SSH_ENV=$HOME/.ssh/environment

Do I need to have a ~/.ssh/environment file for this to work? I am on a cloud-based IDE called "Cloud 9" using ubuntu 20.04 (ide.cs50.io) and it doesn't have this file, but it does have /etc/environment if that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment