57

Create a ssh key:
Command:

ssh-keygen -t rsa -P ""

Moving the key to authorized key:
Command:

cat $HOME/.ssh/id_rsa.pub >> $HOME/.ssh/authorized_keys
bash: /home/user/.ssh/authorized_keys: No such file or directory
4
  • 2
    ~/.ssh/authorized_keys are keys of other computers that you connected to/trust, not your own key. May 15, 2014 at 19:58
  • Some applications require passwordless ssh to one's own machine. May 15, 2014 at 20:03
  • Please don't post the same question on multiple sites of the Stack Exchange network.
    – terdon
    May 16, 2014 at 0:43
  • Why you need to trust the key you own? Exactly what are you trying to do?
    – Braiam
    May 16, 2014 at 1:27

5 Answers 5

113

You have to create the .ssh directory and the authorized_keys file the first time.

  1. Create the .ssh directory:

    mkdir ~/.ssh
    
  2. Set the right permissions:

    chmod 700 ~/.ssh
    
  3. Create the authorized_keys file:

    touch ~/.ssh/authorized_keys
    
  4. Set the right permissions:

    chmod 600 ~/.ssh/authorized_keys
    

The permissions are important! It won't work without the right permissions!

Now you can add the public key to the authorized_keys file:

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

You have to add the public key of your computer to the authorized_keys file of the computer you want to access using SSH Keys!


As terdon mentioned you can also just use this command:

ssh-copy-id user@host

This will put your id_rsa.pub (in your ~/.ssh directory) in the remote computer's authorized_keys file, creating the .ssh directory and authorized_keys file with the right permissions if necessary.

4
  • 6
    In any case, you don't want to just cat it, that's what ssh-copy-id is for.
    – terdon
    May 15, 2014 at 23:48
  • 1
    Wow, cool, I've added it. May 16, 2014 at 6:41
  • @LouisMatthijssen I have copied keys to my remote server. How can I confirm that the public key is added in authorized_keys.
    – Aman
    Jan 31, 2019 at 23:39
  • In my case authorzed_keys is named known_hosts. And after chmods it works! thx
    – Brambor
    Jan 13, 2020 at 21:59
4

Since I don't have enough reputation, I'm adding this here. In addition to Louis Matthijssen's answer if you are still not able to login through ssh as a user that you've created, like

ssh username@host

then this may be because of the absence of owner permission that you must add to your /home/username/.ssh folder. I had the same issue and you can give this permission as:

chown -R username:username /home/username/.ssh

This can probably happen simply because you were creating the directory and setting the permissions as root, but not as the username you want to access the server with.

Hope this helps someone.

3

In case you have been sent the public key in an email to install to a remote server:

1) SSH into the server. I used PuTTY on Windows.

2) Setup the key:

mkdir ~/.ssh
chmod 700 ~/.ssh
vi ~/.ssh/authorized_keys

Take care to copy the key exactly and paste it into a new line in the editor window. Verify that it occupies a single line and save.

chmod 600 ~/.ssh/authorized_keys
0

try

 touch $HOME/.ssh/authorized_keys

to create empty file with the rights of that user.

This file is created when you access for the first a remote host with that user.

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .