1、init
Command: vagrant init [name [url]]
功能:用来初始化一个虚拟机的配置,在相应目录下,会生成一个Vagrantfile文件
案例:
[root@vagrant ubuntu]# 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.
[root@vagrant ubuntu]# ll
total 4
-rw-r--r--. 1 root root 3019 Jan 30 01:34 Vagrantfile
2、up
Command: vagrant up
功能:根据目录下的Vagrantfile文件启动虚拟机,第一次会去网上下载虚拟机,最好先下载下来
案例:
1)、由于第一次运行,下载速度慢,我事先把box文件放到七牛云咯
http://soft.51yuki.cn/ubuntu-16.04_x86-64.box
2)、把刚刚下载的添加到vagrant
[root@vagrant vbox]# vagrant box add ubuntu16.04_louis /vbox/ubuntu-16.04_x86-64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu16.04_louis' (v0) for provider:
box: Unpacking necessary files from: file:///vbox/ubuntu-16.04_x86-64.box
==> box: Successfully added box 'ubuntu16.04_louis' (v0) for 'virtualbox'!
3)、修改vagrantfile文件
~~~
[root@vagrant ubuntu]# vim Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
#config.vm.box = "ubuntu/xenial64"
config.vm.box = "ubuntu16.04_louis" (把这个更改成刚刚vagrant box add Name URL,这里指定的Name)
~~~
4)、启动系统
[root@vagrant ubuntu]# vagrant up
~~~
[root@vagrant ubuntu]# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu16.04_louis'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu_default_1517250341422_88745
==> 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:
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: 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: 5.0.40
default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
default: /vagrant => /vagrant/ubuntu
~~~
3、status 查看状态
[root@vagrant ubuntu]# 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`.
4、ssh
功能: ssh登录到虚拟机
[root@vagrant ubuntu]# vagrant ssh
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
0 packages can be updated.
0 updates are security updates.
vagrant@ubuntu-xenial:~$ sudo apt-get update
5、suspend
功能:挂起虚拟机
案例:
![](https://box.kancloud.cn/d520c8188f1fe692296a0edf93fb6d8a_1167x561.png)
6、reload
vagrant reload命令
功能:重启虚拟机
案例:
![](https://box.kancloud.cn/eefefa10c790ee5a36496f97e748f697_1259x562.png)
7、halt
vagrant halt
功能:关闭虚拟机
案例:
![](https://box.kancloud.cn/b1a161dafeafef89633f0a61a63dd4cb_1227x606.png)
8、destroy
vagrant destroy
功能:删除虚拟机
- 第一章:Vagrant基础介绍
- 第二章:Vagrant的安装部署
- 第一节:在centos7.3的系统上安装部署vagrant
- 第二节:在windows server 2008 R2的系统上安装部署vagrant
- 第三章:vagrant常见命令
- 第一节:box
- 第二节:虚拟机相关的命令
- 第四章:vagrant管理虚拟机
- 第一节:初始化centos6的系统
- 第二节:初始化ubuntu16.04的系统
- 第三节:打包自己的box
- 第五章:Vagrant之网络配置
- 第一节:通过Forwarded Ports(转发端口)
- 第二节:Private Networks(私有网络)
- 第三节:Public Networks(公有网络)
- 第六章:常见错误
- 第七章:Vagrant之文件同步方式
- 第一节:synced_folder(把物理机的目录同步到虚拟机里某个目录)
- 第二节:通过NFS共享
- 第三节:通过rsync共享
- 第八章:Vagrantfile配置文件