zoukankan      html  css  js  c++  java
  • 15.【原创】使用vagrant给VirtualBox安装linux镜像

    vagrant是一个工具,用于创建和部署虚拟化开发环境的。使用它初始化linux实例会非常方便,话不多说,一起来看看具体操作步骤吧。

    1.下载安装VirtualBox

      直接到VirtualBox的官网下载即可,基本没什么难度,安装也是傻瓜式的,只是要开启主板Bois中的虚拟化选项(官网:https://www.virtualbox.org/wiki/Downloads)。

    2.下载安装Vagrant

      2.1.Windows下安装Vagrant也是比较简单的,选定好安装目录狂点下一步即可,安装好后需要重启一下电脑(地址:https://www.vagrantup.com/downloads)。

      2.2.重启电脑后,打开cmd或者powershell输入"vagrant"指令稍等一会,如果出现如下操作指令提示则代表安装成功:

    Microsoft Windows [版本 10.0.18362.900]
    (c) 2019 Microsoft Corporation。保留所有权利。
    
    C:UsersNick>vagrant
    Usage: vagrant [options] <command> [<args>]
    
        -h, --help                       Print this help.
    
    Common commands:
         box             manages boxes: installation, removal, etc.
         cloud           manages everything related to Vagrant Cloud
         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
         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
         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
         upload          upload to machine via communicator
         validate        validates the Vagrantfile
         version         prints current and latest Vagrant version
         winrm           executes commands on a machine via WinRM
         winrm-config    outputs WinRM configuration to connect to the machine
    
    For help on any individual command run `vagrant COMMAND -h`
    
    Additional subcommands are available, but are either more advanced
    or not commonly used. To see all subcommands, run the command
    `vagrant list-commands`.
            --[no-]color                 Enable or disable color output
            --machine-readable           Enable machine readable output
        -v, --version                    Display Vagrant version
            --debug                      Enable debug output
            --timestamp                  Enable timestamps on log output
            --debug-timestamp            Enable debug output with timestamps
            --no-tty                     Enable non-interactive output
    

      

      2.3.在cmd(以管理员身份运行)中切换到存放镜像的目录下,输入"vagrant init + 镜像名称"即可初始化镜像配置文件到VirtualBox中,其中镜像名称可到vagrant的官网查看,此处我用的是"ubuntu/trusty64"(地址:https://app.vagrantup.com/boxes/search?provider=virtualbox)。出现如下提示代表已经初始化配置文件了,或者到对应目录查看该文件是否生成。(此处“images”为镜像安装文件夹,后面步骤3.1中镜像安装地址切换为“VirtualBoxVMs”)

    G:Vagrantimages>vagrant init ubuntu/trusty64
    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.

       

      2.4.接着输入"vagrant up"命令启动镜像,第一次启动时因为没有镜像文件,vagrant会到其远程仓库下载对应的.box后缀的镜像,下载完成之后会启动镜像并且默认将"vagrant"设置为该镜像的管理员,我们使用"vagrant ssh"进行连接或者使用ssh工具连接即可。但是实际操作中发现在国内从vagrant官方仓库中下载镜像是在是太慢了。所以现在有两个解决方案,一是采用传统方式使用linux的.ios文件和VirtualBox的安装向导安装,但是这种方式不在本次讨论的范围(可参考此文下载阿里的镜像:https://www.cnblogs.com/Nick-Hu/p/13277154.html)。第二种方式就是下载好.box镜像到本地,使用vagrant导入本地的镜像,接下来着重讨论此种方式。

    3..使用下列的国内镜像地址下载需要的.box镜像,使用vagrant进行本地添加。

    CentOS 6/7/8 修改链接中的版本号可以下载不同版本 http://mirrors.ustc.edu.cn/centos-cloud/centos/6/vagrant/x86_64/images/
    Ubuntu Server 14.04 https://mirrors.ustc.edu.cn/ubuntu-cloud-images/server/vagrant/trusty/current/
    Ubuntu Server 16.04 https://mirrors.ustc.edu.cn/ubuntu-cloud-images/server/xenial/current/
    Ubuntu Server 18.04 https://mirrors.ustc.edu.cn/ubuntu-cloud-images/server/bionic/current/
    Ubuntu Server 20.04 http://mirrors.ustc.edu.cn/ubuntu-cloud-images/focal/current/
    

      3.1.下载好.box镜像之后将镜像文件移动到镜像存放目录使用下列命令将此镜像添加到vagrant的镜像列表中(其中,VirtualBoxVMs为镜像安装目录,指令格式:vagrant box add {镜像实例名称} {镜像路径})。

    G:VirtualBoxVMs>vagrant box add ubuntu16.04.001 xenial-server-cloudimg-amd64-vagrant.box
    ==> box: Box file was not detected as metadata. Adding it directly...
    ==> box: Adding box 'ubuntu16.04.001' (v0) for provider:
        box: Unpacking necessary files from: file://G:/VirtualBoxVMs/xenial-server-cloudimg-amd64-vagrant.box
        box:
    ==> box: Successfully added box 'ubuntu16.04.001' (v0) for 'virtualbox'!
    
    G:VirtualBoxVMs>vagrant init ubuntu16.04.001
    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.

      3.2.初始化镜像的配置文件("ubuntu16.04.001"为3.1步骤中自定的镜像实例名称)

    G:VirtualBoxVMs>vagrant init ubuntu16.04.001
    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.
    

      

      3.3.启动镜像实例("SSH address: 127.0.0.1:2222"为镜像实例的地址和端口,"SSH username: vagrant"中的初始化超级管理员的账户名和密码)

    G:VirtualBoxVMs>vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    ==> default: Importing base box 'ubuntu16.04.001'...
    ==> default: Matching MAC address for NAT networking...
    ==> default: Setting the name of the VM: VirtualBoxVMs_default_1594313488822_28629
    Vagrant is currently configured to create VirtualBox synced folders with
    the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
    guest is not trusted, you may want to disable this option. For more
    information on this option, please refer to the VirtualBox manual:
    
      https://www.virtualbox.org/manual/ch04.html#sharedfolders
    
    This option can be disabled globally with an environment variable:
    
      VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
    
    or on a per folder basis within the Vagrantfile:
    
      config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
    ==> 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: SSH address: 127.0.0.1:2222
        default: SSH username: vagrant
        default: SSH auth method: private key
        default: Warning: Connection reset. Retrying...
        default: Warning: Connection aborted. Retrying...
        default: Warning: Remote connection disconnect. Retrying...
        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.1.38
        default: VirtualBox Version: 6.1
    ==> default: Mounting shared folders...
        default: /vagrant => G:/VirtualBoxVMs
    

      

      3.4.使用"vagrant ssh"指令访问镜像实例

    G:VirtualBoxVMs>vagrant ssh
    Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-184-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    
    
    0 packages can be updated.
    0 updates are security updates.
    
    New release '18.04.4 LTS' available.
    Run 'do-release-upgrade' to upgrade to it.
    
    
    Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-184-generic x86_64)
    
     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/advantage
    
    
    0 packages can be updated.
    0 updates are security updates.
    
    New release '18.04.4 LTS' available.
    Run 'do-release-upgrade' to upgrade to it.
    
    vagrant@ubuntu-xenial:/$ ls
    bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv  tmp  vagrant  vmlinuz
    boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  sys  usr  var      vmlinuz.old
    
    vagrant@ubuntu-xenial:/$ exit
    logout
    Connection to 127.0.0.1 closed.
    

      

    4.【附】Vagrant常用命令

    序号 命令 解释 
    1  vagrant box list  查看目前已有的box
    2  vagrant box add  新增加一个box
    3  vagrant box remove  删除指定box
    4  vagrant init  初始化配置vagrantfile
    5  vagrant up  启动虚拟机
    6  vagrant ssh ssh  登录虚拟机
    7  vagrant suspend  挂起虚拟机
    8  vagrant reload  重启虚拟机
    9  vagrant halt  关闭虚拟机
    10  vagrant status  查看虚拟机状态
    11  vagrant destroy  删除虚拟机
  • 相关阅读:
    《0908-构建之法:现代软件工程-阅读笔记》
    配置fabric-crashlytics教程
    iOS开发工具——统计Crash的工具Crashlytics
    Overview of iOS Crash Reporting Tools: Part 2/2
    Overview of iOS Crash Reporting Tools: Part 1/2
    添加自签发的 SSL 证书为受信任的根证书
    UIWebView to view self signed websites (No private api, not NSURLConnection)
    Has anybody found a way to load HTTPS pages with an invalid server certificate using UIWebView?
    AFNetworking 2.0 Tutorial
    USING CHARLES FROM AN IPHONE
  • 原文地址:https://www.cnblogs.com/Nick-Hu/p/13276594.html
Copyright © 2011-2022 走看看