Missing ~/.ssh Folder in macOS High Sierra
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".
Related videos on Youtube
Alex Schneider
Updated on September 18, 2022Comments
-
Alex Schneider over 1 year
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.-
Giacomo1968 over 6 years
~/.ssh
is in your home directory. So have you opened a terminal, typedls -la
and just looked?
-
-
Alex Schneider over 6 yearsI 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?
-
baelx over 6 years@AlexSchneider Added a section above about pasting in your old keys.
-
SCH about 4 yearsWhen 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.
-
Giacomo1968 over 3 years@nnyby Assume you solve your issue, but just go into the and type
mkdir ~/.ssh
and thenchmod 700 ~/.ssh
. This is detailed in the answer as well.