112

I want to run a command like:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

My understanding is that ssh-keygen outputs to the home directory. I'm working on a networked computer using Git Bash (Windows, MYSS MINGW64) where the home directory is one I don't have access to. I change the home directory like so:

export HOME=C:/Users/myusername

so now when I enter:

echo $HOME

it says:

C:/Users/myusername

but when I again try to run the ssh-keygen command it runs in the directory that I don't have access to. I've looked through the profile file for some hard coded path but can't find anything. How do I change it to point to a directory that I do have access to?

1
  • This same behavior happens on Ubuntu (changing HOME doesn't affect the default directory for ssh-keygen). Though specifying the directory explicitly works, I'd be interested to know why ssh-keygen doesn't just use $HOME/.ssh as HOME is currently defined.
    – Nathan
    Jun 15, 2019 at 19:13

3 Answers 3

183

You should be able to do this by specifying the name of the output file with the -f option, e.g.,

ssh-keygen -t rsa -b 4096 -C "[email protected]" -f $HOME/.ssh/id_rsa

Of course, it really helps if the output directory has already been created.

For further reading:

1
  • Much obliged. I overlooked the -f option. I just created a new folder with mkdir .ssh in my HOME and ran the command you said. Thank you very much. Nov 24, 2015 at 9:09
0

On Windows 10:

From the accepted answer, $HOME did not work for me for the path, but %userprofile% does:

ssh-keygen -f %userprofile%/.ssh/id_rsa
1
  • When using the -f in Windows, $HOME does not work from the above answer but %userprofile% does work.
    – tzg
    Feb 22, 2023 at 17:28
0

For me you should use this option -f like this :

ssh-keygen -t rsa -b 2048 -f /home/user/project/ssh-keys/id_ras

And finally sh-keygen generate two file private and public into this directory

/home/user/project/ssh-keys/id_ras
/home/user/project/ssh-keys/id_ras.pub

You must log in to answer this question.

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