142

I would like to know how to connect over ssh using a .pem file to any server.

Currently I'm executing the following command:

ssh user@mydomain.com

What option should I use?

danielrvt
  • 9,472
  • 16
  • 73
  • 115

7 Answers7

245

Use the -i option:

ssh -i mykey.pem user@mydomain.com

As noted in this answer, this file needs to have correct permissions set. The ssh man page says:

ssh will simply ignore a private key file if it is accessible by others.

You can change the permissions with this command:

chmod go= mykey.pem

That is, set permissions for group and others equal to the empty list of permissions.

legoscia
  • 38,687
  • 22
  • 110
  • 157
  • 1
    I just to point out that if you aren't root, you should chmod the .pem file and enable the read permission for your user. – Ionut Ciuta Apr 02 '18 at 13:35
  • 1
    This only addresses the client side of the equation. For setting up the server, you'll need to copy your public key into the ~/.ssh/authorized_keys file. You can do this from your local machine by: "ssh-copy-id -i ~/mykey.pub user@mydomain.com". – Todd Walton Nov 02 '18 at 15:39
  • If port is different - ssh -i mykey.pem user@mydomain.com -p 2222 – Koustav Dec 21 '21 at 09:16
58
chmod 400 mykey.pem

ssh -i mykey.pem user@mydomain.com

Will connect you over ssh using a .pem file to any server.

shubham rajput
  • 865
  • 1
  • 8
  • 12
  • 6
    "chmod 400" solved this issue: Permissions 0777 for 'some_file.pem' are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. – pantos27 Nov 22 '17 at 13:45
25

For AWS if the user is ubuntu use the following to connect to remote server.

chmod 400 mykey.pem

ssh -i mykey.pem ubuntu@your-ip
Pranoy Gn
  • 469
  • 5
  • 9
9

To connect from Terminal to AWS AMI:

chmod 400 mykey.pem

ssh -i mykey.pem ec2-user@mydomain.com
shbedev
  • 1,699
  • 16
  • 25
6

You can connect to a AWS ec-2 instance using the following commands.

chmod 400 mykey.pem

ssh -i mykey.pem username@your-ip

by default the machine name usually be like ubuntu since usually ubuntu machine is used as a server so the following command will work in that case.

ssh -i mykey.pem ubuntu@your-ip
officialrahulmandal
  • 1,676
  • 1
  • 15
  • 27
2

If you still got error messages like:

Received disconnect from 34.219.50.0 port 22:2: Too many authentication failures. Disconnected from 34.219.50.0 port 22

  1. Edit your ssh config located at ~/.ssh/config and add new record at the end
Host mydomain.com
   User ubuntu
   IdentityFile /home/you/path-to-pem/key.pem
   IdentitiesOnly yes  
  1. Call short command: ssh mydomain.com
pymen
  • 4,609
  • 38
  • 30
0

what resolved it for me was to run: sudo chown $USER: {.pem_file}

Devqxz
  • 41
  • 1
  • 6