MacOS – SSH access using .pub key

macossshterminal

I don't know if I'm overthinking this too much but I can't seem to understand how to use a ssh public key to create an ssh connection trough my mac.

I'll list my steps bellow:

  1. First created the SSH keys in my server in cpanel -> SSH keys and created a key pair with name id_rsatest

  2. Then I downloaded to my mac both public and private key files, so now in my downloads folder I have the files: id_rsatest and id_rsatest.pub

  3. Then I moved the id_rsatest.pub file to Users/myuser/.ssh/ and opened Terminal.app -> Shell -> New Remote Connection and entered the information below:

    enter image description here

    and receive this error:

    ssh: connect to host example.com port 22: Operation timed out
    [Process completed]
    

What am I doing wrong? Is this even the way on how to create a SSH access with SSH keys?

Best Answer

You got it the wrong way round :-)

  • create a key pair on the Mac you are using
  • put both id_rsa.pub and id_rsa into ~/.ssh and run chmod 600 id_rsa
  • append the generated public key (id_rsa.pub) to .ssh/authorized_keys on the remote machine (your server in this case). If you are allowed to login in via ssh using a password, use

    ssh remoteuser@remotemachine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys" < ~/.ssh/id_rsa.pub
    

    otherwise transfer the file by whatever means you have to upload files.

  • run ssh remoteuser@remotemachine in your shell to connect

PS: The private key should never need to leave the machine it's generated on