Stephen Fry

"Education is the sum of what students teach each other in between lectures and seminars. " - Stephen Fry

Tuesday, 8 January 2013

Quick Guide: Remotely adding a new user on Ubuntu 12.04

In the following is a quick guide to creating a new user (we will be calling this user "username") on a Amazon Cloud EC2 instance with Ubuntu 12.04 AMI

CREATING NEW USER

We will begin by adding the new user and setting the new users password

$ sudo adduser <username>

This command will then take you though the setting up a password. You can check the users that you now have my opening the /etc/passwd file

Now we are going to make use of a unix command called "visudo", this allow you to edit the sudoerrs file in a concurrent access safe way.  Before using visudo, we need to check that the default editor is the one that we would like and change it, if required:

sudo update-alternatives --config editor

Now you will be given a dialogue where you can select your favourite text editor.

visudo


Now add the following line to the file that has been opened:

<username> ALL=(ALL) ALL

Now exit the machine and log back in as ubuntu. 


You can now switch to this newly created user with 
$ su <username>
$ cd /home/<username>

GENERATING KEYS FOR SSH TO NEW USER

We are now going to generate the public key for public/private key pair that will be used later to SSH directly into this new user

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

This line has generated two files, the public key paws.pub and the private key paws. To be able to ssh from a local machine to the new paws user on this remote machine, we need to place copy the contence of paws.pub into /home/paws/.shh/authorized_keys and we need to have a copy of the private key (called paws) on the local machine


$ mkdir .ssh
$ chmod 700 .ssh
$ cat mykey.pub > .ssh/authorized_keys
$ chmod 600 .ssh/authorized_keys

Now that we have the key we need to transfer this key on to our local machine and generate the private key



$ sudo chown <username>:ubuntu .ssh
$ sudo cp mykey /home/ubuntu


$ sudo chown <username>:ubuntu .ssh/authorized_keys

$ sudo chmod 777 /home/ubuntu/mykey


Logout and return to local machine
$ scp -i originalkey.pem ubuntu@<URL-of-server>:/home/ubuntu/mykey mykey

Now you can ssh into your remote machine as this new user:

chmod 400 mykey ssh -i mykey <username>@url-of-server






No comments:

Post a Comment