您的位置:首页 > 其它

Vagrant使用指南:Vagrant命令使用简介

2016-08-26 22:14 459 查看


Vagrant是用ruby写的一个工具, 它的出现是为了更加容易的解决开发环境的一致性问题. 我们将会在本系列教程中学习到如何使用vagrant。在上一篇文章中我们使用了vagrant version/init/up/ssh等命令, 在本篇中对一些常用命令将做简单介绍.

命令一览

以下是vagrant1.8.5的命令一览。

命令详细解释
boxmanages boxes: installation, removal, etc.
connectconnect to a remotely shared Vagrant environment
destroystops and deletes all traces of the vagrant machine
global-statusoutputs status Vagrant environments for this user
haltstops the vagrant machine
helpshows the help for a subcommand
initinitializes a new Vagrant environment by creating a Vagrantfile
loginlog in to HashiCorp’s Atlas
packagepackages a running vagrant environment into a box
pluginmanages plugins: install, uninstall, update, etc.
portdisplays information about guest port mappings
powershellconnects to machine via powershell remoting
provisionprovisions the vagrant machine
pushdeploys code in this environment to a configured destination
rdpconnects to machine via RDP
reloadrestarts vagrant machine, loads new Vagrantfile configuration
resumeresume a suspended vagrant machine
shareshare your Vagrant environment with anyone in the world
snapshotmanages snapshots: saving, restoring, etc.
sshconnects to machine via SSH
ssh-configoutputs OpenSSH valid configuration to connect to the machine
statusoutputs status of the vagrant machine
suspendsuspends the machine
upstarts and provisions the vagrant environment
versionprints current and latest Vagrant version

vagrant status

使用status命令可以确认当前vagrant的状态

[root@host31 ~]# 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`.
[root@host31 ~]#


vagrant suspend

使用suspend命令可以将vm挂起, 下面执行后可以看到状态从running变成saved了。

[root@host31 ~]# vagrant suspend
==> default: Saving VM state and suspending execution...
[root@host31 ~]# vagrant status
Current machine states:

default                   saved (virtualbox)

To resume this VM, simply run `vagrant up`.
[root@host31 ~]#


vagrant halt

使用halt命令可以彻底关闭vm到poweroff状态。

[root@host31 ~]# vagrant halt
==> default: Discarding saved state of VM...
[root@host31 ~]# vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`
[root@host31 ~]#


vagrant reload vmname

使用reload命令可以重新启动vm,其后要跟vm名,因为目前只有一个default,所以vagrant reload default即可以将其再启动。

[root@host31 ~]# vagrant reload default
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> 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: 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: vagrant
default: SSH auth method: private key
default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
default: /vagrant => /root
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
[root@host31 ~]# 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`. [root@host31 ~]#


vagrant port

port命令将会列出和宿主机之间的port的mapping关系

[root@host31 ~]# vagrant port
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.

22 (guest) => 2222 (host)
[root@host31 ~]#


vagrant box

vagrant box下有6个命令,我们已经用过list命令确认vm列表,还有remove用以删除,以及add等

[root@host31 ~]# vagrant box
Usage: vagrant box <subcommand> [<args>]

Available subcommands:
add
list
outdated
remove
repackage
update

For help on any individual subcommand run `vagrant box <subcommand> -h`

[root@host31 ~]# vagrant box list
hashicorp/precise64 (virtualbox, 1.1.0)
[root@host31 ~]#


add命令失败由于网络状况下载时非常不安定而且超级慢,就不再演示了。

[root@host31 ~]# vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
box: URL: https://atlas.hashicorp.com/centos/7 This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) libvirt
2) virtualbox

Enter your choice: 2
==> box: Adding box 'centos/7' (v1607.01) for provider: virtualbox
box: Downloading: https://atlas.hashicorp.com/centos/boxes/7/versions/1607.01/providers/virtualbox.box An error occurred while downloading the remote file. The error
message, if any, is reproduced below. Please fix this error and try
again.

transfer closed with 494391624 bytes remaining to read
[root@host31 ~]#


另外还有vagrant destroy命令起到从停止到删除的一系列动作的命令,以及vagrant login等连接到HashiCorp的 操作,这里也不再一一赘述。

常用vagrant网址

说明URL
官网https://www.vagrantup.com/
box查询和下载https://atlas.hashicorp.com/boxes/search
box查询和下载http://www.vagrantbox.es/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vagrant init 命令