Vagrant IP, RAM & CPU

Vagrant IP, RAM & CPU | Vagrant is an open-source tool that simplifies the process of managing virtualized environments. It provides an easy-to-use command-line interface for creating, configuring, and managing virtual machines (VMs).

Vagrant General Commands

Before making any specific changes, it’s important to know these general commands:

  • Reload Vagrant Environment: After making changes to the Vagrantfile, use this command to apply the changes.
vagrant reload
  • Start Vagrant Environment: To start the Vagrant-managed virtual machine.
vagrant up
  • SSH into Vagrant Environment: To connect to the VM via SSH.
vagrant ssh
  • Suspend Vagrant Environment: To save the current state of the VM and stop it.
vagrant suspend
  • Resume Vagrant Environment: To resume a suspended VM.
vagrant resume
  • Stop Vagrant Environment: To shut down the VM.
vagrant halt

Listing Active Vagrant Environments

To get all the active Vagrant environments, use the following command:

vagrant global-status

Example output:

id       name    provider   state    directory
-------------------------------------------------------------------------
5197c82  default virtualbox poweroff C:/install/vagrant-vms/centos
c487b80  default virtualbox poweroff C:/install/vagrant-vms/ubuntu

Destroying a Vagrant Environment

To destroy a Vagrant environment, navigate to the directory containing the Vagrantfile and run:

vagrant destroy --force

Understanding Vagrant Directories

In the folder where your Vagrantfile is located, there will be a hidden directory .vagrant:

cd ubuntu/
ls -a

Output:

./  ../  .vagrant/  Vagrantfile

The .vagrant directory stores local state and metadata for your Vagrant environment, such as information about the VM state, provider-specific data (like VirtualBox), and the unique identifier for the VM.

Vagrant also creates a global home directory at ~/.vagrant.d:

ls ~/.vagrant.d/

Output:

boxes/  data/  gems/  insecure_private_key  insecure_private_keys/
rgloader/  setup_version  tmp/

The ~/.vagrant.d directory contains global configuration and metadata, such as:

  1. Boxes: Downloaded Vagrant box files and their metadata.
  2. Plugins: Installed Vagrant plugins and their configurations.
  3. Logs: Logs related to Vagrant’s operations.
  4. Data: Various global state files, such as default provider settings, insecure private key, and license files.
  5. Caches: Caches for operations like box downloads to improve performance.

Editing the Vagrantfile

Before making changes, ensure all your Vagrant environments are destroyed. You can use any text editor to open the Vagrantfile. In this file, # denotes comments. The Vagrantfile is written in Ruby, but you don’t need to know Ruby to understand it.

Example snippet from Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  # config.vm.box_check_update = false
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
end

The line config.vm.box = "ubuntu/jammy64" specifies the box. Every Vagrant development environment requires a box. You can search for boxes at Vagrant Cloud.

Configuring Vagrant RAM

To adjust the RAM allocated to the VM, find and modify the following block:

config.vm.provider "virtualbox" do |vb|
  vb.memory = "2048"  # Customize the amount of memory on the VM
end

After making changes, apply them using:

vagrant reload

This command restarts the Vagrant-managed virtual machine, applying the changes made to the Vagrantfile.

Configuring Vagrant Network

To connect the VM to a public network, allowing it to be accessible from other devices:

config.vm.network "public_network"

For assigning a static IP:

config.vm.network "private_network", ip: "192.168.56.10"

Most Wi-Fi routers assign the IP address 192.168.0.1, so it is recommended not to use 0.1 in the last two octets to avoid collisions. The 56.x range is commonly used in VirtualBox, and some computers only allow 56.x, which is why we can assign IPs like 192.168.56.14. Values between 1 and 254 (inclusive) are valid for the fourth octet.

Configuring Vagrant CPU

To configure the CPU for the VM:

config.vm.provider "virtualbox" do |vb|
  vb.memory = "2048"
  vb.cpus = "2"  # Customize the number of CPUs
end

Checking VM Configuration

To check the network, RAM, and CPU configuration from within the Vagrant environment:

vagrant ssh
free -m
cat /proc/cpuinfo

Output for CPU cores:

cpu cores       : 2

Common Issues and Troubleshooting

  1. VirtualBox Kernel Driver Not Installed: Ensure that the VirtualBox kernel driver is installed and loaded.
   sudo /sbin/rcvboxdrv setup
  1. Network Configuration Conflicts: Ensure that the chosen IP range does not conflict with existing networks on your host machine.
  2. Insufficient Resources: Ensure that your host machine has enough resources (CPU, RAM) to allocate to the VM.

Powering Off the VM

To power off the VM:

exit
vagrant halt

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *