Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active June 8, 2022 06:19
Show Gist options
  • Select an option

  • Save maxivak/c318fd085231b9ab934e631401c876b1 to your computer and use it in GitHub Desktop.

Select an option

Save maxivak/c318fd085231b9ab934e631401c876b1 to your computer and use it in GitHub Desktop.
Vagrant with Ubuntu 16.04 in VirtualBox

Setup Ubuntu 16.04 to be used with Vagrant and Virtualbox

Prepare Vagrant box with Ubuntu 16.04

We will use official box "ubuntu/xenial64" and modify it to work with Vagrant.

  • Vagrantfile

Vagrant.configure(2) do |config|

  config.vm.box = "ubuntu/xenial64"

  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 = "1524"
  end


end

  • run Virtual machine (VM)
vagrant up
  • login to VM
vagrant ssh

Hostname

  • fix hostname in /etc/hosts
127.0.0.1 ubuntu-xenial

This will fix error:

"sudo: unable to resolve host ubuntu-xenial

Rename interfaces

  • add (or modify) line in /etc/default/grub
GRUB_CMDLINE_LINUX=" net.ifnames=0 biosdevname=0"
  • update
sudo update-grub
  • so we will have interface named 'eth1'

  • reboot VM

vagrant reload

Install python

  • To use Ansible we need to install Python 2
sudo apt-get install python

Create new box

  • commit the box
vagrant package

it will create *.box file in the current directory.

You can use this box file to add to the local vagrant repository or copy it to another server.

  • use new box in Vagrantfile
Vagrant.configure(2) do |config|

  config.vm.box = "my/ubuntu-xenial64"
  
  ..
end  

Setup VM

hostname

Setup network

  • we will use our new box with python 2 and renamed interface.
  • And we will use Ansible to provision VM

Static IP

  • we will add a new interface 'eth1' with static IP

  • Vagrantfile

HOSTNAME = 'debugserver'
IP = "51.0.99.120"
NETWORK_MASK = "255.0.0.0"
NETWORK_GATEWAY = "51.0.0.1"


Vagrant.configure(2) do |config|

  config.vm.box = "my/ubuntu-xenial64"
  
  # IP
  config.vm.network "public_network", auto_config: false, :bridge => "eth0", ip: "#{IP}"

  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 = "1524"
  end


  config.vm.provision "ansible" do |p|
    p.playbook = "playbook.yml"
    p.verbose        = true
    p.extra_vars =
        {
            machine: "main",
            public_ip: IP,
            public_mask: NETWORK_MASK,
            public_gateway: NETWORK_GATEWAY,
        }
  end
  
end  
@itbj
Copy link
Copy Markdown

itbj commented May 8, 2017

OK. 谢谢。

@ankitgaur-tudip
Copy link
Copy Markdown

What is the root password to put when you do, su - ?

@codelinx
Copy link
Copy Markdown

look at the 'vagrant up' output it has a section that shows
"...
default: SSH username: ubuntu
default: SSH auth method: password
..."

@codelinx
Copy link
Copy Markdown

codelinx commented Jun 20, 2017

@ankitgaur-tudip use 'sudo su -' to switch to root then just type 'passwd' to set a root password.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment