├── README.md ├── old ├── install ├── start-hidden.vbs └── start-ssh-agent.bat └── start-ssh-agent /README.md: -------------------------------------------------------------------------------- 1 | ssh-agent on Bash on Ubuntu on Windows 2 | ====================================== 3 | 4 | Scripts to use to persist `ssh-agent` on Bash on Windows on Ubuntu 5 | 6 | How To 7 | ------- 8 | 9 | Read the blog post here 10 | 11 | http://daveeddy.com/2017/10/18/persistent-sshagent-on-bash-on-ubuntu-on-windows/ 12 | 13 | Install 14 | ------- 15 | 16 | Put the scripts into place 17 | 18 | mkdir -p ~/bin 19 | cp -vf ./start-ssh-agent ~/bin 20 | 21 | Create a hidden scheduled task on Windows to then start this script at login 22 | 23 | powershell -noprofile -windowstyle hidden -command "c:\windows\system32\bash.exe -c "~/bin/start-ssh-agent"" 24 | 25 | 26 | Deprecated Process 27 | ------------------ 28 | 29 | **NOTE:** This is the old way of installing this - the new process is explained 30 | in the blog post and way more simple. 31 | 32 | The above blog post illustrates all of the steps required to make this work. 33 | You can run the included `install` script to put the script files into place. 34 | 35 | 36 | ``` 37 | $ cd old 38 | $ ./install 39 | mkdir: created directory '/home/dave/bin' 40 | '/home/dave/userprofile' -> '/mnt/c/Users/dave' 41 | mkdir: created directory '/home/dave/userprofile/Documents/scripts' 42 | '/home/dave/userprofile/Documents/scripts/start-hidden.vbs' -> '/home/dave/dev/windows-bash-ssh-agent/start-hidden.vbs' 43 | '/home/dave/userprofile/Documents/scripts/start-ssh-agent.bat' -> '/home/dave/dev/windows-bash-ssh-agent/start-ssh-agent.bat' 44 | '/home/dave/bin/start-ssh-agent' -> '/home/dave/dev/windows-bash-ssh-agent/start-ssh-agent' 45 | ``` 46 | 47 | The Windows username can be supplied as the first argument - `$USER` is assumed. 48 | 49 | License 50 | ------- 51 | 52 | MIT License 53 | -------------------------------------------------------------------------------- /old/install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Very opinionated install script for persistent bash on ubuntu on windows 4 | # ssh-agent 5 | # http://daveeddy.com/2017/10/18/persistent-sshagent-on-bash-on-ubuntu-on-windows/ 6 | # Author: Dave Eddy 7 | # Date: October 30, 2017 8 | # License: MIT 9 | 10 | user=${1:-$USER} 11 | winhome=/mnt/c/Users/$user 12 | scriptsdir=~/userprofile/Documents/scripts 13 | 14 | if [[ ! -d "$winhome" ]]; then 15 | echo "error: $winhome not a directory" >&2 16 | echo " - Windows user '$user' not found" 17 | echo " - Try passing the Windows user as the first argument" 18 | exit 1 19 | fi 20 | 21 | mkdir -pv ~/bin 22 | ln -svf "$winhome" ~/userprofile 23 | mkdir -pv "$scriptsdir" 24 | 25 | cp -vf "$PWD/start-hidden.vbs" "$scriptsdir/" 26 | cp -vf "$PWD/start-ssh-agent.bat" "$scriptsdir/" 27 | cp -vf "$PWD/../start-ssh-agent" ~/bin 28 | -------------------------------------------------------------------------------- /old/start-hidden.vbs: -------------------------------------------------------------------------------- 1 | Dim WinScriptHost 2 | Set WinScriptHost = CreateObject("WScript.Shell") 3 | WinScriptHost.Run Chr(34) & "%userprofile%\Documents\scripts\start-ssh-agent.bat" & Chr(34), 0 4 | Set WinScriptHost = Nothing 5 | -------------------------------------------------------------------------------- /old/start-ssh-agent.bat: -------------------------------------------------------------------------------- 1 | \Windows\System32\bash.exe -c "~/bin/start-ssh-agent" 2 | -------------------------------------------------------------------------------- /start-ssh-agent: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Start ssh-agent if it is not running (by becoming it) 4 | # Intended for use on Bash for Windows using the WSL 5 | # 6 | # Author: Dave Eddy 7 | # Date: October 09, 2017 8 | # License: MIT 9 | 10 | # Could be any file - nothing intrinsically valuable about ~/.ssh/environment 11 | envfile=~/.ssh/environment 12 | 13 | # Ensure the environment file exists and has its permissions properly set. 14 | # Source the file - if it was created by this script the source will 15 | # effectively be a noop. 16 | mkdir -p "${envfile%/*}" 17 | touch "$envfile" 18 | chmod 600 "$envfile" 19 | . "$envfile" 20 | 21 | # Check if the daemon is already running 22 | if [[ -n $SSH_AGENT_PID ]] && kill -0 "$SSH_AGENT_PID" 2>/dev/null; then 23 | # The PID is up but it could have been recycled - attempt to list keys. 24 | # This will exit with 2 if the SSH_AUTH_SOCK is broken. 25 | ssh-add -l &>/dev/null 26 | if (($? != 2)); then 27 | echo "alreading running: $SSH_AGENT_PID" 28 | exit 1 29 | fi 30 | fi 31 | 32 | # Overwrite what is in the envfile to start a fresh ssh-agent instance 33 | echo "# Started $(date)" > "$envfile" 34 | 35 | # For some reason, this line doesn't get emitted by ssh-agent when it is run 36 | # with -d or -D. Since we are starting the program with exec we already know 37 | # the pid ahead of time though so we can create this line manually 38 | echo "SSH_AGENT_PID=$$; export SSH_AGENT_PID" >> "$envfile" 39 | 40 | # Become ssh-agent and run forever 41 | exec ssh-agent -D >> "$envfile" 42 | --------------------------------------------------------------------------------