Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.
installing and using vagrant on fedora 26
#sudo dnf install vagrant -y Last metadata expiration check: 2:10:16 ago on Sunday 25 March 2018 06:58:56 PM IST. Package vagrant-1.9.1-3.fc26.noarch is already installed, skipping. Dependencies resolved. Nothing to do. Complete!
initialize vagarant file with
$ vagrant init centos/7 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.
#installing vagrant plugins
$vagrant plugin install vagrant-rekey-ssh ; vagrant plugin install vagrant-mutate ; vagrant plugin install vagrant-libvirt
$lets edit the Vagarntfile file and add some configuration options.
$cat Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.define :one do |one| one.vm.box = "centos/7" one.vm.box_check_update = false one.vm.network "forwarded_port", guest: 80, host: 8080 one.vm.network "private_network", ip: "192.168.122.100" one.vm.hostname = "one.vagrant.box" # config.vm.network "public_network" # config.vm.synced_folder "../data", "/vagrant_data" config.vm.provider "libvirt" do |libvirt| libvirt.connect_via_ssh = false # also needed libvirt.username = "cipher" libvirt.storage_pool_name = "default" libvirt.memory = "420" libvirt.driver = "kvm" end end config.vm.define :two do |two| two.vm.box = "centos/7" two.vm.box_check_update = false two.vm.network "forwarded_port", guest: 80, host: 8081 two.vm.network "private_network", ip: "192.168.122.101" two.vm.hostname = "two.vagrant.box" config.vm.provider "libvirt" do |libvirt| libvirt.connect_via_ssh = false # also needed libvirt.username = "cipher" libvirt.storage_pool_name = "default" libvirt.memory = "420" libvirt.driver = "kvm" end end end
spinning up vms from vagrant file,
$vagrant up Bringing machine 'one' up with 'libvirt' provider... Bringing machine 'two' up with 'libvirt' provider... ==> two: Creating image (snapshot of base box volume). ==> one: Creating image (snapshot of base box volume). ==> one: Creating domain with the following settings... ==> two: Creating domain with the following settings... ==> one: -- Name: cipher_one ==> one: -- Domain type: kvm ==> two: -- Name: cipher_two ==> two: -- Domain type: kvm ==> one: -- Cpus: 1 ==> one: -- Memory: 420M ==> two: -- Cpus: 1 ==> one: -- Management MAC: ==> two: -- Memory: 420M ==> two: -- Management MAC: ==> one: -- Loader: ==> two: -- Loader: ==> one: -- Base box: centos/7 ==> two: -- Base box: centos/7 ==> one: -- Storage pool: default ==> two: -- Storage pool: default ==> one: -- Image: /var/lib/libvirt/images/cipher_one.img (41G) ==> two: -- Image: /var/lib/libvirt/images/cipher_two.img (41G) ==> one: -- Volume Cache: default ==> two: -- Volume Cache: default ==> one: -- Kernel: ==> two: -- Kernel: ==> one: -- Initrd: ==> two: -- Initrd: ==> one: -- Graphics Type: vnc ==> one: -- Graphics Port: 5900 ==> two: -- Graphics Type: vnc ==> two: -- Graphics Port: 5900 ==> one: -- Graphics IP: 127.0.0.1 ==> one: -- Graphics Password: Not defined ==> two: -- Graphics IP: 127.0.0.1 ==> one: -- Video Type: cirrus ==> two: -- Graphics Password: Not defined ==> two: -- Video Type: cirrus ==> one: -- Video VRAM: 9216 ==> one: -- Keymap: en-us ==> two: -- Video VRAM: 9216 ==> two: -- Keymap: en-us ==> one: -- TPM Path: ==> one: -- INPUT: type=mouse, bus=ps2 ==> two: -- TPM Path: ==> two: -- INPUT: type=mouse, bus=ps2 ==> one: -- Command line : ==> two: -- Command line : ==> one: Creating shared folders metadata... ==> two: Creating shared folders metadata... ==> one: Starting domain. ==> two: Starting domain. ==> one: Waiting for domain to get an IP address... ==> two: Waiting for domain to get an IP address... ==> two: Waiting for SSH to become available... ==> one: Waiting for SSH to become available... two: two: Vagrant insecure key detected. Vagrant will automatically replace two: this with a newly generated keypair for better security. one: one: Vagrant insecure key detected. Vagrant will automatically replace one: this with a newly generated keypair for better security. two: two: Inserting generated public key within guest... one: one: Inserting generated public key within guest... two: Removing insecure key from the guest if it's present... one: Removing insecure key from the guest if it's present... two: Key inserted! Disconnecting and reconnecting using new SSH key... one: Key inserted! Disconnecting and reconnecting using new SSH key... ==> two: [vagrant-hostsupdater] Checking for host entries ==> two: [vagrant-hostsupdater] Writing the following entries to (/etc/hosts) ==> two: [vagrant-hostsupdater] 192.168.122.101 two.vagrant.box # VAGRANT: c321765982544b2830cae008921d606f (two) / ed103e25-ec1b-4910-8bdb-b78ee3b115f1 ==> two: [vagrant-hostsupdater] This operation requires administrative access. You may skip it by manually adding equivalent entries to the hosts file. ==> two: Setting hostname... ==> one: [vagrant-hostsupdater] Checking for host entries ==> one: [vagrant-hostsupdater] Writing the following entries to (/etc/hosts) ==> one: [vagrant-hostsupdater] 192.168.122.100 one.vagrant.box # VAGRANT: 916262da6c9a74a83a8ef0c3e4c5209e (one) / d8ed1c7e-f8aa-4a2e-b73e-4cecfc32785f ==> one: [vagrant-hostsupdater] This operation requires administrative access. You may skip it by manually adding equivalent entries to the hosts file. ==> one: Setting hostname... ==> two: Forwarding ports... ==> two: 80 (guest) => 8081 (host) (adapter eth0) ==> one: Forwarding ports... ==> one: 80 (guest) => 8080 (host) (adapter eth0) ==> two: Configuring and enabling network interfaces... ==> one: Configuring and enabling network interfaces... ==> two: Rsyncing folder: /home/cipher/ => /vagrant ==> one: Rsyncing folder: /home/cipher/ => /vagrant
Checking vagrant status
$ vagrant status Current machine states: one running (libvirt) two running (libvirt) This environment represents multiple VMs. The VMs are all listed above with their current state. For more information about a specific VM, run `vagrant status NAME`.
To access running vms we can use vagrant ssh command.
$vagrant ssh one [vagrant@one ~]$ hostname one.vagrant.box [vagrant@one ~]$ exit logout Connection to 192.168.121.220 closed. $ vagrant ssh two [vagrant@two ~]$ hostname two.vagrant.box [vagrant@two ~]$ exit logout Connection to 192.168.121.32 closed.