Tuesday, 20 October 2015

How to create a new user in Ubuntu server for Amazon EC2 user with ssh key based login

Here is the step by step process to create a new user called "tom" in an Ubuntu Server which is hosted in Amazon Web Services EC2 instance.

1. create user 'newuser'

adduser tom

2. create ssh key for the user 'tom'

su - tom

[email protected]:~$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tom/.ssh/id_rsa): 
Created directory '/home/tom/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/tom/.ssh/id_rsa.
Your public key has been saved in /home/tom/.ssh/id_rsa.pub.

3. Copy the public key of this user to authorized_keys file for enabling ssh based login

cp .ssh/id_rsa.pub .ssh/authorized_keys

4. Enable ssh authentication for 'tom' user

Edit /etc/ssh/sshd_config and change the line AllowUsers as below:

AllowUsers ubuntu tom

service ssh restart 

5. Add 'tom' user to sudoers list

cat /etc/sudoers.d/tom-user
# User rules for tom

6. Set password for 'tom' user as root user

passwd tom

7. Now download the /home/tom/.ssh/id_rsa to your machine and rename it to your_servername.pem

8. Set the correct permission for this private key

chmod 400 your_servername.pem 

9. Login to the server using this private key

ssh -i your_servername.pem [email protected]

No comments:

Post a Comment