cmder icon indicating copy to clipboard operation
cmder copied to clipboard

Question: How do I change the route cmder has to find ssh keys?

Open DarioArDi opened this issue 1 year ago • 25 comments

Question

Right now it searches in "~/.ssh "but i would like to put a relative path to not have to copy my keys in the home directory of every computer that I use instead of geting them from the external drive. I have searched everywhere, I tried looking at user_profile.cmd, start-ssh-agent.cmd and init.bat.

Checklist

  • [X] I have read the documentation and made sure what I'm looking for isn't present, or is unclear.
  • [X] I have searched for similar issues, and either this question wasn't asked before, or I didn't find any that describe my question.

DarioArDi avatar Mar 19 '24 19:03 DarioArDi

By asking Claude 3 (AI) this are the steps.

  1. Go to Settings...
  2. Go to Startup -> Tasks
  3. In {bash::bash} (the one that I am using) I added in the commands

set HOME=%CMDER_ROOT%.ssh set SSH_USER_HOME=%CMDER_ROOT%.ssh

I added a .ssh folder to the cmder root with the keys.

DarioArDi avatar Apr 11 '24 16:04 DarioArDi

Yeah I would not do it that way.

Cmder has init scripts you can add that stuff to in your case it would be $CMDER_ROOT/config/user_profile.sh

Add:

export SSH_USER_HOME="$CMDER_ROOT/.ssh"

or whatever else you need to configure in your env.

You could also look at $CMDER_ROOT/config/profile.d and just drop a *.sh|cmd|ps1 file to customize bash|cmd|or powershell sessions.

daxgames avatar Apr 11 '24 17:04 daxgames

Yeah the AI made up a bad answer, LOL.

chrisant996 avatar Apr 11 '24 17:04 chrisant996

Yeah I would not do it that way.

Cmder has init scripts you can add that stuff to in your case it would be $CMDER_ROOT/config/user_profile.sh

Add:

export SSH_USER_HOME="$CMDER_ROOT/.ssh"

or whatever else you need to configure in your env.

You could also look at $CMDER_ROOT/config/profile.d and just drop a *.sh|cmd|ps1 file to customize bash|cmd|or powershell sessions.

It does not recognise export as a command.

Yeah, I did not test it well and in fact, it does not work

DarioArDi avatar Apr 11 '24 20:04 DarioArDi

If you edited $CMDER_ROOT/config/user_profile.sh and are using bash::bash then export is indeed a valid command.

daxgames avatar Apr 11 '24 23:04 daxgames

apparently i am using cmd::Cmder

DarioArDi avatar Apr 12 '24 23:04 DarioArDi

For cmd::cmder edit %CMDER_ROOT%/config/user_profile.cmd and use set instead of export.

You can also do the same for PowerShell::PowerShell by editing $env:CMDER_ROOT\config\user_profile.ps1.

$env:SSH_USER_HOME = "$env:CMDER_ROOT\.ssh"

daxgames avatar Apr 12 '24 23:04 daxgames

I tried that, and it does not give me an error message, but it wont add the keys. I checked start-ssh-agent.cmd and SSH_USER_HOME does not appear anywhere. I can't find what to change in there to set the path.

DarioArDi avatar Apr 13 '24 11:04 DarioArDi

I never said what you were trying to do would actually work. I have never even considered moving my ~/.ssh folder as it is against well established convention.

I was just trying to tell you how to properly set env variables for various shells supported by Cmder.

You may have to make a copy of the script you mentioned and edit it to do what you need.

daxgames avatar Apr 13 '24 12:04 daxgames

Ok, I need this to work in orther to have a portable working git with ssh keys so I don't have to copy my keys everywhere.

DarioArDi avatar Apr 13 '24 18:04 DarioArDi

Wouldn't using the ssh agent be useful here?

Other than that, maybe editing the ssh\ssh_config file in the vendor\git-for-windows and setting the IdentityFile variables to be a custom path can help you, such as ${CMDER_ROOT}/config/.ssh/... or something like that.

Doing a quick search it seems that %SSH_USER_HOME% is not associated with what you want to do, other people suggest changing the whole %HOME% variable to point to somewhere (like CMDER_ROOT\config) and putting your .ssh folder there.

DRSDavidSoft avatar May 07 '24 03:05 DRSDavidSoft

I was able to make it work, thank you @DRSDavidSoft.

i edited the line in init.bat that defines HOME and that did the trick as you pointed out.

From: if not defined HOME set "HOME=%USERPROFILE%"

To: if not defined HOME set "HOME=%CMDER_ROOT%\config"

DarioArDi avatar Aug 30 '24 19:08 DarioArDi

[!CAUTION] The HOME environment variable is used by more than just ssh.

Setting HOME to point into the Cmder config directory will break some other things.

For example, it will make ~ syntax paths point into the Cmder config directory, instead of into the user directory.

So, setting HOME is probably not the best way to resolve the problem.

chrisant996 avatar Aug 30 '24 20:08 chrisant996

@chrisant996 Exactly, to my knowledge Windows doesn't provide a way to set the environment variables for a specific task. Best alternative is to alias ssh to a batch script that would temporarily set HOME to the desired path, and revert it back upon ssh exit. I'm not even sure it would always work, might not be reliable at all.

Personally I always make sure that HOME == USERPROFILE.

Msys2 apparently takes care of Windows/Unix path as well.

DRSDavidSoft avatar Aug 30 '24 22:08 DRSDavidSoft

well, then there is no easy solution to that? maybe i could set the HOME variable at the start of the ssh agent script and set it back how it was at the end?

DarioArDi avatar Aug 30 '24 22:08 DarioArDi

Symlink %cmder_root%\config\.ssh to %userprofile%\.ssh and put ssh config and keys in %cmder_root%\config\.ssh

Just a thought. I have not tested it.

daxgames avatar Aug 30 '24 23:08 daxgames

sadly I don't have symlink (i am working with windows), i tried with mklink /D %cmder_root%\config\.ssh %userprofile%\.ssh and it doesn't work either, it says that can't create a file that already exists. This is the first time that I use this command so i may be using it incorrectly. Maybe this aproach doesn't work because they would be the "same" folder?

DarioArDi avatar Aug 30 '24 23:08 DarioArDi

Below shows symlinks on Windows:

image

  1. You need to enable Developer Mode in Control Panel or be an administator.

    image

  2. Add the below to use native windows symlinks using the ln command.

  • set MSYS=winsymlinks:nativestict in %cmder_root%\config\user_profile.cmd
  • export MSYS=winsymlinks:nativestict in %cmder_root%\config\user_profile.sh
  • $env.MSYS=winsymlinks:nativestict in %cmder_root%\config\user_profile.ps1
  1. Create %cmder_root%\config\.ssh.
  2. Add your config and keys to the folder.
  3. Create the symlink:
  • ln -sf %cmder_root%/config/.ssh %userprofile%/.ssh
  1. try and see if it works.

See below creating the symlink in the Cmder Cmd.exe shell:

image

Note: I do not know if this will work but the symlink can be created.

daxgames avatar Aug 30 '24 23:08 daxgames

sadly I don't have symlink (i am working with windows), i tried with mklink /D %cmder_root%\config\.ssh %userprofile%\.ssh and it doesn't work either, it says that can't create a file that already exists. This is the first time that I use this command so i may be using it incorrectly. Maybe this aproach doesn't work because they would be the "same" folder?

You need to delete the folder before trying to create the symlink.

daxgames avatar Aug 31 '24 00:08 daxgames

essentially this would make the files present in both locations. editing/adding files to one location would edit/add in both locations.

daxgames avatar Aug 31 '24 00:08 daxgames

That's the thing, my objective is to have cmder in a external drive so i can use it wherever without having to copy my ssh keys in every computer. I did not spect for this to be so conversome haha. Could i just edit start-ssh-agent.cmd to look where i want instead of HOME? or whichever the script that looks for the keys is

DarioArDi avatar Aug 31 '24 08:08 DarioArDi

It does work, If it works for you:

image

daxgames avatar Sep 01 '24 12:09 daxgames

@DarioArDi The only thing I know of is using a ~/.ssh/config file and use the IdentityFile key to point to where your key is as shown below:

https://stackoverflow.com/questions/84096/setting-the-default-ssh-key-location

daxgames avatar Sep 01 '24 18:09 daxgames

@DarioArDi You could also write some code in your %cmder_root%/config/user_profile.* init scripts that automatically copies keys stored in your Cmder folder on USB to %userprofile%\.ssh if they do not exist. So when you start Cmder whatever machine you run it on is automatically configred.

daxgames avatar Sep 01 '24 18:09 daxgames

That would make it work, but again, i don't want to leave copies of my keys everywhere. Im going to try the IdentityFile and see if i can check the config to change it if it isin't configured.

DarioArDi avatar Sep 01 '24 19:09 DarioArDi