Saving SSH key in home instead of root

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?

steve zissou

Posted 2018-06-27T07:19:23.973

Reputation: 113

Does the directory /home/user2/.ssh/ exist? – Kamil Maciorowski – 2018-06-27T07:26:08.297

1Why do you call it with sudo in the first place? – gronostaj – 2018-06-27T07:26:48.683

@KamilMaciorowski I thought that .ssh would get created automatically? – steve zissou – 2018-06-27T07:30:51.803

@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 – 2018-06-27T07:31:36.160

Answers

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.

user1686

Posted 2018-06-27T07:19:23.973

Reputation: 283 655

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 – 2018-06-27T09:51:16.703

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). – user1686 – 2018-06-27T09:53:58.093