[AWS] Create/Migrate Linux Users on Amazon EC2

Create or migrate user account with SSH login only (no password login) on AWS EC2 t2.nano with Ubuntu 14.04 LTS.

Create User Account

# login to EC2 running *Ubuntu 14.04*
$ ssh -i my_key.pem ubuntu@my_EC2_IP
# assume the name of the user account is *foo*
$ sudo adduser foo --disabled-password
# su to *foo*
$ sudo su - foo
# Create a *.ssh* directory for the *authorized_keys* file
$ mkdir .ssh
$ chmod 700 .ssh
# Create the public and private keys for ssh login
# Don't use a paraphrase -- just hit enter
$ ssh-keygen -b 1024 -f fookey -t dsa
# After keygen finished,
# two files named *fookey* and *fookey.pub* will be generated.
# *fookey* is private key and *fookey.pub* is public key
$ cat fookey.pub > .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

Download fookey to your local machine. Now you can SSH login as foo:

$ ssh -i fookey foo@my_EC2_IP

For more details, see [2], [4], and [5].

Migrate User Account

My case is simple. Every user has his own block device as home directory. So create user account as described above, mount the user's block device back to his home directory. Then

$ sudo chown foo:foo /home/foo -R

For more details or more sophisticated case, see [6] and [7].


[1]Migrate from t1.micro to t2.micro Amazon AWS - Stack Overflow
[2]Managing User Accounts on Your Linux Instance - Amazon Elastic Compute Cloud
[3]Run Dictionary and Tipitaka Websites on Amazon Web Services
[4]Manage multiple Linux Users on 1 Amazon EC2 Instance | the lost logbook
[5]How to add new users to EC2 and give SSH Key access
[6]chown recursively changed permissions - Ask Ubuntu
[7]Linux: Changing UIDs and GIDs for a user by Stuart Colville