虚拟机相关的命令
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文件启动虚拟机,第一次会去网上下载虚拟机,最好先下载下来
案例:
2.1 由于第一次运行,下载速度慢,我事先把box文件放到七牛云咯
http://soft.51yuki.cn/ubuntu-16.04_x86-64.box
2.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'!
2.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)
2.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
功能:挂起虚拟机
案例:
6、reload
vagrant reload
功能:重启虚拟机
案例:
7、halt
vagrant halt
功能:关闭虚拟机
案例:
8、destroy
vagrant destroy
功能:删除虚拟机