Skip to content

Instantly share code, notes, and snippets.

@tdonohue
Last active November 19, 2022 03:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tdonohue/82fe377c5d72fb3d85fc7fba93699925 to your computer and use it in GitHub Desktop.
Save tdonohue/82fe377c5d72fb3d85fc7fba93699925 to your computer and use it in GitHub Desktop.
ConsoleZ + PowerShell + Posh-Git + Git for Windows (with SSH Agent Forwarding!)

Integrating ConsoleZ + Powershell + Git for Windows + posh-git (with SSH agent forwarding + Vagrant)

Tested on: Windows 10


This setup includes the following:

  • Using ConsoleZ for your terminal (This is optional. If you don't want it, skip anything having to do with ConsoleZ and just use PowerShell)
  • Running PowerShell behind the scenes
  • Installing Git for Windows + posh-git, to provide git commands (and ssh) to PowerShell
  • Auto-starting posh-git's SSH-Agent for SSH forwarding to remote servers, VMs, Vagrant, etc.
  • Using PuTTY to provide SSH-Agent forwarding to Vagrant for provisioning.

How to:

Install Git for Windows

(TO UPGRADE Git for Windows: Just download the latest and install again.)

Install posh-git for PowerShell

  1. Install 'posh-git' module in PowerShell. (See also instructions at https://github.com/dahlbyk/posh-git)
    • Open PowerShell
    • Make sure Get-ExecutionPolicy returns either RemoteSigned or Unrestricted
      • If not, run: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Confirm
    • Install 'posh-git': PowerShellGet\Install-Module posh-git -Scope CurrentUser
  2. Install 'posh-sshell' for SSL functionality. See also https://github.com/dahlbyk/posh-sshell
    • PowerShellGet\Install-Module posh-sshell -Scope CurrentUser
  3. Create a ~/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1 script, with the following
    # This is for PowerShell to load posh-git module and utilize Git for Windows
    # Inspired by: https://github.com/dahlbyk/posh-git/blob/master/profile.example.ps1
    
    # Add both [Git]/cmd/ and [Git]/usr/bin/ to PATH.
    # This ensures access to git, and all other tools provided by Git for Windows (e.g. ssh)
    # Optionally, you could simply add these to your system's PATH environment variable instead.
    $Env:Path = "$Env:ProgramFiles\Git\cmd" + ";" + "$Env:ProgramFiles\Git\usr\bin" + ";" + $Env:Path
    
    # Remove default Powershell Aliases for any Git Bash tools/scripts we want to use (feel free to add to list)
    # (Otherwise, these default PowerShell aliases are used, even if Git Bash tools are first in PATH)
    Remove-Item Alias:cp
    Remove-Item Alias:mv
    
    # Import the posh-git module (https://github.com/dahlbyk/posh-git) to current window, first via installed posh-git module. 
    # If the module isn't installed, then attempt to load it from the cloned posh-git Git repo.
    $poshGitModule = Get-Module posh-git -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1
    if ($poshGitModule) {
        $poshGitModule | Import-Module
    }
    elseif (Test-Path -LiteralPath ($modulePath = Join-Path (Split-Path $MyInvocation.MyCommand.Path -Parent) (Join-Path src 'posh-git.psd1'))) {
        Import-Module $modulePath
    }
    else {
        throw "Failed to import posh-git."
    }
    
    # Start posh-git's SSH Agent.
    Start-SshAgent -Quiet
    
    # Add default SSH keys (e.g. ~/.ssh/id_rsa)
    Add-SshKey
    # Add non-default keys to SSH Agent (from ~/.ssh/)
    Add-SshKey "$Env:USERPROFILE\.ssh\github_rsa"
    

(TO UPGRADE posh-git: just run Update-Module posh-git in your PowerShell (or ConsoleZ))

Install & Configure ConsoleZ (optional)

NOTE: I'm now using Microsoft Terminal instead!! https://github.com/Microsoft/Terminal

  1. Optionally, download and install ConsoleZ: https://github.com/cbucher/console
    • If you don't want/like ConsoleZ, you can simply use Powershell. As we'll just wrap PowerShell
  2. Just download the Zip, and install/unzip into C:\Program Files\ConsoleZ\ (or similar).
  3. Update ConsoleZ Settings (Edit -> Settings or Alt+S)
    • Console -> Shell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    • Console -> Startup dir: [whatver folder you do most of your development in]
    • Console -> Buffer size: 5000
    • Tabs -> (tab name) -> Colors & Cursor -> Import
      • Download Dark Solarized Color Scheme for ConsoleZ: https://github.com/stevenharman/console2-solarized
         <?xml version="1.0"?>
         <settings>
            <console>
               <colors>
                  <color id="0" r="7" g="54" b="66"/>
                  <color id="1" r="38" g="139" b="210"/>
                  <color id="2" r="133" g="153" b="0"/>
                  <color id="3" r="42" g="161" b="152"/>
                  <color id="4" r="220" g="50" b="47"/>
                  <color id="5" r="211" g="54" b="130"/>
                  <color id="6" r="181" g="137" b="0"/>
                  <color id="7" r="238" g="232" b="213"/>
                  <color id="8" r="42" g="161" b="152"/>
                  <color id="9" r="131" g="148" b="150"/>
                  <color id="10" r="88" g="110" b="117"/>
                  <color id="11" r="147" g="161" b="161"/>
                  <color id="12" r="203" g="75" b="22"/>
                  <color id="13" r="108" g="113" b="196"/>
                  <color id="14" r="101" g="123" b="131"/>
                  <color id="15" r="253" g="246" b="227"/>
               </colors>
            </console>
         </settings>
        
      • And import those settings into ConsoleZ.
  4. Now, when you startup ConsoleZ, you should have a Powershell embedded. You'll also have access to git, ssh commands.

(TO UPGRADE ConsoleZ: Just download and unzip over the current version)

Configure SSH Agent forwarding to remote servers

Obviously, first you need some SSH keys, if you don't already have them. See, for example: https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

You'll notice in the instructions above, we already setup an SSH Agent to startup automatically (via posh-git). So, now we just need to let it have access to our keys for specific remote machines.

For SSH Agent forwarding to remote machines, you'll want to setup a ~/.ssh/config file similar to

# Turn on SSH Agent Forwarding for all EC2 servers
Host ec2-*.compute-1.amazonaws.com
  ForwardAgent yes

# Turn on SSH Agent Forwarding for other servers/hosts
Host [your-hostname-here]
  ForwardAgent yes

Configure SSH Agent forwarding to Vagrant VMs (using posh-git)


⚠️ Unfortunately, the below instructions only seem to work for running vagrant ssh. If you need SSH agent forwarding to also occur during a vagrant up, then you'll need to also do SSH Agent forwarding via PuTTY (see instructions below). See this ticket for more: hashicorp/vagrant#8615


For SSH Agent forwarding to Vagrant VMs, you'll need to ensure the Vagrantfile includes config.ssh.forward_agent = true


✔️ As of Vagrant v2.0.2, the VAGRANT_PREFER_SYSTEM_BIN environment variable may no longer be needed (untested). See this note: hashicorp/vagrant#9143 (comment)


  • The embedded ssh.exe in Vagrant is sometimes flakey when it comes to SSH agent forwarding (or at least I've found it to be so). See for example: hashicorp/vagrant#9101
  • So, you may want to set the following environment variable (see also https://www.vagrantup.com/docs/other/environmental-variables.html#vagrant_prefer_system_bin)
    VAGRANT_PREFER_SYSTEM_BIN=true
    
  • That environment variable ensures that vagrant ssh will use whatever ssh.exe it finds in your system's PATH (instead of the one embedded in Vagrant). Since we added C:\Program Files\Git\usr\bin to our PATH (in the above Powershell config), this means Vagrant should use the ssh.exe that comes with Git for Windows.
  • Test it out by spinning up a VM, vagrant ssh, and then try ssh git@github.com. It should identify you as your GitHub username (assuming your GitHub key was included in the keys you passed to SSH-Agent).

Configure SSH Agent forwarding to Vagrant VMs (via PuTTY)


⚠️ At this time (as of Vagrant v2.0.2), if you need SSH agent forwarding to also occur during a vagrant up, then you'll need to also do SSH Agent forwarding via PuTTY. See this ticket for more: hashicorp/vagrant#8615

Currently, for my environment, I only use PuTTY to support SSH forwarding for Vagrant provisioning (e.g. if Vagrant needs to checkout a private repo in GitHub). If you don't need that, then you may not need to even install or configure PuTTY.


This is based on the instructions here: https://github.com/DSpace/vagrant-dspace/blob/master/docs/WINDOWS_INSTRUCTIONS.md#step-4-set-putty-pageant-for-ssh-forwarding

  • Install PuTTY
  • This will install PuTTY and all its utilities to your Program Files under a PuTTY folder. Pageant (pageant.exe) will be included in that directory.
  • Run PuTTY Key Generator (puttygen.exe). You will need to generate a PuTTY version of your GitHub SSH key (as PuTTY has its own key format)
  • Select "Conversions" -> "Import Key"
  • Select your GitHub key (e.g. [HOME]/.ssh/github_rsa), and save it as a PPK file of the same name
  • Run Pageant (pageant.exe), and add that new PPK key to its list. Pageant may open in your notification area. This WinSCP guide shows how to add keys to Pageant.
  • WARNING: Pageant MUST BE RUNNING for SSH forwarding to work properly. You will likely want to set it up to startup whenever you start Windows. The WinSCP guide provides (linked above) provides instructions for that, or this blog post.
    • On Windows 10, you'll want the shortcut to be placed in C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
  • That's it. Now, a vagrant up that needs to use your local GitHub SSH key should also work properly.

Resources / Debugging

This setup is inspired by the following guides/posts:

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