1. 程式人生 > >Vagrant使用指南:Vagrant命令使用簡介

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

這裡寫圖片描述

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

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網址