虚拟机相关的命令

1、init

Command: vagrant init [name [url]]

功能:用来初始化一个虚拟机的配置,在相应目录下,会生成一个Vagrantfile文件

案例:

  1. [root@vagrant ubuntu]# vagrant init ubuntu/xenial64
  2. A Vagrantfile has been placed in this directory. You are now
  3. ready to vagrant up your first virtual environment! Please read
  4. the comments in the Vagrantfile as well as documentation on
  5. vagrantup.com for more information on using Vagrant.
  6. [root@vagrant ubuntu]# ll
  7. total 4
  8. -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

  1. [root@vagrant vbox]# vagrant box add ubuntu16.04_louis /vbox/ubuntu-16.04_x86-64.box
  2. ==> box: Box file was not detected as metadata. Adding it directly...
  3. ==> box: Adding box 'ubuntu16.04_louis' (v0) for provider:
  4. box: Unpacking necessary files from: file:///vbox/ubuntu-16.04_x86-64.box
  5. ==> box: Successfully added box 'ubuntu16.04_louis' (v0) for 'virtualbox'!

2.3 修改vagrantfile文件

  1. [root@vagrant ubuntu]# vim Vagrantfile
  2. # -*- mode: ruby -*-
  3. # vi: set ft=ruby :
  4. # All Vagrant configuration is done below. The "2" in Vagrant.configure
  5. # configures the configuration version (we support older styles for
  6. # backwards compatibility). Please don't change it unless you know what
  7. # you're doing.
  8. Vagrant.configure("2") do |config|
  9. # The most common configuration options are documented and commented below.
  10. # For a complete reference, please see the online documentation at
  11. # https://docs.vagrantup.com.
  12. # Every Vagrant development environment requires a box. You can search for
  13. # boxes at https://atlas.hashicorp.com/search.
  14. #config.vm.box = "ubuntu/xenial64"
  15. config.vm.box = "ubuntu16.04_louis" (把这个更改成刚刚vagrant box add Name URL,这里指定的Name

2.4 启动系统

  1. [root@vagrant ubuntu]# vagrant up
  2. [root@vagrant ubuntu]# vagrant up
  3. Bringing machine 'default' up with 'virtualbox' provider...
  4. ==> default: Importing base box 'ubuntu16.04_louis'...
  5. ==> default: Matching MAC address for NAT networking...
  6. ==> default: Setting the name of the VM: ubuntu_default_1517250341422_88745
  7. ==> default: Clearing any previously set network interfaces...
  8. ==> default: Preparing network interfaces based on configuration...
  9. default: Adapter 1: nat
  10. ==> default: Forwarding ports...
  11. default: 22 (guest) => 2222 (host) (adapter 1)
  12. ==> default: Running 'pre-boot' VM customizations...
  13. ==> default: Booting VM...
  14. ==> default: Waiting for machine to boot. This may take a few minutes...
  15. default:
  16. address: 127.0.0.1:2222
  17. default: SSH username: vagrant
  18. default: SSH auth method: private key
  19. default:
  20. default: Vagrant insecure key detected. Vagrant will automatically replace
  21. default: this with a newly generated keypair for better security.
  22. default:
  23. default: Inserting generated public key within guest...
  24. default: Removing insecure key from the guest if it's present...
  25. default: Key inserted! Disconnecting and reconnecting using new SSH key...
  26. ==> default: Machine booted and ready!
  27. ==> default: Checking for guest additions in VM...
  28. default: The guest additions on this VM do not match the installed version of
  29. default: VirtualBox! In most cases this is fine, but in rare cases it can
  30. default: prevent things such as shared folders from working properly. If you see
  31. default: shared folder errors, please make sure the guest additions within the
  32. default: virtual machine match the version of VirtualBox you have installed on
  33. default: your host and reload your VM.
  34. default:
  35. default: Guest Additions Version: 5.0.40
  36. default: VirtualBox Version: 5.1
  37. ==> default: Mounting shared folders...
  38. default: /vagrant => /vagrant/ubuntu

3、status 查看状态

  1. [root@vagrant ubuntu]# vagrant status
  2. Current machine states:
  3. default running (virtualbox)
  4. The VM is running. To stop this VM, you can run vagrant halt to
  5. shut it down forcefully, or you can run vagrant suspend to simply
  6. suspend the virtual machine. In either case, to restart it again,
  7. simply run vagrant up.

4、ssh

功能: ssh登录到虚拟机

  1. [root@vagrant ubuntu]# vagrant ssh
  2. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64)
  3. Documentation: https://help.ubuntu.com
  4. Management: https://landscape.canonical.com
  5. Support: https://ubuntu.com/advantage
  6. Get cloud support with Ubuntu Advantage Cloud Guest:
  7. http://www.ubuntu.com/business/services/cloud
  8. 0 packages can be updated.
  9. 0 updates are security updates.
  10. vagrant@ubuntu-xenial:~$ sudo apt-get update

5、suspend

功能:挂起虚拟机

案例: 第二节:虚拟机相关的命令 - 图1

6、reload

vagrant reload

功能:重启虚拟机

案例: 第二节:虚拟机相关的命令 - 图2

7、halt

vagrant halt

功能:关闭虚拟机

案例: 第二节:虚拟机相关的命令 - 图3

8、destroy

vagrant destroy

功能:删除虚拟机