Windows下使用Vagrant

栏目: 服务器 · 发布时间: 5年前

内容简介:Vagrant是用来快速构建虚拟机的工具,只需要短短数行命令,就可以帮我们构建一个基于Virtualbox/VMware的虚拟机。在Linux系统下使用非常方便。本文介绍其在Windows下的使用方法。1.1,安装Virtualbox和Vagrant,这里不多说;1.2,新建一个文件夹,例如”C:\vm1″,并进入到此目录;

Vagrant是用来快速构建虚拟机的工具,只需要短短数行命令,就可以帮我们构建一个基于Virtualbox/VMware的虚拟机。在 Linux 系统下使用非常方便。本文介绍其在Windows下的使用方法。

1,准备工作

1.1,安装Virtualbox和Vagrant,这里不多说;

1.2,新建一个文件夹,例如”C:\vm1″,并进入到此目录;

1.3, 在Powershell中进入此目录 ,方法如下(以下方法任选其一):

方法1(推荐):按一下Shift+F10后松开,然后按S

方法2:在目录空白处鼠标右键,在右键菜单中选择”在此处打开 Powershell 窗口(s)”

方法3:按一下Windows+X后松开,然后按I,此时进入的Powershell不在”C:\vm1″目录,我们可以手动运行”cd C:\vm1″命令进入之

2,在Powershell中初始化虚拟机

在https://app.vagrantup.com找到你想要的虚拟机,

比如https://app.vagrantup.com/ubuntu/boxes/xenial64

页面上有个简单的介绍, 只需要下面几行命令

以下命令会在当前目录下(本文是C:\vm1)生成一个Vagrantfile的文件,记录该虚拟机的大致配置信息

$ vagrant init ubuntu/xenial64    #Ubuntu 16.04 

or

$ vagrant init debian/stretch64   #Debian 9

or

$ vagrant init centos/7           #Centos 7

3,虚拟机设定: 指定虚拟机IP

这个非常重要,因为使用Vagrantfile安装的虚拟机默认guest与host网络互不相通

使用Notepad++等 工具 打开这个Vagrantfile文件,取消注释如下行(即将虚拟机的IP指定为192.168.33.10,如果系统中已经有192.168.33.10的虚拟机在运行,请把这里的IP改成其它)

# config.vm.network "private_network", ip: "192.168.33.10"

4,虚拟机设定: 指定虚拟机内存大小

根据情况取消注释如下内容

# config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end

5,开启和运行虚拟机

$ vagrant up     #从vagrant网站下载对应的虚拟机,并运行之

$ vagrant ssh    #进入到虚拟机的 shell 环境, 默认用户名为vagrant,有sudo权限

其它注意事项

1,进入虚拟机以后, 如果发现多个虚拟机不能互相ssh进来, 使用以下命令依次排查问题

$ telnet 192.168.33.10 22

$ egrep '(PasswordAuthentication|PermitRootLogin)' /etc/ssh/sshd_config
#PermitRootLogin yes
#PasswordAuthentication yes
PasswordAuthentication yes        #CentOS 7里面可能默认是no, 需要改成yes以后再systemctl restart sshd

2,如果需要端口转发功能,即物理机的公网IP某转口转向虚拟机的22(执行vagrant的时候会有提示是哪个端口)。也可以在Vagrantfile文件中添加一行如下内容, 使得外部可以SSH进来

config.ssh.forward_agent = true

3,一些其它的有用命令

PS C:\vm1> vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
PS C:\vm1> vagrant help
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to HashiCorp's Vagrant Cloud
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     port            displays information about guest port mappings
     powershell      connects to machine via powershell remoting
     provision       provisions the vagrant machine
     push            deploys code in this environment to a configured destination
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     snapshot        manages snapshots: saving, restoring, etc.
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     validate        validates the Vagrantfile
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

4,如果需要更严格的guest与host访问权限, 可以参考https://www.vagrantup.com/docs/networking/forwarded_ports.html


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

逆向工程权威指南

逆向工程权威指南

Dennis Yurichev(丹尼斯) / 安天安全研究与应急处理中心 / 人民邮电出版社 / 2017-3-1 / 168

逆向工程是一种分析目标系统的过程,旨在于识别系统的各组件以及组件间关系,以便于通过其它形式、或在较高的抽象层次上,重建系统的表征。 本书专注于软件的逆向工程,是写给初学者的一本经典指南。全书共分为12个部分,共102章,涉及X86/X64、ARM/ARM-64、MIPS、Java/JVM等重要话题,详细解析了Oracle RDBMS、Itanium、软件狗、LD_PRELOAD、栈溢出、EL......一起来看看 《逆向工程权威指南》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具