Search code examples
bashsshenvironment-variableszshssh-keygen

Why -f flag of ssh-keygen fails to follow symlink?


My ~/.ssh directory is a symlink to /somewhere/else/.ssh/

Now, the following works perfectly; and the demo key ends up getting created at /somewhere/else/.ssh/Official, as expected.

export NewUser="Sam"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ~/.ssh/Official/demo

However, when -f is supplied with the same path but via a variable, it fails with the following error: Saving key "~/.ssh/Official/demo" failed: No such file or directory

export NewUser="Darsh"
export SSHKey_Path="~/.ssh/Official/demo"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ${SSHKey_Path}

I have tried several ways to supply this variable, but nothing worked. I'm not able to find anything about variables in the documentation either. I wish to know why does -f fail to follow the symlink path ONLY if passed via a variable? Is there a workaround? I'm not sure but, would it be recommended to bring this to notice here?



EDIT: Updating question after debugging. The original question has been preserved below:

I am aware that ssh-keygen has a flag -f to specify input_keyfile - With which, once can create a key with custom name at a custom location. However, this fails if the input_keyfile is a variable.
How do I provide the key path as a variable to ssh-keygen?

Following are oversimplified snippets from the bigger code:
This works perfectly:

export NewUser="Sam"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ~/.ssh/Official/demo

However, this fails with the following error Saving key "~/.ssh/Official/demo" failed: No such file or directory

export NewUser="Darsh"
export SSHKey_Path="~/.ssh/Official/demo"
ssh-keygen -N "" -t rsa -b 8192 -C "Login Key of ${NewUser}." -f ${SSHKey_Path}

I have tried wrapping ${SSHKey_Path} in several ways, but nothing worked:
", ', $(echo ${SSHKey_Path}), and many more.


Solution

  • The failure is not in the variable, but in the interpretation of ~.

    Try

    export SSHKey_Path=~/.ssh/Official/demo
    

    or

    export SSHKey_Path="$HOME/.ssh/Official/demo"