我正在使用Vagrant在Windows中启动一个VirtualBox虚拟机。在其他平台中,我可以直接
$ vagrant ssh
如何在Windows中连接到此Vagrant box?
Vagrant文档建议使用PuTTy进行连接,但这种方式也没有起作用:
http://docs-v1.vagrantup.com/v1/docs/getting-started/ssh.html
我正在使用Vagrant在Windows中启动一个VirtualBox虚拟机。在其他平台中,我可以直接
$ vagrant ssh
如何在Windows中连接到此Vagrant box?
Vagrant文档建议使用PuTTy进行连接,但这种方式也没有起作用:
http://docs-v1.vagrantup.com/v1/docs/getting-started/ssh.html
我之前也遇到过同样的问题。
在homestead文件夹中,使用bash init.sh
命令。
如果在D:/Users/你的用户名/
下没有.ssh文件夹,你需要生成一对ssh密钥,使用命令ssh-keygen -t rsa -C "you@homestead"
。
编辑Homestead.yaml(homestead/)文件,将authoriza: ~/.ssh/id_rsa.pub
添加进去。
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
你需要使用Git Bash桌面应用程序。
打开Git Bash桌面应用程序。vagrant up
vagrant ssh
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
其中vagrant_rsa
和vagrant_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"
这是 joefitzgerald/packer-windows openssh setup script 的简化版本。
现在你可以通过 vagrant ssh
进入 Windows 虚拟机。
对于那些使用 vagrant-libvirt 的用户,可能需要将一些端口(如 RDP)从主机转发到虚拟机。vagrant-libvirt 使用 ssh 进行 端口转发,因此您需要按照上述说明在 Windows 上设置 ssh,然后就可以转发端口了:
config.vm.network :forwarded_port, host: 3389, guest: 3389, id: "rdp", gateway_ports: true, host_ip: '*'
我认为对于这个问题,一个更好的答案应该是:
https://eamann.com/tech/linux-flavored-windows/
Eric 写了一篇关于如何将您的 Windows 计算机转换为 Linux 环境的好文章。即使使用一些技巧来让 Vim 在 cmd 中本地工作。
如果您按照此指南操作,基本上可以让您安装 git cli,并通过一些技巧,在 vagrant box 文件夹中键入 vagrant ssh 命令提示符,它将正确执行所有操作,无需配置 ssh 密钥等。所有这些都与 ssh 和 git cli /bin 一起提供。
这样做的好处是,您实际上可以运行 powershell 并获得所有 *nix 的好处。这真正简化了您的环境,并有助于运行 Vagrant 和其他事情。
TL;DR 下载 Git cli 并将 git/bin 添加到 PATH。在 /bin 中修改 vim.bat 以适用于 Windows。在 cmd 中本地使用 ssh。