Add an ssh user with key to an Amazon EC2 instance

First you need to create a pair of keys on your local machine (replace “user” with your chosen username):

ssh-keygen -b 1024 -f user -t dsa

This will create 2 files: user (private key), (public key). Now copy the public key file to a temporary place on your instance:

scp -i root *.pub

Log in to the instance as root. For each user you are creating, add the user to your instance with the

sudo adduser user

For simplicity’s sake, use the same “user” name as you did for key generation. Now we need to place the key into their ssh authorized keys file (replacing “user” with the username you chose earlier)

sudo mkdir ~user/.ssh
sudo cat /tmp/ >> ~user/.ssh/authorized_keys
sudo chmod 700 ~user/.ssh
sudo chmod 600 ~user/.ssh/authorized_keys
sudo chown user:user ~user/.ssh
sudo chown user:user ~user/.ssh/authorized_keys

Now log in:

ssh -i ~/.ssh/user -l user

To add your new user to the sudoers list:

sudo adduser user sudo

Don’t forget, that you probably want to delete an old user:

sudo userdel -r olduser

Published: July 24 2013

blog comments powered by Disqus