Reusing a private SSH key on another Mac

I recently had to access a Git repo which was hosted on Bitbucket on another workstation. Rather than create a new private SSH key, I did the following instead:

  1. Create the ~/.ssh directory (it’s not there by default on a Mac so don’t worry!)
  2. Transfer the SSH key to the other machine (I emailed it which is fine)
  3. Copy the SSH key to the directory and set the permissions
  4. Register the key to the SSH system

At this point, I could git clone the repo and continue development on that workstation. Job done!

You can run this script to do the same, just ensure the private SSH key filename and location is correct (mine was in ~/Downloads/id_rsa) so adjust the first two lines then just copy and paste into a Terminal session:

# the private SSH key we want to install - change these to your private key file location and filename
SSH_PRIVATE_KEY_FILENAME=id_rsa
SSH_PRIVATE_KEY_DIRECTORY=~/Downloads

# create the directory if it doesn't exist
SSH_DIR=~/.ssh && [ ! -d $SSH_DIR ] && mkdir $SSH_DIR

# copy across the private SSH key file
cp $SSH_PRIVATE_KEY_DIR/$SSH_PRIVATE_KEY_FILENAME $SSH_DIR

# set restricted permissions
chmod 600 $SSH_DIR/$SSH_PRIVATE_KEY_FILENAME

# add the key
ssh-add $SSH_DIR/$SSH_PRIVATE_KEY_FILENAME

Then enter the passphrase for the private key when prompted.

Enjoy! 😉

Top