175

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.example

What option should I use?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
danielrvt
  • 10,177
  • 20
  • 80
  • 121

7 Answers7

288

Use the -i option:

ssh -i mykey.pem user@mydomain.example

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
legoscia
  • 39,593
  • 22
  • 116
  • 167
  • 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
70
chmod 400 mykey.pem

ssh -i mykey.pem user@mydomain.example

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
shubham rajput
  • 1,015
  • 1
  • 9
  • 12
  • 7
    "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
26

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
  • 489
  • 5
  • 10
18

To connect from Terminal to AWS AMI:

chmod 400 mykey.pem

ssh -i mykey.pem ec2-user@mydomain.example
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
shbedev
  • 1,875
  • 17
  • 28
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
  • 2,473
  • 1
  • 23
  • 31
4

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.example
   User ubuntu
   IdentityFile /home/you/path-to-pem/key.pem
   IdentitiesOnly yes
  1. Call short command: ssh mydomain.example
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
pymen
  • 5,737
  • 44
  • 35
0

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Devqxz
  • 51
  • 1
  • 6