- Create a folder at the root of your user home folder
(Example:
C:/Users/uname/
) called.ssh
. - Create the following files if they do not already
exist (paths begin from the root of your user home
folder):
.ssh/config
.bash_profile
.bashrc
Follow the steps in the section named "Generating a new SSH Key" found in the following documentation from GitHub: Generating a new SSH key and adding it to the ssh-agent
Add the following text to .ssh/config
(.ssh
should be found
in the root of your user home folder):
Host github.com
Hostname github.com
IdentityFile ~/.ssh/id_ed25519
First, ensure that following lines are added to .bash_profile
,
which should be found in your root user home folder:
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
Now, add the following text to .bashrc
, which should be found
in your root user home folder:
# Start SSH Agent
#----------------------------
SSH_ENV="$HOME/.ssh/environment"
function run_ssh_env {
. "${SSH_ENV}" > /dev/null
}
function start_ssh_agent {
echo "Initializing new SSH agent..."
ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo "succeeded"
chmod 600 "${SSH_ENV}"
run_ssh_env;
ssh-add ~/.ssh/id_ed25519;
}
if [ -f "${SSH_ENV}" ]; then
run_ssh_env;
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_ssh_agent;
}
else
start_ssh_agent;
fi
If you want to add multiple keys to the agent simply replace the line
ssh-add ~/.ssh/id_rsa;
with the following block:This will add every ssh key that start with
id_
The inconvenience is that when the ssh-agent start, it's going to prompt you to enter the password for every keys even if you don't necessarily use them. I haven't found a solution for this.
You might want to use that with git for windows if, say, you have a different github account: 1 for your personal work and another for your professional work. Github won't let you re-use the same ssh key for both accounts so you need 2 keys.
To make this work, you will need to do 2 more steps. in
${HOME}/.ssh
create theconfig
file with the following content:In this example, we have the secondary ssh key named
id_rsa_corpo
.If you already cloned the repo for the secondary key, change the remote to point to
github-corpo
.If not, then just clone by replacing the host :
git@github-corpo:myUserName/myProject.git