173

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

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
Sathish
  • 20,660
  • 24
  • 63
  • 71
  • 2
    Big tutorial on this, featuring [how to SSH into a Vagrant box in Windows 7/8](http://www.dev-metal.com/setup-virtual-machine-multiple-vagrant-puphpet/) – Sliq Sep 15 '13 at 16:24
  • 12
    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. – Omri Spector Jan 26 '14 at 09:34
  • 1
    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](https://github.com/mitchellh/vagrant/issues/4396) ... it puzzled me – KCD Nov 05 '14 at 05:43

23 Answers23

245

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
DATEx2
  • 3,585
  • 1
  • 24
  • 26
Dror Bereznitsky
  • 20,048
  • 3
  • 48
  • 57
  • 28
    note too: when the login as: prompt appears, enter 'vagrant' as the user name (without quotes). – snorkpete Jun 28 '12 at 14:14
  • 5
    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. – Michael Field Dec 04 '12 at 18:07
  • How did you know you need to convert key to .ppk? – sobi3ch Nov 29 '13 at 09:36
  • @sobi3ch there are many resources explaining how to use your keys with Putty, for example http://meinit.nl/using-your-openssh-private-key-in-putty – Dror Bereznitsky Dec 02 '13 at 12:02
  • 20
    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` – mpen Mar 23 '14 at 05:33
  • 1
    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. – Larry Silverman Jul 30 '14 at 15:08
  • Why would you go though all that effort just to use PuTTY – ThorSummoner Aug 08 '14 at 17:31
  • @ThorSummoner to use x11 forwarding for example. – ibizaman Sep 08 '14 at 19:40
  • 8
    You can use `vagrant ssh-config` to know what options to give to putty. – ibizaman Sep 08 '14 at 19:41
  • @ThorSummoner not alone, but with XMing for example, it can. – ibizaman Sep 09 '14 at 09:44
  • 1
    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) – Idan Gozlan Dec 29 '14 at 12:27
  • 1
    The other answer that says to add ssh.exe to your path variable is preferred. less manual work needed. – Shani Elharrar Feb 23 '15 at 07:45
122

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 ;.

Community
  • 1
  • 1
Michael Field
  • 2,092
  • 1
  • 17
  • 12
  • 1
    Please comment on the Github pull request for the issue if you use this work around, it is time it got merged. https://github.com/mitchellh/vagrant/pull/933 – Cory Dolphin Jan 08 '13 at 08:51
  • 4
    This appears to have been merged about three months ago. It definitely exists in version 1.2.2. – Johann May 07 '13 at 16:45
  • Is this still needed or patched in recent Vagrant dists? – Alex Jul 10 '13 at 10:05
  • 2
    @Alex It's definitely patched into recent Vagrant versions. I tested it and documented it here: http://www.robertpate.net/blog/2013/getting-the-vagrant-ssh-command-to-work-on-windows/ – robertpateii Nov 26 '13 at 17:26
  • 1
    Quickly set path to your git OpenSSH, open PowerShell and run: `[Environment]::SetEnvironmentVariable("Path", "$env:Path;c:\Program Files (x86)\Git\bin\", "User")` – Janusz Skonieczny Jan 10 '15 at 12:45
  • Very helpful. Still not working for me, probably because I'm using MobaXterm. I posted my follow-up question here: http://stackoverflow.com/q/33879568/5419599 – Wildcard Nov 23 '15 at 20:01
  • Works for me after I updated my path, although I had to restart to get it to stick for some reason. – dtbarne Dec 19 '15 at 00:43
96

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

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)

starball
  • 20,030
  • 7
  • 43
  • 238
gabrielhpugliese
  • 2,588
  • 19
  • 19
  • Someone upvote this answer. Was trying to figure out how I got this working, now I know! Thanks – RadiantHex Jun 20 '13 at 11:57
  • 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 Aug 05 '13 at 17:49
  • 2
    Thanks, but I now use [conemu](https://code.google.com/p/conemu-maximus5/). 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. – Bartvds Aug 06 '13 at 23:14
  • 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. – ThorSummoner Aug 08 '14 at 19:39
  • 4
    for Git 2.5.0 PATH should be "C:\Program Files\Git\usr\bin". – vladimir Aug 25 '15 at 12:58
  • 1
    set PATH=%PATH%;C:\Program Files\Git\usr\bin Working perfect! Thanks! – v.babak Feb 01 '16 at 14:23
72

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
Rafael Gorski
  • 1,483
  • 14
  • 22
33

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
Community
  • 1
  • 1
begie
  • 1,194
  • 1
  • 11
  • 23
  • 4
    If you want to use Putty, then this is the best answer. You have to convert the correct private key. Many thanks! – johnzachary Apr 06 '16 at 15:19
  • 1
    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 Nov 30 '16 at 10:38
  • It is the correct answer, things(settings) were changed in years. – Bud Damyanov Feb 18 '21 at 17:44
11

Now you could also use the following plugin to connect using putty:

https://github.com/nickryand/vagrant-multi-putty

Just execute

vagrant plugin install vagrant-multi-putty

and make sure putty.exe and puttygen.exe are in your PATH

Then you'll be able to use

vagrant putty

The above mentioned executables are available from:

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Camilo Estevez
  • 637
  • 1
  • 8
  • 16
7

Either

  1. In your cmd console type the following:

    set PATH=%PATH%;C:\Program Files (x86)\Git\bin
    

OR

  1. Permanently set the path in your system's environment variables:

    C:\Program Files (x86)\Git\bin;
    
techraf
  • 64,883
  • 27
  • 193
  • 198
Kripa Jayakumar
  • 891
  • 9
  • 16
4

There is an OpenSSH package for Windows which is basically a stripped down Cygwin. It has an msi-Installer and (after setting your path accordingly) works with "vsagrant ssh":

http://sourceforge.net/projects/opensshwindows/?source=directory

Christian Waidner
  • 1,324
  • 1
  • 13
  • 22
  • 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 http://www.mls-software.com/opensshd.html – icc97 Mar 19 '15 at 22:01
2

Now I have a much better solution that enables painless Vagrant upgrade. It is based on patched file.

The first line of a vagrantfile should be:

load "vagrantfile_ssh" if Vagrant::Util::Platform.windows?

And the patched vagrantfile_ssh file (originaly named ssh.rb) should exist in the same directory as vagrantfile. This is both elegant and functional.

Download the patched vagrantfile_ssh.

Michael Field
  • 2,092
  • 1
  • 17
  • 12
2
  1. Install Git Bash (Comes with OpenSSH, as well as grep, find, perl, sed, etc.;)

    If you have Vagrant installed, open appwiz.cpl (AKA Add/Remove Programs) and Repair the Vagrant installation. This will make vagrant add itself to your git-bash path.

  2. Open Git Bash (Via the start menu)

  3. cd ~/vagrant/mybox to your vagrant box'es folder and vagrant ssh
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
2

I was doing as suggested above but changing the environment variable on the command line only with PATH=%PATH%;"C:\Program files\git\usr\bin"

This did not work. But when I amended the Environment variable through Windows Settings and then started a new command prompt it worked first time!

Paul Pritchard
  • 614
  • 2
  • 11
  • 23
1

Personally, I just use Cygwin. Which allows you to use many common *nix commands in Windows. SSH being one of them.

Kiksy
  • 1,272
  • 5
  • 20
  • 43
  • You will need to amen your PATH to include the Cygwin bin directory, something like `set PATH=%PATH%;c:\cygwin\bin` – David Soroko Apr 15 '15 at 13:42
1

Windows 7 ( not tested on other versions )

  1. Right Click on "My Computer" and click properties
  2. Click Advanced System Settings
  3. Click Environment Variables
  4. In second box under "System Variables" scroll down and click on variable "Path" and click EDIT
  5. Make sure the path is included in the Variable Value by appending (a semicolon if it's not there and) C:\Program Files (x86)\Git\bin; ( or whatever is the path to your ssh.exe )
  6. OK > OK > OK and RESTART WINDOWS
  7. You'll have to run vagrant up but after that vagrant ssh should work permanently

More Info which might help with other versions ... http://www.computerhope.com/issues/ch000549.htm

byronyasgur
  • 4,627
  • 13
  • 52
  • 96
1

very simple, once you install Vagrant manager and virtual box, try installing cygwin on windows but while installing cygwin, make sure to select the SSH package, VIM package which will enable your system to login to your VM from cygwin after spinning up your machine through vagrant.

Prashanth
  • 91
  • 6
1

The vagrant installation folder contains an ssh.exe that behaves like ssh(1) on linux (takes the same flags/arguments).

To see all of the arguments used, you can run vagrant ssh-config or vagrant ssh --debug for a more verbose output.

from Powershell:

C:\\HashiCorp\\Vagrant\\embedded\\usr\\bin/ssh.EXE ubuntu@127.0.0.1 -p 2222 -o LogLevel=FATAL -o DSAAuthentication=yes -o Strict
HostKeyChecking=no -i "C:/Users/path/to/project/.vagrant/machines/default/virtualbox/private_key"

This is useful is situations where vagrant status says your vm is in poweroff or suspended mode when you're positive it actually is running, to force ssh connection.

If you don't want to remember/type the above command, juste write it in a vagrant.ps1 file so you can execute it from your powershell using .\vagrant_ssh.ps1

rxdazn
  • 1,380
  • 1
  • 14
  • 30
1

you can using emulator terminal cmder for windows.

Follow below the steps for instalation:

  1. Access cmder.net;
  2. Click in Download Full;
  3. Unzip
  4. (optional) Place your own executable files into the bin folder to be injected into your PATH.
  5. Run Cmder (Cmder.exe)

enter image description here

Terminal cmder on Windows

Now execute command required for settings VM vagrant, for connect only execute the command vagrant ssh; Watch cmder offers ssh client embedded.

I hope this helps.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

note too: when the login as: prompt appears, enter 'vagrant' as the user name (without quotes). – Snorkpete Jun 28 '12 at 14:14

Or you can go to Category->Connection->Rlogin and set the 'Auto-login username' field to 'Vagrant'.

Save the session.

BadAlgorithm
  • 117
  • 3
  • 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` – perlyking Nov 30 '16 at 10:42
0

I use vagrant ssh command with git ssh on windows 7, 8.1 and Windows 10.

To install only download msysgit from msysgit project page.

And to run ssh.exe only include the bin folter to windows environment variables or install git from Git Download page and copy the msysgit bin folter to C:\Program Files\Git\bin.

0

Add the following lines to your Vagrantfile:

Vagrant.configure(2) do |config|

  # ...

  config.ssh.private_key_path = "vagrant_rsa"
  config.vm.provision "shell", path: "openssh.ps1"
  config.vm.provision "file", source: "./vagrant_rsa.pub", destination: "~/.ssh/authorized_keys"
end

where vagrant_rsa and vagrant_rsa.pub is the private and public keys located in current vagrant project folder (and generated e.g. by ssh-keygen -t rsa -C "your@email.here") and openssh.ps1 is:

$is_64bit = [IntPtr]::size -eq 8

# setup openssh
$ssh_download_url = "http://www.mls-software.com/files/setupssh-7.1p1-1.exe"

if (!(Test-Path "C:\Program Files\OpenSSH\bin\ssh.exe")) {
    Write-Output "Downloading $ssh_download_url"
    (New-Object System.Net.WebClient).DownloadFile($ssh_download_url, "C:\Windows\Temp\openssh.exe")

    Start-Process "C:\Windows\Temp\openssh.exe" "/S /privsep=1 /password=D@rj33l1ng" -NoNewWindow -Wait
}

Stop-Service "OpenSSHd" -Force

# ensure vagrant can log in
Write-Output "Setting vagrant user file permissions"
New-Item -ItemType Directory -Force -Path "C:\Users\vagrant\.ssh"
C:\Windows\System32\icacls.exe "C:\Users\vagrant" /grant "vagrant:(OI)(CI)F"
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\bin" /grant "vagrant:(OI)RX"
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\usr\sbin" /grant "vagrant:(OI)RX"

Write-Output "Setting SSH home directories"
    (Get-Content "C:\Program Files\OpenSSH\etc\passwd") |
    Foreach-Object { $_ -replace '/home/(\w+)', '/cygdrive/c/Users/$1' } |
    Set-Content 'C:\Program Files\OpenSSH\etc\passwd'

# Set shell to /bin/sh to return exit status
$passwd_file = Get-Content 'C:\Program Files\OpenSSH\etc\passwd'
$passwd_file = $passwd_file -replace '/bin/bash', '/bin/sh'
Set-Content 'C:\Program Files\OpenSSH\etc\passwd' $passwd_file

# fix opensshd to not be strict
Write-Output "Setting OpenSSH to be non-strict"
$sshd_config = Get-Content "C:\Program Files\OpenSSH\etc\sshd_config"
$sshd_config = $sshd_config -replace 'StrictModes yes', 'StrictModes no'
$sshd_config = $sshd_config -replace '#PubkeyAuthentication yes', 'PubkeyAuthentication yes'
$sshd_config = $sshd_config -replace '#PermitUserEnvironment no', 'PermitUserEnvironment yes'
# disable the use of DNS to speed up the time it takes to establish a connection
$sshd_config = $sshd_config -replace '#UseDNS yes', 'UseDNS no'
# disable the login banner
$sshd_config = $sshd_config -replace 'Banner /etc/banner.txt', '#Banner /etc/banner.txt'
# next time OpenSSH starts have it listen on th eproper port
Set-Content "C:\Program Files\OpenSSH\etc\sshd_config" $sshd_config

Write-Output "Removing ed25519 key as Vagrant net-ssh 2.9.1 does not support it"
Remove-Item -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\etc\ssh_host_ed25519_key"
Remove-Item -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\etc\ssh_host_ed25519_key.pub"

# use c:\Windows\Temp as /tmp location
Write-Output "Setting temp directory location"
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\tmp"
C:\Program` Files\OpenSSH\bin\junction.exe /accepteula "C:\Program Files\OpenSSH\tmp" "C:\Windows\Temp"
C:\Windows\System32\icacls.exe "C:\Windows\Temp" /grant "vagrant:(OI)(CI)F"

# add 64 bit environment variables missing from SSH
Write-Output "Setting SSH environment"
$sshenv = "TEMP=C:\Windows\Temp"
if ($is_64bit) {
    $env_vars = "ProgramFiles(x86)=C:\Program Files (x86)", `
        "ProgramW6432=C:\Program Files", `
        "CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files", `
        "CommonProgramW6432=C:\Program Files\Common Files"
    $sshenv = $sshenv + "`r`n" + ($env_vars -join "`r`n")
}
Set-Content C:\Users\vagrant\.ssh\environment $sshenv

# record the path for provisioners (without the newline)
Write-Output "Recording PATH for provisioners"
Set-Content C:\Windows\Temp\PATH ([byte[]][char[]] $env:PATH) -Encoding Byte

# configure firewall
Write-Output "Configuring firewall"
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow service=OpenSSHd enable=yes
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow program="C:\Program Files\OpenSSH\usr\sbin\sshd.exe" enable=yes
netsh advfirewall firewall add rule name="ssh" dir=in action=allow protocol=TCP localport=22

Start-Service "OpenSSHd"

which is simplified version of joefitzgerald/packer-windows openssh setup script.

Now you can vagrant ssh into the Windows box.

For those who use vagrant-libvirt you possibly want to forward some ports like RDP from host to guest. vagrant-libvirt uses ssh for port forwarding so you need to setup ssh on Windows like in above instructions and then you will be able to forward ports like:

config.vm.network :forwarded_port, host: 3389, guest: 3389, id: "rdp", gateway_ports: true, host_ip: '*'
mixel
  • 25,177
  • 13
  • 126
  • 165
0

I also met the same problem before.

  1. In the homestead folder, use bash init.sh.

  2. If you don't have .ssh folder in D:/Users/your username/, you need to get a pair of ssh keys, ssh-keygen -t rsa -C "you@homestead".

  3. Edit Homestead.yaml(homestead/), authoriza: ~/.ssh/id_rsa.pub.

  4. keys: - ~/.ssh/id_rsa

5.

folders:
    - map: (share directory path in the host computer) 
      to: /home/vagrant/Code

sites:
    - map: homestead.app
      to: /home/vagrant/Code
  1. You need to use git bash desktop app.

  2. Open git bash desktop app. vagrant up

  3. vagrant ssh

JKol
  • 13
  • 7
0

Another solution here but only for the virtual box of windows 10 to test explorer. ssh user: IEUser ssh pass:Passw0rd!

Gustavo
  • 785
  • 1
  • 12
  • 31
0

A good alternative to PuTTY is the Mintty terminal emulator. It has more configurable options than PuTTY.

yvoloshin
  • 378
  • 6
  • 18
-1

I think a better answer to this question would be the following:

https://eamann.com/tech/linux-flavored-windows/

Eric wrote a nice article on how to turn your windows computer into a Linux environment. Even with hacks to get Vim working natively in cmd.

If you run through this guide, which basically gets you to install git cli, and with some hacks, you can bring up a command prompt and type vagrant ssh while in the folder of your vagrant box and it will properly do the right things, no need to configure ssh keys etc. All that comes with ssh and the git cli /bin.

The power of this is that you can then actually run powershell and still get all the *nix goodness. This really simplifies your environment and helps with running Vagrant and other things.

TL;DR Download Git cli and add git/bin to PATH. Hack vim.bat in /bin to work for windows. Use ssh natively in cmd.

Mark Liu
  • 124
  • 1
  • 3
  • 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. – Mark Liu Nov 20 '14 at 21:51