0

I'm working a script that create user and add a key for that user so he can use that key with his username to SSH into my VM

Ex. user = john

useradd -m john &&
cd ~/.ssh/ && 
rm -rf tmp_rsa* &&
ssh-keygen -t rsa -b 4096 -C "john@email.com" -N '' -f john_rsa &&
echo "#tmp_rsa" >> authorized_keys &&
cat john_rsa.pub >> authorized_keys &&
cat authorized_keys &&
service ssh restart &&
echo ">>> Done"

Is the above script is correct to acheive what I want ?

I tried connect with UN : john, and the key generated john_rsa.

I got

enter image description here


Thanks to @marosg and @Takkat

Here is my updated script

adduser -m john &&
cd ~/.ssh/ && 
rm -rf tmp_rsa* &&
ssh-keygen -t rsa -b 4096 -C "john@email.com" -N '' -f john_rsa &&
echo "#tmp_rsa" >> ~john/.ssh/authorized_keys &&
cat john_rsa.pub >> ~john/.ssh/authorized_keys &&
cat authorized_keys &&
echo ">>> Done"
6
  • The command useradd will not create a home directory for john, but adduser will. See askubuntu.com/questions/139304/… – Takkat Jan 23 '19 at 19:39
  • @Takkat what about the -m ? – cyb3rZ Jan 23 '19 at 19:45
  • @Takkat : What you think of my updated script ? – cyb3rZ Jan 23 '19 at 19:47
  • There is no option -m in Ubuntu. The home directory will be created by default in /home/ - after that it appears you may want to create your keys as user john. – Takkat Jan 23 '19 at 19:55
  • So this should do right ? adduser -m john – cyb3rZ Jan 23 '19 at 20:20
3

There are couple of things wrong here:

  • you create user john and then you do nothing with this user any more

  • You are adding keys to YOUR user

  • remote user who needs to login here needs the private key from ssh keypair on the machine from which he is connecting

  • there is no need to restart ssh service after adding keys

What you need on client side

  • user generates ssh keypair and provides you public key of this keypair (ssh-keygen ...; cat id_rsa.pub)

What you need on server side is

  • add user john

  • add the public key provided by user to ~john/.ssh/authorized_keys (echo id_rsa.pub_provided_by_remote_user >> ~john/.ssh/authorized_keys)

4
  • I want to do the whole things in 1 script, can you help me adjust what I got. I can update what I got now base on your suggestion, and update my post. Is it ok ? – cyb3rZ Jan 23 '19 at 19:44
  • What you think of my updated script ? – cyb3rZ Jan 23 '19 at 19:47
  • If you and that other user are two different humans, it is not possible to do this in one script securely. 1 . User John creates keypair on his machine, there is private key, which is secret and he keeps it on his machine. There is public key, which he sends you as a text 2. On server you create user john and then put that public key to his ~john/.ssh/authorized_keys Above you use cd ~/.ssh/ which means you are working in YOUR .ssh directory, you need to use ~john/.ssh – marosg Jan 24 '19 at 6:19
  • If user John has Launchpad account then you can use ssh-import and he does not need to send you his public key but I think this would be too complicated to your setup. – marosg Jan 24 '19 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.