3

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?

4
  • Does the directory /home/user2/.ssh/ exist? Jun 27, 2018 at 7:26
  • 1
    Why do you call it with sudo in the first place?
    – gronostaj
    Jun 27, 2018 at 7:26
  • @KamilMaciorowski I thought that .ssh would get created automatically?
    – pd441
    Jun 27, 2018 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
    – pd441
    Jun 27, 2018 at 7:31

2 Answers 2

2

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.

2
  • 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?
    – pd441
    Jun 27, 2018 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). Jun 27, 2018 at 9:53
0

For me the reason for this was an existing key in the same location in which the keygen tries to create the key. Deleting the key via

 sudo rm -rf /home/ec2-user/.ssh

and re-running the same command did the trick.

P.S. I am working on a aws host. you can try doing

sudo rm -rf ~/.ssh
3
  • Sorry but this is wrong. Firstly: a user owns every single file and directory under /home/user. There's no need to sudo to remove files / folders. Secondly: this will remove user's entire .ssh-directory, not recommended and unnecessary to resolve the issue. Thirdly: the accepted answer is correct. As one is creating ssh keys for themselves, it can be done with user's own privileges. sudoing will cause the keys to be written in root's home folder where the user has no access to them anyway. I'd suggest either deleting this or explaining why such a drastic measure would be necessary. Feb 11, 2023 at 14:06
  • Oh, so does deleting an existing id_rsa file under this directory work? Feb 12, 2023 at 5:28
  • The specific problem in OP:s question was simply sudo. Creating a personal key pair, you simply give the command without it. There's not necessarily a need to delete anything, asssh-keygen will in the beginning ask for the filename, which by default is ~/.ssh/id_rsa. If the user doesn't give a new filename, the existing one's simply overwritten. As key pairs are personal, they should be created by the user. For user1 to create a key pair for user 2, they can su user2, and the key will be saved as /home/user2/.ssh/id_rsa and user2 can use it. The given answer is correct and complete. Feb 12, 2023 at 12:20

You must log in to answer this question.

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