SSH to Vagrant box in Windows?

167,523

Solution 1

I use PuTTY to connect to my Vagrant boxes on Windows7.

Make sure you

  • convert the %USERPROFILE%\.vagrant.d\insecure_private_key to .ppk using PuTTYGen
  • use the .ppk key in your PuTTY session - configured in Connection > SSH > Auth > Private key file
  • use host 127.0.0.1
  • use port 2222 instead of 22
  • you can set the default username (vagrant) under Connection > SSH > Auth > Private key for authentication

Solution 2

You must patch some Vagrant code by modifying only one file, ssh.rb.

All the info is here: https://gist.github.com/2843680

vagrant ssh will now work also in Windows, just like in Linux.


EDIT: In newer Versions this became unnecessary. You still have to add the path to your ssh.exe to your PATH Variable:

Search for ssh.exe on your computer, copy the Path (i.e. C:\Program Files (x86)\Git\bin), open System Preferences, find the Environment variable Settings, click on the Path Variable, add the path, separating the existing paths using ;.

Solution 3

Another option using git binaries:

  1. Install git: http://git-scm.com/download/win
  2. Start Menu > cmd (shift+enter to go as Administrator)
  3. set PATH=%PATH%;C:\Program Files\Git\usr\bin
  4. vagrant ssh

Hope this helps :)

Just a bonus after months using that on Windows: use Console instead of the Win terminal, so you can always open a new terminal tab with PATH set (configure it on options)

Solution 4

Download Putty: http://www.chiark.greenend.org.uk/~sgtatham/putty/

Using putty.exe:

Putty GUI:

HostName: 127.0.0.1
Port: 2222

When you connect(Terminal Screen):

User: vagrant
Passwd: vagrant

Before you try to connect, verify your VM using cmd.exe:

 vagrant status

If it is down use:

vagrant up

Solution 5

The accepted answer is really helpful however it may happen that instead of the following key:

%USERPROFILE%\.vagrant.d\insecure_private_key

a different key has to be converted to the Putty's format:

{vagrant_machine_root}/.vagrant/machines/default/virtualbox/private_key

Where {vagrant_machine_root} is a folder with the Vagrantfile of the machine we want to connect to using Putty.

As @ibizaman mentioned use vagrant ssh-config to check what key is used by vagrant:

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile c:/dev/test/.vagrant/machines/default/virtualbox/private_key <-- HERE IS THE KEY YOU NEED TO CONVERT TO THE PUTTY FORMAT
  IdentitiesOnly yes
  LogLevel FATAL
Share:
167,523

Related videos on Youtube

Sathish
Author by

Sathish

Updated on July 08, 2022

Comments

  • Sathish
    Sathish almost 2 years

    I'm using Vagrant to start a VirtualBox VM in windows. In other platforms, I can just

    $ vagrant ssh
    

    to connect to the VM.

    How do i connect to this Vagrant box in windows?

    The way suggested in Vagrant documentation to use PuTTy also did not work:

    http://docs-v1.vagrantup.com/v1/docs/getting-started/ssh.html

    • Sliq
      Sliq over 10 years
      Big tutorial on this, featuring how to SSH into a Vagrant box in Windows 7/8
    • Omri Spector
      Omri Spector over 10 years
      This is not an issue anymore. If you have ssh.exe in your path (e.g. c:\Program Files\git\bin), then 'vagrant ssh' works.
    • KCD
      KCD over 9 years
      FYI Vagrant doesn't include ssh due to licensing reasons. It seems possible but until a lawyer gives the go-ahead on this, we're not gong to do this. Ship ssh.exe by default in the Windows installer ... it puzzled me
  • DGomez
    DGomez almost 12 years
    note too: when the login as: prompt appears, enter 'vagrant' as the user name (without quotes).
  • Michael Field
    Michael Field over 11 years
    And then do that for every VM in Vagrant. You know, Vagrant works with multiple VM's. So, just use the proper solution, not workaround: there is my answer below. Small patch in Vagrant.
  • Cory Dolphin
    Cory Dolphin over 11 years
    Please comment on the Github pull request for the issue if you use this work around, it is time it got merged. github.com/mitchellh/vagrant/pull/933
  • Johann
    Johann almost 11 years
    This appears to have been merged about three months ago. It definitely exists in version 1.2.2.
  • RadiantHex
    RadiantHex almost 11 years
    Someone upvote this answer. Was trying to figure out how I got this working, now I know! Thanks
  • Alex
    Alex almost 11 years
    Is this still needed or patched in recent Vagrant dists?
  • Bartvds
    Bartvds over 10 years
    This worked fine for me. As I have GIT it works a lot cleaner then switching to Putty since you can just reuse the same console window.
  • Bartvds
    Bartvds over 10 years
    Thanks, but I now use conemu. It is similar but more flexible then Console. I have it wrapping many consoles, like cmd.exe, git bash and putty with alliases and other fancy stuff.
  • robertpateii
    robertpateii over 10 years
    @Alex It's definitely patched into recent Vagrant versions. I tested it and documented it here: robertpate.net/blog/2013/…
  • sobi3ch
    sobi3ch over 10 years
    How did you know you need to convert key to .ppk?
  • Dror Bereznitsky
    Dror Bereznitsky over 10 years
    @sobi3ch there are many resources explaining how to use your keys with Putty, for example meinit.nl/using-your-openssh-private-key-in-putty
  • mpen
    mpen about 10 years
    You can set the default username under Connection > Data > Auto-login username and the PPK setting is under Connection > SSH > Auth > Private key for authentication
  • Larry Silverman
    Larry Silverman almost 10 years
    If you prefer as I do to secure your private keys with passwords, it may be easier in the long run to use the pageant.exe app at startup, loading your ppk file into it. Then you enter your password to pageant once, and you can log in without having to specify the ppk password in the putty terminal every time.
  • ThorSummoner
    ThorSummoner over 9 years
    Why would you go though all that effort just to use PuTTY
  • ThorSummoner
    ThorSummoner over 9 years
    If you install git bash before vagrant, vagrant will add itself to the git bash path allowing it to use the git bash ssh from a git bash terminal.
  • ibizaman
    ibizaman over 9 years
    @ThorSummoner to use x11 forwarding for example.
  • ibizaman
    ibizaman over 9 years
    You can use vagrant ssh-config to know what options to give to putty.
  • ibizaman
    ibizaman over 9 years
    @ThorSummoner not alone, but with XMing for example, it can.
  • Mark Liu
    Mark Liu over 9 years
    Why was this voted now? It is a reasonable solution and if you want to dev in Windows, this is a great option, much better than hacking your putty, hacking your other commands.
  • Idan Gozlan
    Idan Gozlan over 9 years
    Another thing to do before ready to use on putty with @ibizaman way, is to convert the private key to ppk with PuttyGen (import>save)
  • Janusz Skonieczny
    Janusz Skonieczny over 9 years
    Quickly set path to your git OpenSSH, open PowerShell and run: [Environment]::SetEnvironmentVariable("Path", "$env:Path;c:\Program Files (x86)\Git\bin\", "User")
  • Shani Elharrar
    Shani Elharrar about 9 years
    The other answer that says to add ssh.exe to your path variable is preferred. less manual work needed.
  • icc97
    icc97 about 9 years
    This was by far the best solution for me. The link you give hasn't been updated since 2010. There is a more recent version available at mls-software.com/opensshd.html
  • David Soroko
    David Soroko about 9 years
    You will need to amen your PATH to include the Cygwin bin directory, something like set PATH=%PATH%;c:\cygwin\bin
  • vladimir
    vladimir over 8 years
    for Git 2.5.0 PATH should be "C:\Program Files\Git\usr\bin".
  • Wildcard
    Wildcard over 8 years
    Very helpful. Still not working for me, probably because I'm using MobaXterm. I posted my follow-up question here: stackoverflow.com/q/33879568/5419599
  • dtbarne
    dtbarne over 8 years
    Works for me after I updated my path, although I had to restart to get it to stick for some reason.
  • v.babak
    v.babak about 8 years
    set PATH=%PATH%;C:\Program Files\Git\usr\bin Working perfect! Thanks!
  • johnzachary
    johnzachary about 8 years
    If you want to use Putty, then this is the best answer. You have to convert the correct private key. Many thanks!
  • appoll
    appoll over 7 years
    This shows perfectly how not to get lost in words. Amazing, thanks!
  • perlyking
    perlyking over 7 years
    Worth mentioning that you should check the User value and make sure you supply it to Putty in Connection->Data->Auto-login username. On my VM the name was ubuntu and I had been repeatedly trying, and failing, to log in as vagrant.
  • perlyking
    perlyking over 7 years
    Best to check the output of vagrant ssh-config to make sure this is the correct user name: on my VM, ubuntu/xenial64, it was ubuntu
  • Neoraptor
    Neoraptor about 7 years
    This is the best answer here. Cygwin will give you a bash like terminal to work with. Awesome!
  • Jared Beach
    Jared Beach about 7 years
    Not working for me. Do I need a password when running ssh-keygen?
  • mixel
    mixel about 7 years
    @JaredBeach No, don't enter passphrase when generating key.
  • Bud Damyanov
    Bud Damyanov about 3 years
    It is the correct answer, things(settings) were changed in years.