How to create users for your EC2 Instance that can connect securely via ssh

Public and Private Keys for AWS Users





Create a normal user in AWS
sudo adduser username


How to add user in sudo group or wheel group
sudo usermod --groups sudo --append username


Allow user to run the sudo command without asking the password

root    ALL=(ALL)       ALL
username ALL=(ALL) NOPASSWD:ALL



Or you can add complete sudo or wheel group. The users under sudo or wheel group run sudo command with asking password to sudo users

%wheel ALL=(ALL) NOPASSWD:ALL
%sudo ALL=(ALL) NOPASSWD:ALL


To switch to the user directory

cd /home/username


Run the following command inside the user home directory to generate keys

ssh-keygen - b 4096 -f username -t rsa


To create the .ssh directory under user home directory
 mkdir .ssh


Give permission to the owner to read, write and execute (700) 
chmod 700 .ssh


To store public key in authorized_keys file (after exit from the username )

sudo cat username.pub >> .ssh/authorized_keys


so that the owner can read and write to the file 

chmod 600 .ssh/authorized_keys


To set the owner to  username and the group owner is centos 

sudo chown username:centos .ssh


To set the owner to and the group owner is centos
sudo chown username:centos .ssh/authorized_keys


To copy user 
sudo cp username /home/centos

OR 

sudo rsync -avr username /home/centos


so that all can read, write and execute 

sudo chmod 777 /home/centos/username


To Download the private key file to local pc (using your local terminal) 
scp -i /home/gautamthakur/Download/gautam-demo.pem centos@aws-public-ip:/home/centos/username username


After download the private key to the local PC. So that user can read. (If you have set the password at the time of created the key then provide the password)
chmod 400 username


To access you AWS Server from the local PC
sudo ssh -i private_key_just_created username@you_aws_public_ip


This key that you have created, you can now give that ti the person that you want to access your instance

If you added a password on your key like in demo, you would also need to give your user that password as well.

Popular posts from this blog

Firewalld Command - Useful firewall-cmd Examples (RHEL based)

How to Convert PEM KeyFile into PPK KeyFile and vice versa?