There must be some easier way than the documented ssh -i my.pem user@ip-addr, by putting the pem file in ~/.ssh and ssh knowing to use my.pem based on user@ip-addr, but I can't figure out how.
2 Answers
Most command-line options can be specified through ~/.ssh/config, either globally or for individual hosts. The equivalent of -i is the IdentityFile option.
If this key file is only used for EC2, it's best to specify this in a section that only matches EC2. For example, if you usually SSH by name (e.g. ssh ec2-198-x.compute-1.amazonaws.com), this format would work:
Host *.amazonaws.com IdentityFile ~/.ssh/myec2.pem(The most-specific sections should go at the top; the global Host * section should go at the very bottom of the file.)
In addition to this, OpenSSH will also look at several fixed locations, such as ~/.ssh/id_rsa, for the private key file. You could place the key there, but it's better to reserve this filename for your "main" key, and one that you generated yourself and not just received from AWS.
This solution comes from the StackOverflow post
Connect to Amazon ec2 Instance without specifying .pem file:
Copy the
.pemfile to~/.sshCreate a config file at
~/.sshtouch ~/.ssh/configopen the config file in nano or vim:
nano ~/.ssh/configAdd the following line to
configfileHost host_name HostName ec2.server.address.com User ec2-user IdentityFile "~/.ssh/ec2.pem"Now connect from terminal:
ssh host_name.