Vagrant是用ruby写的一个工具, 它的出现是为了更加容易的解决开发环境的一致性问题. 我们将会在本系列教程中学习到如何使用vagrant。在上一篇文章中我们使用了vagrant version/init/up/ssh等命令, 在本篇中对一些常用命令将做简单介绍.
以下是vagrant1.8.5的命令一览。
命令 | 详细解释 |
---|---|
box | manages boxes: installation, removal, etc. |
connect | connect to a remotely shared Vagrant environment |
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 Atlas |
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 |
share | share your Vagrant environment with anyone in the world |
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 |
version | prints current and latest Vagrant version |
使用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 ~]#
使用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 ~]#
使用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 ~]#
使用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 ~]#
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下有6个命令,我们已经用过list命令确认vm列表,还有remove用以删除,以及add等
[root@host31 ~]# vagrant box
Usage: vagrant box []
Available subcommands:
add
list
outdated
remove
repackage
update
For help on any individual subcommand run `vagrant box -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的 操作,这里也不再一一赘述。
说明 | URL |
---|---|
官网 | https://www.vagrantup.com/ |
box查询和下载 | https://atlas.hashicorp.com/boxes/search |
box查询和下载 | http://www.vagrantbox.es/ |