简体   繁体   English

SSH 到 Windows 中的 Vagrant 框?

[英]SSH to Vagrant box in Windows?

I'm using Vagrant to start a VirtualBox VM in windows.我正在使用 Vagrant 在 Windows 中启动 VirtualBox VM。 In other platforms, I can just在其他平台,我可以

$ vagrant ssh

to connect to the VM.连接到虚拟机。

How do i connect to this Vagrant box in windows?我如何在 Windows 中连接到这个 Vagrant 盒子?

The way suggested in Vagrant documentation to use PuTTy also did not work: Vagrant 文档中建议的使用 PuTTy 的方式也不起作用:

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

I use PuTTY to connect to my Vagrant boxes on Windows7.我使用 PuTTY 连接到 Windows7 上的 Vagrant 盒子。

Make sure you确保你

  • convert the %USERPROFILE%\\.vagrant.d\\insecure_private_key to .ppk using PuTTYGen使用PuTTYGen%USERPROFILE%\\.vagrant.d\\insecure_private_key为 .ppk
  • use the .ppk key in your PuTTY session - configured in Connection > SSH > Auth > Private key file在 PuTTY 会话中使用 .ppk 密钥 - 在 Connection > SSH > Auth > Private key file 中配置
  • use host 127.0.0.1使用主机127.0.0.1
  • use port 2222 instead of 22使用端口 2222 而不是 22
  • you can set the default username (vagrant) under Connection > SSH > Auth > Private key for authentication您可以在 Connection > SSH > Auth > Private key for authentication 下设置默认用户名(vagrant)

You must patch some Vagrant code by modifying only one file, ssh.rb .你必须通过只修改一个文件ssh.rb修补一些 Vagrant 代码。

All the info is here: https://gist.github.com/2843680所有信息都在这里: https : //gist.github.com/2843680

vagrant ssh will now work also in Windows, just like in Linux. vagrant ssh现在也可以在 Windows 中工作,就像在 Linux 中一样。


EDIT : In newer Versions this became unnecessary.编辑:在较新的版本中,这变得不必要了。 You still have to add the path to your ssh.exe to your PATH Variable :您仍然需要ssh.exe的路径添加到PATH变量中

Search for ssh.exe on your computer, copy the Path (ie 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 ;在电脑上搜索ssh.exe ,复制路径(即C:\\Program Files (x86)\\Git\\bin ),打开系统偏好设置,找到环境变量设置,点击路径变量,添加路径,分隔现有路径使用; . .

Another option using git binaries:使用 git 二进制文件的另一种选择:

  1. Install git: http://git-scm.com/download/win安装 git: http : //git-scm.com/download/win
  2. Start Menu > cmd (shift+enter to go as Administrator)开始菜单 > cmd(shift+enter 以管理员身份进入)
  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)在 Windows 上使用几个月后只是一个奖励:使用控制台而不是 Win 终端,因此您始终可以打开一个带有 PATH 设置的新终端选项卡(在选项上配置)

Download Putty: http://www.chiark.greenend.org.uk/~sgtatham/putty/下载腻子: http : //www.chiark.greenend.org.uk/~sgtatham/putty/

Using putty.exe:使用 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:在尝试连接之前,请使用 cmd.exe 验证您的 VM:

 vagrant status

If it is down use:如果它是下来使用:

vagrant up

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:必须将不同的密钥转换为 Putty 的格式:

{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.其中{vagrant_machine_root}是一个文件夹,其中Vagrantfile我们要使用 Putty 连接的机器的Vagrantfile

As @ibizaman mentioned use vagrant ssh-config to check what key is used by vagrant:正如@ibizaman 提到的,使用vagrant ssh-config来检查 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

Now you could also use the following plugin to connect using putty:现在您还可以使用以下插件使用 putty 进行连接:

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

Just execute只需执行

vagrant plugin install vagrant-multi-putty vagrant 插件安装 vagrant-multi-putty

and make sure putty.exe and puttygen.exe are in your PATH并确保 putty.exe 和 puttygen.exe 在您的 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 http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Either任何一个

  1. In your cmd console type the following:在您的cmd控制台中键入以下内容:

     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;

There is an OpenSSH package for Windows which is basically a stripped down Cygwin.有一个适用于 Windows 的 OpenSSH 包,它基本上是一个精简的 Cygwin。 It has an msi-Installer and (after setting your path accordingly) works with "vsagrant ssh":它有一个 msi-Installer 并且(在相应地设置您的路径后)与“vsagrant ssh”一起使用:

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

Now I have a much better solution that enables painless Vagrant upgrade.现在我有了一个更好的解决方案,可以实现无痛 Vagrant 升级。 It is based on patched file.它基于补丁文件。

The first line of a vagrantfile should be: vagrantfile 的第一行应该是:

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 .并且修补过的vagrantfile_ssh文件(最初名为ssh.rb )应该与vagrantfile存在于同一目录中。 This is both elegant and functional.这既优雅又实用。

Download the patched vagrantfile_ssh .下载打过补丁的 vagrantfile_ssh

  1. Install Git Bash (Comes with OpenSSH, as well as grep , find , perl , sed , etc.;)安装Git Bash (随 OpenSSH 一起提供,以及grepfindperlsed等;)

    If you have Vagrant installed, open appwiz.cpl (AKA Add/Remove Programs) and Repair the Vagrant installation.如果您安装了 Vagrant,请打开appwiz.cpl (又名添加/删除程序)并修复Vagrant 安装。 This will make vagrant add itself to your git-bash path.这将使 vagrant 将自己添加到您的 git-bash 路径中。

  2. Open Git Bash (Via the start menu)打开 Git Bash(通过开始菜单)

  3. cd ~/vagrant/mybox to your vagrant box'es folder and vagrant ssh cd ~/vagrant/mybox到你的 vagrant box'es 文件夹和vagrant ssh

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"我按照上面的建议进行操作,但仅使用 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!但是当我通过 Windows 设置修改环境变量然后启动一个新的命令提示符时,它第一次起作用了!

Personally, I just use Cygwin .就个人而言,我只使用Cygwin Which allows you to use many common *nix commands in Windows.它允许您在 Windows 中使用许多常见的 *nix 命令。 SSH being one of them. SSH 就是其中之一。

Windows 7 ( not tested on other versions ) Windows 7 (未在其他版本上测试)

  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;通过附加(如果不存在分号和) C:\\Program Files (x86)\\Git\\bin;确保路径包含在变量值中 ( or whatever is the path to your ssh.exe ) (或任何 ssh.exe 的路径)
  6. OK > OK > OK and RESTART WINDOWS OK > OK > OK 并重新启动 WINDOWS
  7. You'll have to run vagrant up but after that vagrant ssh should work permanently你必须运行 vagrant up 但在那之后 vagrant ssh 应该永久工作

More Info which might help with other versions ... http://www.computerhope.com/issues/ch000549.htm更多信息可能对其他版本有帮助... http://www.computerhope.com/issues/ch000549.htm

很简单,一旦你安装了 Vagrant manager 和 virtual box,尝试在 windows 上安装 cygwin,但是在安装 cygwin 时,一定要选择 SSH 包,VIM 包,这将使你的系统在启动你的机器后从 cygwin 登录到你的虚拟机流浪汉。

you can using emulator terminal cmder for windows.您可以在 Windows 上使用模拟器终端 cmder。

Follow below the steps for instalation:请按照以下步骤进行安装:

  1. Access cmder.net;访问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. (可选)将您自己的可执行文件放入要注入到您的 PATH 中的 bin 文件夹中。
  5. Run Cmder (Cmder.exe)运行 Cmder (Cmder.exe)

在此处输入图片说明

Terminal cmder on Windows Windows 上的终端命令

Now execute command required for settings VM vagrant, for connect only execute the command vagrant ssh;现在执行设置VM vagrant所需的命令,连接只执行命令vagrant ssh; Watch cmder offers ssh client embedded.观看 cmder 提供嵌入式 ssh 客户端。

I hope this helps.我希望这有帮助。

note too: when the login as: prompt appears, enter 'vagrant' as the user name (without quotes).还要注意:当出现 login as: 提示时,输入 'vagrant' 作为用户名(不带引号)。 – Snorkpete Jun 28 '12 at 14:14 – Snorkpete 2012 年 6 月 28 日 14:14

Or you can go to Category->Connection->Rlogin and set the 'Auto-login username' field to 'Vagrant'.或者您可以转到 Category->Connection->Rlogin 并将“自动登录用户名”字段设置为“Vagrant”。

Save the session.保存会话。

I use vagrant ssh command with git ssh on windows 7, 8.1 and Windows 10.我在 Windows 7、8.1 和 Windows 10 上使用带有git ssh vagrant ssh命令。

To install only download msysgit from msysgit project page .要安装仅从msysgit 项目页面下载 msysgit。

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 .并且要运行ssh.exe只将bin folter到 windows 环境变量或从Git 下载页面安装 git 并将msysgit bin folter复制到C:\\Program Files\\Git\\bin

Add the following lines to your Vagrantfile : Vagrantfile添加到您的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 eg by ssh-keygen -t rsa -C "your@email.here" ) and openssh.ps1 is:其中vagrant_rsavagrant_rsa.pub是位于当前 vagrant 项目文件夹中的私钥和公钥(例如由ssh-keygen -t rsa -C "your@email.here" )和openssh.ps1是:

$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 .这是joefitzgerald/packer-windows openssh setup script 的简化版本。

Now you can vagrant ssh into the Windows box.现在你可以通过vagrant ssh进入 Windows 框。

For those who use vagrant-libvirt you possibly want to forward some ports like RDP from host to guest.对于那些使用vagrant-libvirt 的人,您可能希望将一些端口(如 RDP)从主机转发到来宾。 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: vagrant-libvirt 使用 ssh 进行端口转发,因此您需要按照上述说明在 Windows 上设置 ssh,然后您将能够转发如下端口:

config.vm.network :forwarded_port, host: 3389, guest: 3389, id: "rdp", gateway_ports: true, host_ip: '*'

I also met the same problem before.我之前也遇到过同样的问题。

  1. In the homestead folder, use bash init.sh .在 homestead 文件夹中,使用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" .如果D:/Users/your username/没有 .ssh 文件夹,则需要获取一对 ssh 密钥, ssh-keygen -t rsa -C "you@homestead"

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

  4. keys: - ~/.ssh/id_rsa

5. 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 .您需要使用git bash 桌面应用程序

  2. Open git bash desktop app.打开 git bash 桌面应用程序。 vagrant up

  3. vagrant ssh

Another solution here but only for the virtual box of windows 10 to test explorer.这里的另一个解决方案,但仅适用于 Windows 10 的虚拟框来测试资源管理器。 ssh user: IEUser ssh pass:Passw0rd! ssh 用户: IEUser ssh pass:Passw0rd!

The vagrant installation folder contains an ssh.exe that behaves like ssh(1) on linux (takes the same flags/arguments). vagrant 安装文件夹包含一个 ssh.exe,其行为类似于 linux 上的 ssh(1)(采用相同的标志/参数)。

To see all of the arguments used, you can run vagrant ssh-config or vagrant ssh --debug for a more verbose output.要查看使用的所有参数,您可以运行vagrant ssh-configvagrant ssh --debug以获得更详细的输出。

from Powershell:来自 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.这在vagrant status表示您的 vm 处于poweroffsuspended模式的情况下很有用,当您确定它实际上正在运行时,强制 ssh 连接。

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如果您不想记住/键入上述命令,请将其写入vagrant.ps1文件中,以便您可以使用.\\vagrant_ssh.ps1从您的 powershell 执行它

A good alternative to PuTTY is the Mintty terminal emulator. PuTTY 的一个很好的替代品是Mintty终端模拟器。 It has more configurable options than PuTTY.它比 PuTTY 有更多的可配置选项。

I think a better answer to this question would be the following:我认为这个问题的更好答案如下:

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

Eric wrote a nice article on how to turn your windows computer into a Linux environment. Eric 写了一篇关于如何将 Windows 计算机变成 Linux 环境的好文章。 Even with hacks to get Vim working natively in cmd.即使有一些技巧可以让 Vim 在 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.如果您阅读了本指南,它基本上可以让您安装 git cli,并通过一些技巧,您可以调出命令提示符并在 vagrant 框的文件夹中键入 vagrant ssh,它会正确执行正确的操作,不需要配置 ssh 密钥等。 ssh 和 git cli /bin 附带的所有内容。

The power of this is that you can then actually run powershell and still get all the *nix goodness.这样做的好处是,您可以实际运行 powershell 并仍然获得所有 *nix 优点。 This really simplifies your environment and helps with running Vagrant and other things.这确实简化了您的环境并有助于运行 Vagrant 和其他东西。

TL;DR Download Git cli and add git/bin to PATH. TL;DR 下载 Git cli 并将 git/bin 添加到 PATH。 Hack vim.bat in /bin to work for windows.破解 /bin 中的 vim.bat 以适用于 Windows。 Use ssh natively in cmd.在 cmd 中本地使用 ssh。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM