Tuesday, February 24, 2015

How to add users to your AWS EC2 Linux instance

Do you need to add more users to your AWS EC2 Linux instance? This is a step-by-step tutorial to guide you through the basic process.

1) Open a terminal session and navigate to the path where you have your-key-pair.pem file

2) Type the following command in order to extract the public key from your key pair
ssh-keygen -y -f your-key-pair.pem
3) You will get as a response a string like the one below. Copy it to your clipboard.
ssh-rsa AAAAB3NzaC1yVh0/ThkcfO479gFjMUVw48D2Pi4u0P+0lvP0tpzKcZ/nwnzhFIDyUHsVKMN0F97DCoPQEbk5jmyHRSBok+cuEXAMPLEt1VI7TLSAwWZj5aRedb+awFDLxBgS8SN/nvsaP4+KY8uGum10YV83/wGNZjYEVRLg9NjyDbuVERYFAKEhscyZAbWTMw2t30JELizxyXZx4s4OImfS4yOCnDLFgHFf3JUjGhTUg1O+10I3V2TB3j63166AEB+98JizrRtwJ85AUN/wmMD0V2YIiEaa2rMLbdGZw8lSlPakV3bedx+8NYf+s2+SLwB
4) Login with your ec2-user to your instance, as usual.
ssh -i your-key-pair.pem ec2-user@yourawspublicip
5) Create a Linux user account on the EC2 instance
sudo adduser username
6) OPTIONAL: Only if you want to give this user sudo access, do the following:
  • Switch to root: sudo su -
  • Open sudo config file: visudo
  • Add this line to the commands section: username  ALL=(ALL)  NOPASSWD:ALL

7) Switch to the user you have just created
sudo su - username
8) Create a .ssh directory
mkdir .ssh
9) Change permissions of this directory
chmod 700 .ssh
10) Create a new file authorized_keys in this directory and paste the public key that you have on your clipboard. Make sure that it doesn't miss the first letter.
vi .ssh/authorized_keys
11) Change permissions of this file
chmod 600 .ssh/authorized_keys
12) Send your-key-pair.pem file to the new user. She will be able to connect to the EC2 instance running the following command from the same folder where she has placed your-key-pair.pem file.
ssh -i your-key-pair.pem username@yourawspublicip
13) If thy get public key error, make sure they give the right permissions to your-key-pair.pem.
    chmod 600 your-key-pair.pem

1 comment: