Skip to content

Instantly share code, notes, and snippets.

Last active October 10, 2022 19:29
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
This enables portable SSH keys in cmder, enabling you to have full SSH access using cmder portably on a USB (for example). Please note that when you ssh-keygen, it will still default to %USERPROFILE%, so you will either have to change the path on generation, or manually move the keys to your %CMDER_ROOT%\config\.ssh directory.
:: use this file to run your own startup commands
:: use in front of the command to prevent printing the command
:: call "%GIT_INSTALL_ROOT%/cmd/start-ssh-agent.cmd"
:: set "PATH=%CMDER_ROOT%\vendor\whatever;%PATH%"
:: LS 2017-09-12_17.35.11
:: ======================
:: Copied from %CMDER_ROOT%\vendor\init.bat
:: Then changed line 61 following line from:
:: Find root dir
if not defined CMDER_ROOT (
for /f "delims=" %%i in ("%ConEmuDir%\..\..") do set "CMDER_ROOT=%%~fi"
:: Remove trailing '\'
if "%CMDER_ROOT:~-1%" == "\" SET "CMDER_ROOT=%CMDER_ROOT:~0,-1%"
:: LS 2017-09-12_17.31.42
:: ======================
:: Copied in its entirety from %CMDER_ROOT%\vendor\git-for-windows\cmd\start-ssh-agent.cmd
:: Then changed line 61 following line from:
:: to:
:: @SET "HOME=%CMDER_ROOT%\config"
@REM Do not use "echo off" to not affect any child calls.
@REM Enable extensions, the `verify` call is a trick from the setlocal help
@VERIFY other 2>nul
@SETLOCAL EnableDelayedExpansion
@ECHO Unable to enable extensions
@GOTO failure
@REM Start the ssh-agent if needed by git
@FOR %%i IN ("git.exe") DO @SET GIT=%%~$PATH:i
@IF EXIST "%GIT%" @(
@REM Get the ssh-agent executable
@FOR %%i IN ("ssh-agent.exe") DO @SET SSH_AGENT=%%~$PATH:i
@FOR %%s IN ("%GIT%") DO @SET GIT_DIR=%%~dps
@FOR %%s IN ("!GIT_DIR!") DO @SET GIT_DIR=!GIT_DIR:~0,-1!
@FOR %%s IN ("!GIT_DIR!") DO @SET GIT_ROOT=%%~dps
@FOR /D %%s in ("!GIT_ROOT!\usr\bin\ssh-agent.exe") DO @SET SSH_AGENT=%%~s
@IF NOT EXIST "!SSH_AGENT!" @GOTO ssh-agent-done
@REM Get the ssh-add executable
@FOR %%s IN ("!SSH_AGENT!") DO @SET BIN_DIR=%%~dps
@FOR %%s in ("!BIN_DIR!") DO @SET BIN_DIR=!BIN_DIR:~0,-1!
@FOR /D %%s in ("!BIN_DIR!\ssh-add.exe") DO @SET SSH_ADD=%%~s
@IF NOT EXIST "!SSH_ADD!" @GOTO ssh-agent-done
@REM Check if the agent is running
@FOR /f "tokens=1-2" %%a IN ('tasklist /fi "imagename eq ssh-agent.exe"') DO @(
@ECHO %%b | @FINDSTR /r /c:"[0-9][0-9]*" > NUL
@IF "!ERRORLEVEL!" == "0" @(
) else @(
@REM Unset in the case a user kills the agent while a session is open
@REM needed to remove the old files and prevent a false message
@REM Connect up the current ssh-agent
@IF [!SSH_AGENT_PID!] == [] @(
@ECHO Removing old ssh-agent sockets
@FOR /d %%d IN (%TEMP%\ssh-??????*) DO @RMDIR /s /q %%d
) ELSE @(
@ECHO Found ssh-agent at !SSH_AGENT_PID!
@FOR /d %%d IN (%TEMP%\ssh-??????*) DO @(
@FOR %%f IN (%%d\agent.*) DO @(
@IF NOT [!SSH_AUTH_SOCK!] == [] @(
@ECHO Found ssh-agent socket at !SSH_AUTH_SOCK!
) ELSE (
@ECHO Failed to find ssh-agent socket
@REM See if we have the key
@"!SSH_ADD!" -l 1>NUL 2>NUL
@IF NOT !result! == 0 @(
@IF !result! == 2 @(
@ECHO | @SET /p=Starting ssh-agent:
@FOR /f "tokens=1-2 delims==;" %%a IN ('"!SSH_AGENT!"') DO @(
@IF NOT [%%b] == [] @SET %%a=%%b
@ECHO. done
@ECHO %cmdcmdline% | @FINDSTR /l "\"\"" >NUL
@CALL cmd %*
Copy link

Nilpo commented Sep 16, 2018

Better yet. Add a symbolic link so that you don't need your ssh keys in two different places.

ln -s %USERPROFILE%\.ssh %CMDER_ROOT%\config\.ssh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment