购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

3.2 Vagrant常用命令

注意事项: 由于默认情况下,Vagrant会把临时文件和Box文件存放在用户主目录里。如果Box文件过大,会导致占用过大空间,可以通过设置环境变量VAGRANT_HOME来设定Vagrant的主目录路径。Vagrant默认设置的主目录为用户主目录的.vagrant.d文件夹。本次实验时把此环境变量设置为D:\vagrant\home,关于各个操作系统的环境变量的设置,请查阅相关文档。

1.基本命令

基础命令总览:

·box add:导入box

·box list:查看box

·box remove:删除box

·init:初始化

·up:启动

·status:查看状态

·ssh:SSH连接

·reload:重载

·halt:关闭

·suspend:暂停

·destroy:删除

下面举例说明这些命令的使用方法。

(1)导入Box

导入已经下载的Box命令如下:


$ vagrant box add --name centos-7.4-base /f/vagrant/box/centos-7.4-base.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos-7.4-base' (v0) for provider:
    box: Unpacking necessary files from: file://F:/vagrant/box/centos-7.4-base.box
    box:
==> box: Successfully added box 'centos-7.4-base' (v0) for 'virtualbox'!

/f/vagrant/box/centos-7.4-base.box表示Box的路径地址,这是由于使用了Git Bash的路径表示方法。如果使用CMD命令行,使用F:/vagrant/box/centos-7.4-base.box即可。此处使用的Box从第2章“实验说明”中提供的百度云盘链接上下载。

(2)查看Box

查看可用的Box如下所示:


$ vagrant box list
centos-7.4-base      (virtualbox, 0)

(3)删除Box

删除不再使用的Box如下所示:


$ vagrant box remove centos-7.4-base
Removing box 'centos-7.4-base' (v0) with provider 'virtualbox'...

(4)初始化

初始化虚拟机如下所示:


$ mkdir test
$ cd test
$ vagrant init centos-7.4-base
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.

查看未被注释配置文件内容:


$ cat Vagrantfile | tr -d '\r' | egrep -v '^#|^ +#|^$'
Vagrant.configure("2") do |config|
    config.vm.box = "centos-7.4-base"
end

(5)启动

启动虚拟机如下所示:


$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos-7.4-base'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: test_default_1543216915133_85015
==> 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:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.

(6)查看状态

查看虚拟机状态如下所示:


$ 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'.

(7)SSH连接

注意,如果Windows下使用Git Bash时无法使用SSH连接虚拟机,可以尝试使用系统自己带的命令行工具CMD连接虚拟机:


$ vagrant ssh
Last login: Tue Dec 12 16:01:23 2017 from 10.0.2.2
[vagrant@localhost ~]$ hostname
localhost.localdomain
[vagrant@localhost ~]$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:ca:e4:8b brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
       valid_lft 86194sec preferred_lft 86194sec
    inet6 fe80::5054:ff:feca:e48b/64 scope link
       valid_lft forever preferred_lft forever
[vagrant@localhost ~]$ exit;
logout
Connection to 127.0.0.1 closed.

(8)重新

当我们编辑当前文件夹下的虚拟机配置文件Vagrantfile后,可以使用reload命令重载虚拟机,使配置生效。比如添加设置主机名的配置:


...
config.vm.box = "centos-7.4-base"
config.vm.hostname = "istio"
...

重载使配置文件生效:


$ vagrant reload
==> default: Attempting graceful shutdown of VM...
Connection to 127.0.0.1 closed by remote host.
==> 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: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Machine already provisioned. Run 'vagrant provision' or use the '--provision'
==> default: flag to force provisioning. Provisioners marked to run always will still run.
$ vagrant ssh
Last login: Tue Dec 12 16:11:12 2017 from 10.0.2.2
[vagrant@istio ~]$ hostname
istio
[vagrant@istio ~]$ exit;
logout
Connection to 127.0.0.1 closed.

(9)关闭

关闭虚拟机如下所示:


$ vagrant halt
==> default: Attempting graceful shutdown of VM...

(10)暂停

由于上一步骤关闭虚拟机,执行本步骤时需要先启动虚拟机,然后再暂停虚拟机:


$ vagrant suspend
==> default: Saving VM state and suspending execution...

(11)删除

删除虚拟机的命令如下所示:


$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Discarding saved state of VM...
==> default: Destroying VM and associated drives...

2.使用虚拟机快照命令

虚拟机快照命令如下:

·save:保存虚拟机快照。

·list:查看虚拟机快照。

·restore:用快照恢复虚拟机。

·delete:删除虚拟机快照。

进行如下快照的相关操作时,需要先创建虚拟机并启动虚拟机。

保存虚拟机快照示例:


$ vagrant snapshot save istio
==> default: Snapshotting the machine as 'istio'...
==> default: Snapshot saved! You can restore the snapshot at any time by
==> default: using 'vagrant snapshot restore'. You can delete it using
==> default: 'vagrant snapshot delete'.

查看虚拟机快照示例:


$ vagrant snapshot list
istio

用快照恢复虚拟机示例:


$ vagrant snapshot restore istio
==> default: Forcing shutdown of VM...
==> default: Restoring the snapshot 'istio'...
==> default: Resuming suspended VM...
==> 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: Machine booted and ready!

删除虚拟机快照示例: 90SFLPz0KOOoEKVCWLopHQZPtmIW7k/M4VIDIJtiCYNtB0bsInLplRCyrJ6n+YR9


$ vagrant snapshot delete istio
==> default: Deleting the snapshot 'istio'...
==> default: Snapshot deleted!

点击中间区域
呼出菜单
上一章
目录
下一章
×