Create a ssh key:

$ ssh-keygen -t rsa –P ""

Moving the key to authorized key:

$ cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys

bash: /home/user/.ssh/authorized_keys: No such file or directory
  • Same question, why you need to add the key to your own host? – Braiam May 16 '14 at 1:28
  • @Braiam I've done this when my home directory was on network drive. This allowed me to ssh to any machine that mounted this drive. So I wasn't sshing to my own host. – FDinoff May 16 '14 at 2:20
bash: /home/user/.ssh/authorized_keys: No such file or directory

An element of the path /home/user/.ssh/ does not exist; the shell (bash) checks the redirection (>>) before it executes the command (cat). If /home/user does exist and you try this:

cat $HOME/.ssh/id_rsa.pub >> $HOME/test_this

You'll now get:

cat: /home/user/.ssh/id_rsa.pub: No such file or directory

Since that file can't be in that place, if /home/user/.ssh/ did not exist to start with.

You're following some instructions having possibly skipped some bits. To create the directory:

mkdir $HOME/.ssh
chmod 700 $HOME/.ssh

However, id_rsa.pub won't be there. It will be wherever you ran the ssh-keygen command in the first place.

| improve this answer | |

Make sure that you are logged in as the user that you want to create the ssh key for first (or be prepared to modify the paths in the command to the correct home directory).

Then just create the directory:

mkdir ~/.ssh

Create the file using the cat command you have listed above. Then don't forget to set the permissions correctly:

chmod 400 ~/.ssh/authorized_keys

You might want to make sure that the .ssh directory has appropriate permissions as well.

| improve this answer | |
  • 2
    As your last comment implies, yes I find you need to chmod 700 ~/.ssh – KCD Dec 8 '14 at 5:50

If possible I always encourage people to use the commandline tool ssh-copy-id.

$ ssh-copy-id -h
Usage: /usr/bin/ssh-copy-id [-i [identity_file]] [user@]machine

If you just want to copy your default SSH public key to a remote sever you simply run the following command:

$ ssh-copy-id user@remoteserver

I show an actual full example of how to use this tool in this Q&A titled: Can't share an ssh connection with rsync.

| improve this answer | |

This can also fail if, when creating the key with ssh-keygen, you give it a filename. I entered some name my-ssh-file-name, and it wrote the key to /Users/MyUserName instead of the .ssh folder. If you leave the filename blank, it will write to .ssh as expected.

| improve this answer | |
  • or of course you can specify to write the custom key to .ssh folder directly: -f ~/.ssh/custom-key-name – parliament Jan 23 '16 at 22:53

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.