好记性不如铅笔头

操作系统

vagrant简单使用笔记

闲话不扯了,简单的记个流水账。PS:本文比较水,很多内容都省略了。

CONTENTS

软件依赖

vagrant依赖现有的虚拟机软件【VMware,virtualbox】等,这里考虑到速度解决战斗,直接使用免费的virtualbox。下载地址:

vagrant:【 https://www.vagrantup.com/downloads.html
virtualbox:【 https://www.virtualbox.org/wiki/Downloads

使用vagrant

vagrant默认提供了很多box,参考网址【 https://atlas.hashicorp.com/ubuntu/boxes/ 】,这里简单起见,我们使用ubuntu/xenial64
进入命令行,输入如下命令,此时会在当前目录中建立一个vagrantfile文件,这个文件是用来描述当前虚拟机实例的。注意这里的名字,如果这个名字能够和本地box列表中的对应上,那么就使用box中的虚拟机模板来在本地路径中实例化一个虚拟机,如果这个名字在本地box列表中没有找到,就回去box网站上搜索和下载。 

D:\code>vagrant init ubuntu/xenial64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

可以看到,作者本地没有这个虚拟机模板,因此尝试启动这个虚拟机实例时会自动下载。

启动虚拟机实例

D:\code>vagrant up ubuntu/xenial64
Bringing machine 'ubuntu/xenial64' up with 'virtualbox' provider...
==> default: Box 'ubuntu/xenial64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/xenial64'
    default: URL: https://atlas.hashicorp.com/ubuntu/xenial64
==> default: Adding box 'ubuntu/xenial64' (v20161119.0.0) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/xenial64/versions/20161119.0.0/providers/virtualbox.box
    default: Progress: 0% (Rate: 3793/s, Estimated time remaining: 25:52:35)

如果网速非常慢,可以使用下载工具手动下载链接,下载后使用命令手动将虚拟机模板加入本地vagrant的box列表。

D:\code>vagrant box add -name ubuntu/xenial64 xenial-server-cloudimg-amd64-vagrant.box

再次启动

D:\code>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/xenial64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: code_default_1479832133505_47764
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: ubuntu
    default: SSH auth method: password
。。。。。。
。。。。。。
    default:
    default: Guest Additions Version: 5.0.24
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => D:/code

注意这里已经表明了SSH用户名和共享目录,那密码呢?

D:\code>vagrant ssh
`ssh` executable not found in any directories in the %PATH% variable. Is an
SSH client installed? Try installing Cygwin, MinGW or Git, all of which
contain an SSH client. Or use your favorite SSH client with the following
authentication information shown below:

Host: 127.0.0.1
Port: 2222
Username: ubuntu
Private key: D:/code/.vagrant/machines/default/virtualbox/private_key

可以看到,登录密码被放到了private_key文件中。如果想变更用户名和密码,可以修改对应虚拟机实例的vagrantfile。vagrant的虚拟机是基于vagrantfile文件的,关于这个文件如何使用,可以参考【 https://www.vagrantup.com/docs/vagrantfile/ 】,这里作者只使用最简单的功能,就不在展开了。

发表评论

8 + 10 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据