Configure SSH authentication for users

When you create a Linux VM/EC2 instance on cloud, you get a choice to either create a user ID password or use SSH authentication.

While creating the VM its is quite straight forward. You take the “Public Key phrase” from the key generator and paste it in the space provided. You will be allowed to login with this key.

Below steps help you in adding additional users to the VM once created.

Login to the Linux machine with root privileges.

Obtain the public key file (generated via Puttygen or ssh-keygen) from the end user and copy it on the server where it has to be added.

Create the required user using below command. There is no dependency between user name and the key files.

[root@testsshserver]# useradd -m <user_name> -s /bin/bash

Create the directory path required for SSH keys

[root@testsshserver]# cd /home/<user_name>/

[root@testsshserver]# mkdir .ssh

[root@testsshserver]# cd .ssh

[root@testsshserver]# chmod 700 .ssh

[root@testsshserver]# cd .ssh

Create the key file

[root@testsshserver]# touch authorized_keys

[root@testsshserver]# chmod 644 authorized_keys

Copy the public key file content into the “authorized_keys file”

[root@testsshserver]# cat <path to public file>/<pubilc key file> >> authorized_keys

[root@testsshserver]# chown <username> authorized_keys

Edit the file to have the correct format. Normally when you generate the file from puttygen, the content starts with “—- BEGIN SSH2 “.

Make sure adjust the content to look like below:

ssh-rsa AAAAB3fafsdal;fasd;lkjfasd;lkjfadslkjfadskjfsdkjlfdaskjsfdkjldsfakjdfffffkfdkdbgJscZiA
Bula+oErKgpM3EQRjlBNktNj53If+3bGxo1alzZoaAt2Jer5co3+q7WYIdoEVplvl7B5qTkgmoJ2BoSu
XLXiKWwuDL9ZjWtI40NAiCYZ6xlB/Ts54GZVi653CZ1UnTW0I5bm2f+n5drDZKJE091yuzoPwAUCA+b4
Buljfk,kadfhsdafsad,fkfdDKKDKfasJLGD,fdalkfdsakl;jkjlk;lkjkl;;krtKNVDWE>KK<moJ2BoSu
+FH0r6ByNzJhf7d33LpQnK/i/bEoWmLKrjzETbgNUjm0k+YNBeDBpdCtGw==

Now you should be able to ssh to the server using the private key file and the passphrase if used.

 

 

 

Leave a comment