12

I have Ubuntu desktop, and I have been given a PEM file (mykey.pem) that is the SSH private key for a Linux server. I am trying to figure out where this PEM file needs to be placed locally on my machine, and how it can be configured/added to my "SSH known hosts".

Googling this subject matter turns up lots of answers/articles for creating SSH keys, but not for adding an existing key to known hosts. Ideas?

1
  • // , Good question! This should work: $ ssh-keygen -y -f mykey.pem >> ~/.ssh/authorized_keys Too many IT departments just send a PEM file out without any sort of instructions on how to use it, locally. If anyone needs more about this, I can add an answer. Aug 27, 2015 at 16:48

2 Answers 2

5

I don't know about different types of SSH keys. But you would put the public key on the destination computer, not your private key. Your private key stays private.

And the public key of the source computer should be placed on the dest computer in ~/.ssh/authorized_keys This can be done manually or via the ssh-keygen command.

I suggest you do cat on the public key on the source computer and cat on authorized_keys on the dest computer and make sure the source's one looks like it is the same format as those in authorized_keys

The known_hosts file is something which gets appended to automatically. You don't need to edit it manually. You can connect even after deleting the known_hosts file.

EDIT-

To incorporate some of the comments into the answer. The public key comes from the private key. Normally the private key stays private, but the OP was being given a private key, this is unusual, but it's an interesting way of doing it, because it means the dest computer can then already have his public key. So he could log in without having to add anything to the dest computer's authorized_keys. ssh -i always takes a private key. He need only do ssh -i path/to/privatekeyfile user@dest The OP is using "openstack", some cloud service, and as the openstack site says docs.openstack.org/user-guide/content/ssh-into-instance.html $ ssh -i MyKey.pem [email protected] So whatever the name of the private key file is, and wherever it is stored, you specify that when doing ssh -i

5
  • Thanks @barlop (+1) - if you read the first sentence of my question, you'll see me state that the PEM file is the SSH private key, and that I am trying to install it correctly on my computer. I'm not worried about the server-side, that has already been taken care of for me. So my question is: how do I install a private key on my Ubuntu machine? It was a key that I did not generate via ssh-keygen; it was given to me by a trusted source.
    – smeeb
    Jan 22, 2015 at 19:20
  • 1
    @smeeb why is somebody giving you their private key? their private key is meant to be private to them.
    – barlop
    Jan 22, 2015 at 19:22
  • Thanks again @barlop (+1) - But it is not a someone, it is OpenStack, a cloud-generating IaaS. It is a powerful open source project with millions of man-hours and corporate backing behind it. The 30,000 foot view here is: to create a new "app instance" on OpenStack Horizon, you need to first create an SSH key pair. OpenStack automagically installs the public key on the new instance. You are supposed to download the private key (a PEM file) and install it locally so that you can SSH into the instance if you wish.
    – smeeb
    Jan 22, 2015 at 19:26
  • @smeeb I don't know about openstack so much, but you can do e.g. ssh -i ~/.ssh/id_rsa [email protected] or as the openstack site says docs.openstack.org/user-guide/content/ssh-into-instance.html $ ssh -i MyKey.pem [email protected] So whatever the name of the private key file is, and wherever it is stored, you specify that when doing ssh -i
    – barlop
    Jan 22, 2015 at 19:36
  • 1
    Also, since the public key comes from the private key, it may be that that command will do it, as they should already have the public key if they're giving you a private key. So you won't have to add the public key to authorized_keys on their computer as it'd already be there.
    – barlop
    Jan 22, 2015 at 19:40
3

According to the ssh manual:

-i identity_file

Selects a file from which the identity (private key) for public key authentication is read. The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa for protocol version 2.

E.g. $ ssh -i mykey.pem [email protected].

You can also add your key to the authentication agent to save you from having to pass the identity file every time you want to connect to the remote server:

ssh-add — adds private key identities to the OpenSSH authentication agent

E.g. after $ ssh-add mykey.pem you can just do $ ssh [email protected].

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .