-
ssh into your ec2 instance with your default credentials(username and .pem key file): **
ssh -i <path to the .pem key file> <default username>@<Public DNS of ec2 instance>
-
create a new user:
sudo adduser <newusername>
-
add the new user to the sudoers file:
sudo visudo
-
add the following line after the comment line, “User privilege specification”:
-
copy the public key of the default user present in :
~/.ssh/authorized_keys
-
cat ~/.ssh/authorized_keys
-
Switch to new user:
sudo su <newusername>
-
Create the SSH directory and authorized users file, with the correct permissions:
mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
-
paste the public key copied from the dafault user's home directory :
~/.ssh/authorized_keys
to the new user's authorized_keys file:
vi ~/.ssh/authorized_keys
-
ssh into the instance with th new newusername by using the previous .pem file:
ssh -i <path to the .pem key file> <newusername>@<Public DNS of ec2 instance>