1

When I try to create an SSH key using sudo ssh-keygen -t rsa I get the request:

Enter file in which to save the key (/root/.ssh/id_rsa):

However, I would like the file to be saved in /home/user2/.ssh/id_rsa. When I enter that directory I get this error:

Saving key "/home/user2/.ssh/id_rsa" failed: No such file or directory

Any attempt to enter a different directory returns an error.

How can I generate the key in /home/user2/.ssh/id_rsa?

EDIT: when I remove sudo I get this problem:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/hduser/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Saving key "/home/hduser/.ssh/id_rsa" failed: Not a directory

But shouldn't SSH create the directory?

  • Does the directory /home/user2/.ssh/ exist? – Kamil Maciorowski Jun 27 '18 at 7:26
  • 1
    Why do you call it with sudo in the first place? – gronostaj Jun 27 '18 at 7:26
  • @KamilMaciorowski I thought that .ssh would get created automatically? – steve zissou Jun 27 '18 at 7:30
  • @gronostaj I'm not sure, i got it from a guide, I'll edit my question to give the output whenever I don't use sudo – steve zissou Jun 27 '18 at 7:31
1

When I try to create an SSH key using sudo ssh-keygen -t rsa I get the request:

Enter file in which to save the key (/root/.ssh/id_rsa):

By using sudo you ask to run the program under another account, specifically root. So the keygen will run as root, create a key belonging to root, and of course it will use root's home directory to store that key.

To create files belonging to yourself (and to use your own paths, etc.) just don't use sudo.

And if not using sudo leads to weird permission errors, better investigate and fix those – they won't quietly fix themselves if you just sudo the whole thing; often that'll just make it worse.

when I remove sudo I get this problem:

Generating public/private rsa key pair.
Enter file in which to save the key (/home/hduser/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Saving key "/home/hduser/.ssh/id_rsa" failed: Not a directory

But shouldn't SSH create the directory?

It should. But what the error message really means is that /home/hduser/.ssh already exists – it's just something else than a directory. It might be a file, a broken symlink, a symlink to a file, etc.

Again: investigate the situation. To see what .ssh actually is, run ls -ld on it. If necessary, rename the file/link/item or completely delete it.

  • Thanks! It worked when I removed the file. btw how does ls -ld tell me what the file actually is. doesn't the . in front of the filename tell me that its a hidden directory? – steve zissou Jun 27 '18 at 9:51
  • No. The . in front of the filename only tells you that it's a hidden something, but does not in any way imply it's a directory. The actual item type is the 1st character of ls -l output (before permission indicators). – grawity Jun 27 '18 at 9:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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