2

I tried to set up a ssh key for easily logging into the server which is hosting my website : ssh-keygen -t dsa -b 1024. The keys (public and private) have been saved in /var/root/.ssh which I dont like because I have to sudo -s prior to rsync for accessing the private key.

Is there any easy and safe way to move the ssh folder from /var/root/.ssh to my user folder, so that I can use the keys even if not root ? what location to move it ? what is the command for newbie to be used for the relocation ? (for mac os 10.10) Thank you.

2

If you've simply created a DSA key as root, it would be safest to do the following (from a terminal as your own user):

mkdir -p ~/.ssh
sudo cp -av /var/root/.ssh/id_dsa /var/root/.ssh/id_dsa.pub /Users/$USER/.ssh/
chown -v $USER: ~/.ssh/id_dsa*
chmod -v 600 ~/.ssh/id_dsa

This will create a .ssh configuration directory in your homedir (mkdir -p won't make loud noises if ~.ssh already exists, and won't do anything), copy the public and private key files from root's homedir to your own, then set the correct permissions and ownership on your files.

This is non-destructive (you're not moving anything, you're just copying); once you're happy the key-pair is letting you connect to your remote host successfully as your normal login user, you can sudo rm -fv /var/root/.ssh/id_dsa /var/root/.ssh/id_dsa.pub This also doesn't affect any other files in either account's .ssh folder.

2

Open Terminal while logged in as your usual user, then run

mkdir ~/.ssh
sudo sh -c 'mv /var/root/.ssh/* /Users/'$USER'/.ssh/'
sudo chown $USER ~/.ssh/*
sudo chgrp $(groups | cut -d \  -f 1) ~/.ssh/*
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*

This assumes that you haven't created any ssh key pairs as your usual used (and therefore don't already have files in ~/.ssh).

PS: Some explanations:

  • First sudo moves the content of root's .ssh folder into the one of the current user. Needs to be done by calling a subshell (sh -c) because the wildcard part (/var/root/.ssh/*) only works after the sudo has changed the current user to root. $USER is an environment variable containing the name of the current user
  • Third sudo changes group ownership of the copied files to the primary group of the current user. As there is no convenient environment variable for this, the output of groups (which lists all groups a user belongs to) needs to used. Please note the two space characters after the \.
  • I understand the plan, Patrix, yet I can't get line number 2 working : when logged as xxx:~gilles$ it asks me my password, and fails : No such file or directory. Remark, if I write sudo -s, prompt is : bash-3.2 # and then I can list (ls) /var/root/.ssh – Gilles Sep 6 '15 at 14:28
  • Ah, bugger. Edited, please try again. – nohillside Sep 6 '15 at 15:43
  • It looks like it is successfull post. In my first attempt in copying your commands, I failed because I did not take care at the syntax (cut -d \ -f 1) where there are 2 needed blank spaces. I will test it tomorrow and try to understand the basics ;). – Gilles Sep 6 '15 at 17:15
  • I have used this solution, yet following (untested) neuro's answer seems safer, at least for blind newbie. – Gilles Sep 8 '15 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .