How to generate an SSH key pair in Mac OS?

You can generate a key in Mac OS using the ssh-keygen command. You should run it in Terminal or iTerm and specify a type of encryption. You will also be asked for a file in which the key should be saved to and for a passphrase (password) for the key:

This will create a private key written to ~/id_rsa and a public key written to ~/id_rsa.pub. The passphrase is used to protect your key. You will be asked for it when you load your key. You can load the key using the following command:

ssh-add ~/id_rsa

11 Comments

  1. Reply February 11, 2016 / 06:10 BrandtSiteGround Team

    Your strong tags should not be literal

    • Reply February 11, 2016 / 15:51 Ivan YordanovSiteGround Team

      Thank you for the report.

  2. Reply February 11, 2016 / 06:12 BrandtSiteGround Team

    Maybe change your example to rsa instead of dsa

    • Reply February 11, 2016 / 15:52 Ivan YordanovSiteGround Team

      Everything would be the same. You just need to use:

      ssh-keygen -t rsa

      when you are creating the key pair.

  3. Reply April 29, 2016 / 18:32 OlgaSiteGround Team

    How i can use SG ssh key ?
    I did key in CP SSH manager
    and dowload it to my mac
    in terminal
    i used ssh-keygen -t rsa
    and follow all the instructions here
    and i copy data from SGssh key to the .ssh/id_rsa
    and try to connect
    with ssh user@domain -p18765
    but
    SG does not allow it by say - Permission denied (publickey)

    What i can do else?

    • Reply May 3, 2016 / 10:40 escalationsSiteGround Team

      Using the cPanel SSH key generation is merely an alternative to creating the key from the terminal. You should use one of the methods, but not both. The step which you seem to have missed in your explanation above is loading the generated key. For more detailed information, please refer to the article below:

      https://www.siteground.com/kb/how_to_log_in_to_my_shared_account_via_ssh_in_mac_os/

  4. Reply December 11, 2016 / 01:26 PeterSiteGround Team

    As of the new OS/X Sierra dsa keys are no longer supported. The result is the horribly cryptic "permission denied (public key) error. Changing to rsa fixed the problem (thanks go to Linode tech support).

    • Reply February 10, 2017 / 05:48 Sandra EricksonSiteGround Team

      What do you mean changing to rsa fixed the problem? I am experiencing permission denied error.

      • Reply February 10, 2017 / 12:21 Ivan AtanasovSiteGround Team

        Peter was referring to converting the DSA key to RSA. Alternatively, you can create a new RSA key and load it in your local SSH agent.

  5. Reply January 1, 2017 / 13:46 BernhardSiteGround Team

    Maybe change dosa to rsa in the other document too https://www.siteground.com/kb/how_to_log_in_to_my_shared_account_via_ssh_in_mac_os/

    • Reply January 6, 2017 / 20:05 Ivan StefanovSiteGround Team

      Thank you for the feedback, Bernhard, we've altered the article in question.

* (Required)