Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login to Amazon EC2 via ssh

I have a question and am stuck at something. I recently created AWS EC2 Ubuntu instance with default settings. I have an Ubuntu machine locally as well and so far I managed to connect successfully to the Ubuntu instace with this command

ssh -i /var/www/my-key-pair.pem [email protected]

However I want to know if there is a simpler way to login to ssh without passing the private key everytime I login and add its ip to my ~/.ssh/config so that I could login as easily as ssh [email protected]. By the way I am using AWS free tier package.

like image 320
Sanjok Gurung Avatar asked Feb 06 '23 23:02

Sanjok Gurung


2 Answers

Create ~/.ssh/config with:

Host myhost
  IdentityFile /var/www/my-key-pair.pem
  Use ec2-user
  Hostname ec2-198-51-100-1.compute-1.amazonaws.com

And the you can login using ssh myhost.

like image 56
Jakuje Avatar answered Feb 08 '23 12:02

Jakuje


ssh-add -K /var/www/my-key-pair.pem will permanently add your private key as identity file in ~/.ssh/config.

Thus, ssh username@ip_address_of_ec2_instance works.

To avoid typing username and IP address every time, add to .ssh/config:

Host my-instance
HostName some.ip.address.here
User username

With this config, you can simply do ssh my-instance to remote login.

like image 44
acsrujan Avatar answered Feb 08 '23 13:02

acsrujan