50

I have a fresh install of Ubuntu 16.04 on my machine. Now I want to use my existing ssh key on my machine, so that I can use GitHub with my previous activities.

How do I set this up?

amc
  • 7,022
  • 7
  • 39
  • 51
smehsan
  • 728
  • 1
  • 8
  • 13

2 Answers2

67

If you have a copy of your ssh keys (e.g., on a USB stick) then simply copy the key files to the ~/.ssh/ directory.

E.g.,

cp /path/to/my/key/id_rsa ~/.ssh/id_rsa
cp /path/to/my/key/id_rsa.pub ~/.ssh/id_rsa.pub
# change permissions on file
sudo chmod 600 ~/.ssh/id_rsa
sudo chmod 600 ~/.ssh/id_rsa.pub
# start the ssh-agent in the background
eval $(ssh-agent -s)
# make ssh agent to actually use copied key
ssh-add ~/.ssh/id_rsa

Otherwise, you will need to create a new one and add it to your GitHub account https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/. Be sure to remove the old key from GitHub while you're at it.

amc
  • 7,022
  • 7
  • 39
  • 51
  • Thank You @amc How can i copy from github to my machine ? – smehsan Jun 14 '16 at 17:40
  • If you don't already have a copy of the key, then you will need to create a new one and add it to your GitHub account https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/ – amc Jun 14 '16 at 17:41
  • So, i have to create a new SSH key and add that to github, isn't it. and i have to delete the previous SSH key from github, i had created earlier. – smehsan Jun 14 '16 at 17:43
  • yes, it sounds like you don't have a copy of you key files so you just need to make new ones and remove the old key. – amc Jun 14 '16 at 17:44
  • 1
    Just dropping a short note to mention that the permissions on both files need to be set to 600 eg. -rw------- example: chmod 600 id_rsa* The default file permissions for copy pasting them there won't work. – Strixy Jul 01 '18 at 21:37
  • The perms on the public key are not as critical as the private key - the system will permit the public key to be world-readable while it will not permit the private key to be, – Thomas Ward Nov 15 '18 at 17:41
8

Step 1: Give permission to ssh folder

chmod 700 ~/.ssh

Step 2: Give permission to ssh key files

chmod 600 ~/.ssh/id_rsa

chmod 644 ~/.ssh/id_rsa.pub

Step 3: Run the below command on the client machine, that will add the SSH key to the agent.

ssh-add

Now you can confirm with ssh-add -l (again on the client) that it was indeed added.

Priyanka
  • 81
  • 1
  • 2