vagrant上安装ubuntu

vagrant

apt install vagrant

box

官网

可以去官网查找需要的box

vagrant box add generic/ubuntu1804

执行上面的命令可以下载一个box,这里会有一个重定向的过程,实际上文件是从提供方的服务器上下载,而不是vagrant的服务器上下载。并且这里不像docker一样有官方的服务,都是第三方提供。
这个box和docker里image概念比较像。文件会下载到默认位置,也是使用类似aufs的文件管理方式来应对多个实例

国内镜像

以下载ubuntu18.04为例

vagrant box add \
https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box \
--name ubuntu/bionic

instance

创建一个空文件夹,进去

vagrant init generic/ubuntu1804

如果是使用清华的box则执行下面的

vagrant init ubuntu/bionic

执行上面的命令后会在文件夹里创建一个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://vagrantcloud.com/search.
  config.vm.box = "generic/ubuntu1804"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

然后执行下面命令启动

vagrant up

但是可能会报错:

Bringing machine 'default' up with 'libvirt' provider...
Error while connecting to libvirt: Error making a connection to libvirt URI qemu:///system?no_verify=1&keyfile=/root/.ssh/id_rsa:
Call to virConnectOpen failed: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory

livirt

根据网上的方案执行下面命令安装一个第三方库

apt install -y libvirt-daemon-system

virtualbox

另一个方法是使用virtualbox

apt install -y virtualbox
apt install -y virtualbox-dkms
systemctl start virtualbox

vagrant up

启动成功后控制台会打印一些信息
通过vagrant ssh进入虚拟机,然后sudo su直接切换root账号

网络

通过修改Vagrantfile写入下面两行开启dhcp获取独立ip的形式(第二行是本来就由,取消注释就可以了),更多功能参见官网

Vagrant.configure("2") do |config|
  config.vm.network "public_network",
    use_dhcp_assigned_default_route: true
end

然后启动的时候会提示使用哪个网卡,选物理网卡即可。启动后使用ip a会看到三条记录,除了lo就是一个nat和一个bridged。

内存&CPU

看上去vagrant使用的内存单位是M,可以通过打开Vagrantfile来修改内存和CPU

config.vm.provider "virtualbox" do |v|
  v.memory = 8192
  v.cpus = 2
end

主要下面end的注释要打开。
更全面的配置(例如hostname)可以参考官网说明

你可能感兴趣的:(linux)