21

I'm trying to find my ~/.ssh folder, but can't. I'm familiar with terminal so I've been going through my filesystem and can't find it anywhere. Has it relocated in the file tree or is my computer just funky? I need to find my private key.

1
  • 2
    ~/.ssh is in your home directory. So have you opened a terminal, typed ls -la and just looked? – Giacomo1968 Oct 5 '17 at 0:31
26

In macOS, you need to generate your public and private keys from the Terminal. If you haven't yet done this, the .ssh directory will not exist. To create them:

Open the terminal App and enter the following command:

ssh-keygen

You'll get a prompt to choose the location for the keys. It will say “Enter file in which to save the key (/Users/your-username/.ssh/id_rsa)”. If you’re happy with the default location(~/.ssh/) just tap Return. Within your shell the ~ character is equivalent to /Users/your-username/. It stands for your home directory.

It will now say “Enter passphrase (empty for no passphrase):”. Enter your passphrase and press Return. You are asked to re-enter the password to confirm you typed it correctly. This passphrase is used to encrypt the private key and it's recommended you set one.

The prompt will now say “Your identification has been saved in /Users/your-username/.ssh/id_rsa” and “Your public key has been saved in /Users/your-username/.ssh/id_rsa.pub.” It'll then show you the key's Fingerprint and Randomart. The Fingerprint matches the public key and can be used in some situations for authentication, and the Randomart file is designed to match the Fingerprint but be easier to visually identify that it is the right key. You don't need to copy these down for most purposes.

Now you can view the newly-created .ssh directory and find your key within.

You can find a pretty readable guide on the subject here.

Edit: If you want to copy in previously-saved public and private keys:

  • In the terminal, enter cd ~
  • Then mkdir .ssh; chmod 700 ~/.ssh

This will create the directory and give it adequate permissions. Within this directory, you can now paste in your two files which contain the matching public and private key pair. These will be your id_rsa.pub and id_rsa files respectively. Once this is done, double-check their permissions are what they need to be by running:

ls -l ~/.ssh/id_rsa*

The output should look like this(except the numbers 1766 and 388):

-rw------- 1 user root 1766 Oct 04  2017 .ssh/id_rsa
-rw-r--r-- 1 user root  388 Oct 04  2017 .ssh/id_rsa.pub

In case you get something that doesn't look like this, set the permissions of these files with:

$ chown user:user ~/.ssh/id_rsa*
$ chmod 600 ~/.ssh/id_rsa
$ chmod 644 ~/.ssh/id_rsa.pub

Note that with chown user:user ~/.ssh/id_rsa* just above, user is the user account you're logged in with, not literally "user".

4
  • I already have a private and public key inside my keychain, imported over from my previous Mac Mini. Is there a way to load those into the ssh folder? – Alex Schneider Oct 4 '17 at 23:16
  • 1
    @AlexSchneider Added a section above about pasting in your old keys. – baelx Oct 5 '17 at 0:20
  • When I run ssh-keygen, I get the error: "Could not create directory '/Users/user/.ssh': No such file or directory". I'm on macOS 10.15.3. – nnyby Mar 18 '20 at 14:28
  • @nnyby Assume you solve your issue, but just go into the and type mkdir ~/.ssh and then chmod 700 ~/.ssh. This is detailed in the answer as well. – Giacomo1968 Oct 31 '20 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.