cusX
cusX

Reputation: 500

ssh-keygen: The System Cannot Find the Path Specified

EDIT:

It turns out that the error is due to the fact that ssh-keygen in PowerShell is running the ssh-keygen.bat file instead of ssh-keygen.exe

So setting an alias as mention below is the way to go.

I use Set-Alias instead of New-Alias because New-Alias requires me to reset my $profile every now and then, which is weird.

To reset, type . $profile

Upvotes: 2

Views: 15364

Answers (3)

Chetna Priyadarshini
Chetna Priyadarshini

Reputation: 217

I solved the problem by generating my own ssh key pair using command

$ ssh-keygen -t rsa -C "[email protected]"

in powershell. If you already have a SSH key, then don't a generate new key, as they will be overwritten. You can use ssh-keygen command, only if you have installed Git with Git Bash.

When you run the above command, it will create 2 files in the ~/.ssh directory.

~/.ssh/id_rsa − It is private key or identification key.

~/.ssh/id_rsa.pub − public key

See link generate ssh key

Upvotes: 0

wullxz
wullxz

Reputation: 19540

Actually, you can add single executables from the git bin folder to your path by symlinking them into a folder that is contained in your path variable.
I also wanted to use some of the git tools inside powershell but I didn't want to load the git-bin-folder into path every time I wanted to use one of the tools and I also didn't want to overwrite tools like cp, find, ls and so on...

  • Create a folder that you can add to path. I created mine in C:\dev\bin and added it to the path variable.
  • Then you need to symlink all the dll-files from the git bin folder to your new bin folder because the tools you'll link need them in the same directory (the runpath won't be the git-bin-dir but the new bin-dir). I'll add a Script to do that below.
  • And finally you just need to create symlinks for all tools you'd like to use in powershell as well (hint: ssh.exe is really nice to use in powershell ;))

Here are my powershell functions that'll help you set up your new bin-folder with all the nice tools from git:

# generic symlink function
function Create-Symlink {
    param(
        [string]$link,
        [string]$target
    )

    & cmd.exe /c mklink "$link" "$target"
}

# symlink all teh git dlls
function Create-GitDllSymlinks {
    param(
        [string]$newBinDir='C:\dev\bin',
        [string]$gitBinDir='C:\Program Files (x86)\Git\bin'
    )

    $dlls = gci $gitBinDir -Filter *.dll

    $dlls | foreach {
        Create-Symlink -link (join-path $newBinDir $_.Name) -target $_.FullName
    }
}

# to easily link your git tools
function Create-GitSymlink {
    param(
        [string]$executable,
        [string]$newBinDir='C:\dev\bin',
        [string]$gitBinDir='C:\Program Files (x86)\Git\bin'
    )

    if (-not $executable.EndsWith('.exe')) {
        $executable = ($executable + '.exe')
    }

    Create-Symlink -link (join-path $newBinDir $executable) -target (join-path $gitBinDir $executable)
}

# create all dll symlinks needed
Create-GitDllSymlinks
# link ssh and ssh-keygen to use in powershell
Create-GitSymlink -executable ssh-keygen
Create-GitSymlink -executable ssh

Another nice thing to do is this:

  • create a bash.bat file in your new bin-folder
  • write this into that .bat file:

    "C:\Program Files (x86)\Git\bin\sh.exe" --login -i

  • quickly switch between powershell and bash:

    • when in powershell, type bash and git-bash will start inside the same window with access to all git tools in the git-bin-folder.
    • want to go back to powershell? just type exit!

Edit:
I reinstalled my computer a few days ago (with Win 10) and ran into some issues with my solution above. Apparently, git and/or the cygwin environment in git has been updated and the cygwin applications now search for their unix-environment-paths differently.

For example, ssh always said that it couldn't find my home path:

Could not create directory '/home/myuser/.ssh'.
The authenticity of host 'hostx (IP)' can't be established.
ECDSA key fingerprint is SHA256:xxxxxx.
Are you sure you want to continue connecting (yes/no)?

Using procmon I found out, that it was looking for files in c:\etc and c:\home\myuser. Setting environment variables like HOME or USERPROFILE didn't work, so I just made two additional symlinks to satisfy my cygwin tools:

PS bin # cmd /c mklink /D C:\etc "C:\Program Files\Git\etc"
symbolic link created for C:\etc <<===>> C:\Program Files\Git\etc
PS bin # cmd /c mklink /D C:\home "C:\Users"
symbolic link created for C:\home <<===>> C:\Users

Upvotes: 1

nehcsivart
nehcsivart

Reputation: 753

One way to do this is to add it to your path.

Unfortunately, there appears to be no way to add only the executable. In other words, you will be required to add the whole bin folder (namely C:\Program Files\Git\usr\bin) to your path.

I can show you how to add the whole directory to the path, but I don't think this is what you want. Instead, below is an alternative method that only adds the ssh-keygen.exe executable.

  1. Navigate to $env:homepath\Documents\WindowsPowerShell (create it if you don't have it).
  2. Create a file called profile.ps1.
  3. Add to the file the following line of code.
    New-Alias Ssh-Keygen "C:\Program Files\Git\usr\bin\ssh-keygen.exe"

Now, each time you launch Powershell, Ssh-Keygen will be available. It even works with tab completion (e.g. type ssh- and press tab, then it automatically becomes Ssh-Keygen).

Upvotes: 5

Related Questions