SSH Connection from local machine to Amazon(aws) EC2 ubuntu instance with new user and keys


In Amazon Elastic Cloud Compute(EC2), a key needs to assign when creating an instance. ssh connection can be establish by using that key.

ssh -i your_private_key.pem username@hostname

If you have a ubuntu instance the username may be ubuntu

Now, we need to allow another user from another local machine to connect your remote server via ssh.

1. Generating key pairs for your local machine:
Open your terminal(Ctrl+Alt+t on windows), in your local machine, write
ssh-keygen -t rsa

It will create two files id_rsa and in .ssh directory under your home directory, where id_rsa is your private key and is your public key.

2. Configure remote server

Create a new user on remote server:
adduser testuser

Login with newly created user:
sudo su testuser

Creating authorized_keys file:

cd $HOME
mkdir .ssh
chmod 700 .ssh
vi .ssh/authorized_keys

Press i for insert mode, copy content and paste here. Press ESC, then type :wq save your changes.

Changing file permissions and owner:

chmod 600 .ssh/authorized_keys
chown testuser:testuser .ssh
chown testuser:testuser .ssh/ authorized_keys

Now you can able to connect with your remote server from local machine without locating .pem file:
ssh testuser@hostname