how to SSH to EC2 without explicitly using the pem key?
Solution 1
ssh-add ~/.ssh/KEY_PAIR_NAME.pem
Solution 2
By default the SSH client will look for keys named id_rsa
, id_dsa
and id_ecdsa
in ~/.ssh/
. If your key isn't named like that you either need to specify it on the command line with -i
as you've been doing, or specify it in the client configuration.
You can add something like this to ~/.ssh/config
to automatically select this key when SSHing to EC2:
Host *.compute-1.amazonaws.com
IdentityFile ~/.ssh/ec2_rsa
Solution 3
How do you name your private key? It should have default id_rsa file name (rename pem file to /home/ubuntu/.ssh/id_rsa)
Solution 4
You can use ssh-agent
and ssh-add
to avoid having to specify the private key explicitly.
You can put the commands in your .profile
or .bashrc
so they get executed every time you log in. You can find an example startup script at the bottom of this post.
Related videos on Youtube

user798562
Updated on September 18, 2022Comments
-
user798562 about 2 months
I have a windows as my main OS. Using VMware player, I setup a Ubuntu server 12.4 as a guest machine. The Ubuntu server have "ubuntu" user.
I created a new EC2 instance + setup pem key. From the windows machine, when I use putty+pem key - I can ssh.
I added the pem key to my VMware Ubuntu server ( /home/ubuntu/.ssh/) In addition, i set the following permissions:
chmod 700 /home/ubuntu/.ssh
chmod 600 /home/ubuntu/.ssh/*
Through the Ubuntu server - I tried to SSH to the ec2 instance without success:
ssh [email protected]_IP Permission denied (publickey)
. If I explicit use the pem key, it works:ssh -i /home/ubuntu/.ssh/NAME.pem [email protected]_IP
- Please note, that I must use direct path to the key, otherwise, I'll getWarning: Identity file NAME.pem not accessible: No such file or directory. Permission denied (publickey).
Please advise. Thanks!
-
user798562 over 9 yearsI did that, but when i reboot - i need to do that all over again. Any way to avoid that?
-
user798562 over 9 yearsit called eldad.pem (default name from AWS). I renamed it to id_rsa and it worked. is there a way to keep it in the current name and still use it?
-
Andrei Mikhaltsov over 9 yearsYep, Daniel already wrote about it in his answer :)
-
David Levesque over 9 yearsEdited my answer to include that.
-
douglaslps almost 8 yearsYou might need to start the authentication agent: eval 'ssh-agent -s'