Friday, August 7, 2015

Creating/Adding new user in amazon web services (AWS)

Software used
Local OS
Windows 7
Remote Host
Amazon web services
Terminal
Mingw32 included in (PortableGit-1.9.5)
Other useful tools
Putty, WinSCP






Creating User
First log in to the amazon aws using your ec2-user account

Now, create new user by using follwing commands,
[ec2-user@amazon ~]$sudo adduser username

Set password for “username” by:
[ec2-user@amazon ~]$ sudo su username
[root@amazon ec2-user]$ passwd username

Now go to home/username folder and type following command
[username@amazon ~]$ ssh-keygen -t rsa
[username@amazon ~]$ cd .ssh
[username@amazon ~]$ chmod 700 .ssh
[username@amazon ~]$ cat id_rsa.pub > .ssh/authorized_keys
[username@amazon ~]$ chmod 600 .ssh/authorized_keys
[username@amazon ~]$ sudo chown username:ec2-user .ssh

In the above step, username is the user we created and ec2-user is the default user.

Before downloading the private key, you will need to copy the key to ec2-user folder and chmod it to 777. We can only download from the file using ec2-user, since you have the key for that username.
[username@amazon ~]$ sudo cp id_rsa /home/ec2-user/
[username@amazon ~]$ sudo chmod 777 /home/ec2-user/id_rsa

Now you just need to download the private key “id_rsa”. We can use scp command to download/upload files from EC2. If you prefer GUI then you may use 'WinSCP' software. 

Now come to local machine’s terminal, where you have my_key.pem file for ec2-user and do as follows:
[username@local ~]$ scp -i my_key.pem ec2-user@amazon:/home/ec2-user/id_rsa download_folder

The above command will copy the key “id_rsa” to the download_folder directory on your local machine. Delete “/home/ec2-user/id_rsa” from server, because it’s a private key.

Once you have copied the key in your local machine, do following.
[username@local ~]$  chmod 600 id_rsa

Now we can connect to aws using new username as follows:
[username@local ~]$  ssh -i id_rsa username@amazon


Congratulation!! you have done it.
P.S. you can rename your id_rsa file to any name you want. In my case, I have renamed to my name.

So, in this manner, you can setup multiple users to use one EC2 instance!!

Possible errors
You might get following error.


Following might be your solution.

At the amazon server
[username@amazon ~]$ cat id_rsa.pub > .ssh/authorized_keys (make sure this is done)
[username@amazon ~]$ chmod 700 .ssh (make sure ssh folder has required access right)
[username@amazon ~]$ chmod 600 .ssh/authorized_keys (make sure has required access right)

At the local machine
[username@local]$ chmod 600 id_rsa (make id_rsa has access right)

Making life easier
Go to your windows machine and navigate to C:\Users\username\

Create .ssh folder and inside .ssh create a file 'config'.
Now open config in notepad and write following

------------------------
Host amazon            #can use any host name
Hostname ec2-52-0-158-182.compute-1.amazonaws.com #address of your amazon server
User username         # username of amazon server
IdentityFile /id_rsa   # private key, make sure its in .ssh folder
-------------------------

And save it and exit.

Now go into your local machine terminal and type following

[username@local ~]$  ssh amazon


Hola!! you are in.

This way you don't have to type the hostname or ip address of amazon every time you login. It makes your like much easier.

Good Luck!!!

1 comment:

  1. MGM Resorts Casino Hotel - DRMCD
    MGM Resorts 세종특별자치 출장마사지 Casino Hotel - Davenport, 정읍 출장샵 IA - See reviews, photos & phone number for MGM 구미 출장마사지 Resorts Casino 화성 출장마사지 Hotel - 태백 출장샵 Davenport.

    ReplyDelete