Copying SSH Key From Root To Another User On Same Machine

7,491

This is just one of a few different ways to do this. I am assuming you've just created your user and it has sudo access.

  1. Create the folder if it doesn't already exist:

    mkdir /home/$USER/.ssh

  2. Make the directory only executable by the user:

    chmod 700 /home/$USER/.ssh

  3. Copy the authorized_keys file that contains your public key:

    sudo cp /root/.ssh/authorized_keys /home/$USER/.ssh/authorized_keys

  4. Make everything in .ssh owned by your user:

    sudo chown -R $USER:$USER /home/$USER/.ssh

  5. Make it readable only by your user:

    sudo chmod 600 /home/$USER/.ssh/authorized_keys

If your user does NOT have sudo access, you can modify this workflow a bit. Using your user name instead of <user>, run these as root:

mkdir /home/<user>/.ssh
chmod 700 /home/<user>/.ssh
cp /root/.ssh/authorized_keys /home/<user>/.ssh/authorized_keys
chmod 600 /home/<user>/.ssh/authorized_keys
chown -R <user>:<user> /home/<user>/.ssh
Share:
7,491

Related videos on Youtube

Doobie
Author by

Doobie

Updated on September 18, 2022

Comments

  • Doobie
    Doobie over 1 year

    I am trying to create a server setup script. On initialization of the server, I add my ssh key which then gives me access to the root user via ssh. I now have created another user and gave them sudo access. Now I am trying to figure out how I can copy my SSH key from root user to the new user I just created. Both users are on the same machine and one is root and the other is a sudo user.

  • Doobie
    Doobie about 4 years
    Awesome, thank you very much it worked like charm. Thank you for your time and response!
  • Kurankat
    Kurankat about 4 years
    Great, glad to be able to help
  • Mike Ciffone
    Mike Ciffone over 2 years
    Go through this all the time when creating DO droplets
  • Admin
    Admin almost 2 years
    thanks for the note on how to do it for non-sudo users (: btw, chmod 600 = u+rw (or user read/write) and 700 = u+rwx (read/write/execute) permissions. newer OSes support eg chmod u+rw and I find it more readable and easier to use than calculating the 7xx values (: