How to add PEM file as SSH private key to "known hosts"

26,710

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

Share:
26,710

Related videos on Youtube

smeeb
Author by

smeeb

Updated on September 18, 2022

Comments

  • smeeb
    smeeb over 1 year

    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?

    • Nathan Basanese
      Nathan Basanese almost 9 years
      // , 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.
  • smeeb
    smeeb over 9 years
    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.
  • barlop
    barlop over 9 years
    @smeeb why is somebody giving you their private key? their private key is meant to be private to them.
  • smeeb
    smeeb over 9 years
    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.
  • barlop
    barlop over 9 years
    @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
    barlop over 9 years
    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.