从已有的vagrant虚拟机中打包镜像

使用 vagrant 的好一个好处就是能够想协做中分享咱们的 Boxhtml

打包

下面就介绍一下打包方式git

首先肯定须要打包哪台虚拟机,进入安装 VirtualBox 的安装目录github

默认在这个路径 C:\Program Files\Oracle\VirtualBox
cd "C:\Program Files\Oracle\VirtualBox"

查看全部的虚拟机,能够看到有显示格式以下: "vmname" {vmid} ,咱们这里须要的是 vmnameshell

> ./VBoxManage.exe list vms
"django-twitter_default_1619620682201_12310" {b3c74405-5d50-4f05-9f0e-98c9563f1fa5}

将目标切换到你须要的路径中,而后执行如下的目录django

vagrant package --base django-twitter_default_1619620682201_12310 --output demo_ubuntu18.box

好了,这个时候你的路径下就会生成一个 demo_ubuntu18.box 的文件ubuntu

使用打包的好的镜像

光有打包是不够的,由于打包的目的是为了使用 box 镜像。使用的时候有两个注意点,那就是由于 ssh 密钥的缘故,你 add ,而后 up 以后会发现进不去,这是就须要去修改 vagrantfile 这个文件,将默认的密钥登陆改成帐号密码登陆。安全

ssh 密钥错误以下app

> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/bionic64_demo_ubuntu18'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: twitter-term-1_default_1623336326135_46711
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: You are trying to forward to privileged ports (ports <= 1024). Most
==> default: operating systems restrict this to only privileged process (typically
==> default: processes running as an administrative user). This is a warning in case
==> default: the port forwarding doesn't work. If any problems occur, please try a
==> default: port higher than 1024.
==> default: Forwarding ports...
    default: 8000 (guest) => 80 (host) (adapter 1)
    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: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
    default: Warning: Authentication failure. Retrying...
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
可是帐号密码登陆不如密钥登陆安全,因此第一次(帐号+密码)登陆以后,建议改成密钥登录

初始化

若是你尚未 vagrantfile 文件,才须要初始化操做ssh

你能够复用老虚拟机的 vagrantfile 文件,或者执行初始化操做新建一个 vagrantfile 文件
vagrant init [box_name]

添加 box

vagrant box add {box_name} {box_file_path}

参考文章:

利用vagrant打包系统--制做本身的box
Vagrant+Miniconda+PyCharm
Vagrant ssh authentication failure
"Warning: Authentication failure. Retrying... " after packaging box #5186ionic