Thanks to the original author on StackOverflow – Source link below; for this beautiful and clear and concise documentation on adding a new keypair (by creating a new user) on the AWS linux instance. I tried this on a CentOS7 installation and bitnami powered suiteCRM installation and with a few changes, it worked awesomely. Post w/ instructions for bitnami instances will follow soon.
Though you can’t add a key pair to a running EC2 instance directly, you can create a linux user and create a new key pair for him, then use it like you would with the original user’s key pair.
In your case, you can ask the instance owner (who created it) to do the following. Thus, the instance owner doesn’t have to share his own keys with you, but you would still be able to ssh into these instances. These steps were originally posted by Utkarsh Sengar (aka. @zengr) at http://utkarshsengar.com/2011/01/manage-multiple-accounts-on-1-amazon-ec2-instance/. I’ve made only a few small changes.
Step 1: login by default “ubuntu” user:
$ ssh -i my_orig_key.pem [email protected]
Step 2: create a new user, we will call our new user “john”:
[[email protected] ~]$ sudo adduser john
Set password for “john” by:
[[email protected] ~]$ sudo su –
[[email protected] ubuntu]$ passwd john
Add “john” to sudoer’s list by:
[[email protected] ubuntu]$ visudo
.. and add the following to the end of the file:
john ALL = (ALL) ALL
Alright! We have our new user created, now you need to generate the key file which will be needed to login, like we have my_orin_key.pem in Step 1.
Now, exit and go back to ubuntu, out of root.
[[email protected] ubuntu]$ exit
[[email protected] ~]$
Step 3: creating the public and private keys:
[[email protected] ~]$ su john
Enter the password you created for “john” in Step 2. Then create a key pair. Remember that the passphrase for key pair should be at least 4 characters.
[[email protected] ubuntu]$ cd /home/john/
[[email protected] ~]$ ssh-keygen -b 1024 -f john -t dsa
[[email protected] ~]$ mkdir .ssh
[[email protected] ~]$ chmod 700 .ssh
[[email protected] ~]$ cat john.pub > .ssh/authorized_keys
[[email protected] ~]$ chmod 600 .ssh/authorized_keys
[[email protected] ~]$ sudo chown john:ubuntu .ssh
In the above step, john is the user we created and ubuntu is the default user group.
[[email protected] ~]$ sudo chown john:ubuntu .ssh/authorized_keys
Step 4: now you just need to download the key called “john”. I use scp to download/upload files from EC2, here is how you can do it.
You will still need to copy the file using ubuntu user, since you only have the key for that user name. So, you will need to move the key to ubuntu folder and chmod it to 777.
[[email protected] ~]$ sudo cp john /home/ubuntu/
[[email protected] ~]$ sudo chmod 777 /home/ubuntu/john
Now come to local machine’s terminal, where you have my_orig_key.pem file and do this:
$ cd ~/.ssh
$ scp -i my_orig_key.pem [email protected]:/home/ubuntu/john john
The above command will copy the key “john” to the present working directory on your local machine. Once you have copied the key to your local machine, you should delete “/home/ubuntu/john”, since it’s a private key.
Now, one your local machine chmod john to 600.
$ chmod 600 john
Step 5: time to test your key:
$ ssh -i john [email protected]
So, in this manner, you can setup multiple users to use one EC2 instance!!