Vagrant is a tool for building and managing virtual machine environments. As we are an operations engineer, Vagrant gives you a disposable environment and consistent workflow for developing and testing infrastructure management scripts.
Almost all interaction with Vagrant is done through the command-line interface.
Vagrant Features:
- Connect to a VM: Vagrant includes a built-in SSH system to authenticate and connect.
- Folder Synchronization: Files on the host can be synchronized with files in vagrant VM and vice versa.
- Networking: Vagrant supports multiple networking configuration options for VMs.
- Providers: Providers are plugins to provide support to a particular hypervisor.
- Provisioners: Provisioners run the first time when Vagrant VM started to install software and setting configurations.
- Multi-machine Vagrantfiles: A single Vagrant file can define more than one VM.
Vagrant Component:
- The CLI: used to start and stop Vagrant VMs, initialize a new vagrant environment and manage running VMs.
- Vagrantfile: Vagrantfiles are small programs in the Ruby language. They are executed by the vagrant CLI to define and run a Vagrant VM. If you don't know Ruby, no need to worry, these vagrant files are just statements, not complex coding.
- Vagrant Cloud: is an online Marketplace for public virtual machines. We can easily publish and share our VM on Vagrant Cloud and can make them available publically OR we can use a private account to restrict the access.
When we provision VM we have to do a lot of things manually; like:
- Set CPU and Memory for OS
- Install OS
- Set a hostname
- Assign IP to the box
- Setup Port Forwarding
- Copy some files from base to VM
- Install VM Tools or
- We may need to install the required software
Vagrant is a command line utility for deploying operating system environments in VMs. In Vagrant, we create a Vagrantfile that contains the configuration and using that Vagrantfile we can create one or multiple VMs. This exercise not only saves time but also we can share this file with anyone else to recreate the exact same environment. Vagrant is platform independent. So, we can use Vagrant on Windows, Linux, and even Mac. Vagrant also supports lots of virtualization technologies like VMWare, VirtualBox, Hyper-V, Docker and even parallel on MAC.
BETTER IF WE HAVE SOME BASIC KNOWLEDGE ABOUT:
- Virtual Box
- TCP/IP and DNS Networking
- Linux Commands
Vagrant Installation:
- Go to the link https://www.virtualbox.org/ and Click on Download VirtualBox 6.0.
- Scroll down to VirtualBox older builds then VirtualBox older builds.
- Click on VirtualBox 6.0
- Then download VirtualBox-6.0.2.
- Once the installer is downloaded fins it on the system and run the installation process with default parameters.
2. Now install Vagrant:
Windows:
Go to URL: https://www.vagrantup.com/downloads.html ➔ Select OS name and architecture ➔ Download and Install
Linux:
Go to URL: https://www.vagrantup.com/downloads.html ➔ Right click on selected OS architecture ➔ Copy link address > Follow commands:
$ sudo rpm -ivh <paste copied path>
$ vagrant -v //to verify the version installed on the system
Once the installer is complete we can use Power Shell or Command Prompt in Windows or Terminal in Linux or MAC and use command vagrant -v; if this shows the version then vagrant is installed successfully.
Once the installer is complete we can use Power Shell or Command Prompt in Windows or Terminal in Linux or MAC and use command vagrant -v; if this shows the version then vagrant is installed successfully.
Vagrant Common Commands:
➔ box:
Vagrant boxes are base images to quickly clone a virtual machine. Vagrant box command is used to manage boxes.
Syntax: $ vagrant box <subcommand>
Subcommands: add, list, outdated, prune, remove, repackage, update
➔ cloud:
Vagrant cloud command manages everything related to Vagrant Cloud.
Syntax: $ vagrant cloud <subcommand>
Subcommands: auth, box, provider, publich, search version
➔ connect:
Vagrant connect command connects the share commands by enabling access to shared environments.
Syntax: $ vagrant connect <name>
Options: (--disable-static-ip), (--static-ip <IP>) and (--ssh)
➔ destroy:
Vagrant destroy command stops and deletes all resources that were created during the machine creation process.
Syntax: $ vagrant destroy [name|id]
Options: (-f OR --force) and (--[no-]parallel)
The destroy command does not remove a box that may have been installed on your computer during vagrant up. TO delete that box we need to use "vagrant box remove" command.
➔ global-status:
Vagrant global-status command outputs the state of all active Vagrant environments on the system for the currently logged in user, based on a cache memory.
Syntax: $ vagrant global-status
Options: (--prune)
➔ halt:
Vagrant halt command shuts down the running vagrant machine.
Syntax: $ vagrant halt [name|id]
Options: (-f) OR (--force)
➔ help:
Vagrant help command shows the help for a subcommand.
Syntax: $ vagrant <subcommand> --help
Options: (-h) OR (--help)
➔ init:
This initializes the current directory to be a Vagrant environment by creating an initial Vagrantfile if one does not already exist.
Syntax: $ vagrant init [name [url]]
Options: (--box-version), (--force), (--minimal), (--output FILE) and (--template FILE)
➔ login:
Vagrant login command packages a running vagrant environment into a box. Logging in is not a requirement to use Vagrant.
Syntax: $ vagrant login
Options: (--check), (--logout) and (--token)
➔ package:
Vagrant package command packages a currently running VirtualBox or Hyper-V environment into a re-usable box.
Syntax: vagrant package [name|id]
Options: (--base NAME), (--output NAME), (--include x,y,z) and (--vagrantfile FILE)
➔ plugin:
Vagrant plugin command manages plugins.
Syntax: vagrant plugin <subcommand>
Subcommands: expunge, install, license, list, repair, uninstall, update.
Options: (--force), (--global-only), (--local), (--local-only) and (--reinstall).
Syntax: $ vagrant port [name|id]
Options: (--guest PORT) and (--machine-readable)
➔ powershell:
Vagrant powershell command opens a PowerShell prompt on the host into a running Vagrant guest machine.
Syntax: $ vagrant powershell
Options: (-c COMMAND) or (--command COMMAND)
➔ provision:
Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. Vagrant provision command runs any configured provisioners against the running Vagrant managed machine.
Syntax: $ vagrant provision [vm-name]
Options: (--provision-with x,y,z)
➔ push:
Vagrant push command deploys code in this environment to a configured destination.
➔ rdp:
This will start an RDP client for a remote desktop session with the guest.
Syntax: $ vagrant rdp
➔ reload:
This command is usually required for changes made in the Vagrantfile to take effect. After making any modifications to the Vagrantfile, a reload should be called.
Syntax: vagrant reload [name|id]
Options: (--provision) and (--provision-with x,y,z)
➔ resume:
Vagrant resume command resumes a Vagrant managed machine that was previously suspended.
Syntax: $ vagrant resume [name|id]
Options: (--provision) and (--provision-with x,y,z)
➔ share:
Vagrant share command initializes a Vagrant Share session. It allows us to share our Vagrant environment with anyone in the world.
Syntax: $ vagrant share
Options: (--disable-http), (--http PORT), (--https PORT), (--ssh). (--ssh-no-password) and (--ssh-once)
Vagrant Share is a Vagrant plugin that must be installed, which is not included with Vagrant system packages.
Command: $ vagrant plugin install vagrant-share
➔ snapshot:
Vagrant snapshot command is used to manage snapshots with the guest machine. Snapshots record a point-in-time state of a guest machine. You can then quickly restore to this environment
Subcommands: push, pop, save, restore, list and delete
➔ ssh:
This will SSH into a running Vagrant machine and connects to machine via SSH.
Syntax: $ vagrant ssh [name|id] [-- extra_ssh_args]
Options: (-c COMMAND OR --command COMMAND) and (-p OR --plain)
➔ ssh-config:
Vagrant ssh-config command outputs OpenSSH valid configuration to connect to the machine.
Syntax: $ vagrant ssh-config [name|id]
Options: (--host NAME)
➔ status:
Vagrant status command outputs status of the vagrant machine.
Syntax: $ vagrant status [name|id]
➔ suspend:
Vagrant suspend command suspends the guest machine, rather than fully shutting it down or destroying it.
Syntax: $ vagrant suspend [name|id]
➔ up:
Vagrant up command creates and configures guest machines according to the Vagrantfile.
Options: (name), (id), (--[no-]destroy-on-error), (--[no-]install-provider), (--[no-]parallel), (--provider x), (--[no-]provision) and (--provision-with x,y,z)
➔ upload:
Vagrant upload command uploads to machine via communicator.
➔ validate:
Vagrant validate command validates the Vagrantfile.
Syntax: $ vagrant validate
Options: (--ignore-provider)
➔ version:
Vagrant version command prints current and latest Vagrant version.
Syntax: ($ vagrant –v) OR ($ vagrant --version)
➔ winrm:
Vagrant winrm command executes commands on a machine via WinRM.
➔ winrm-config:
Vagrant winrm-config command outputs WinRM configuration to connect to the machine.
➔ docker-exec:
Syntax: $ vagrant docker-exec app -- rake db:migrate
For more details:
https://www.vagrantup.com/docs/docker/commands.html
➔ docker-logs:
Vagrant docker-logs command is used to see the logs of a running container.
Syntax: $ vagrant docker-logs
For more details:
https://www.vagrantup.com/docs/docker/commands.html
➔ docker-run:
Syntax: $ vagrant docker-run app -- rake db:migrate
For more details:
https://www.vagrantup.com/docs/docker/commands.html
Vagrant boxes are base images to quickly clone a virtual machine. Vagrant box command is used to manage boxes.
Syntax: $ vagrant box <subcommand>
Subcommands: add, list, outdated, prune, remove, repackage, update
➔ cloud:
Vagrant cloud command manages everything related to Vagrant Cloud.
Syntax: $ vagrant cloud <subcommand>
Subcommands: auth, box, provider, publich, search version
➔ Command: $ vagrant cloud auth login
Options: (--check), (--logout) and (--token)
➔ Command: $ vagrant cloud auth logout
➔ Command: $vagrant cloud auth whomi
This command validates our Vagrant Cloud token and will print the user who it belongs to.➔ connect:
Vagrant connect command connects the share commands by enabling access to shared environments.
Syntax: $ vagrant connect <name>
Options: (--disable-static-ip), (--static-ip <IP>) and (--ssh)
➔ destroy:
Vagrant destroy command stops and deletes all resources that were created during the machine creation process.
Syntax: $ vagrant destroy [name|id]
Options: (-f OR --force) and (--[no-]parallel)
The destroy command does not remove a box that may have been installed on your computer during vagrant up. TO delete that box we need to use "vagrant box remove" command.
➔ global-status:
Vagrant global-status command outputs the state of all active Vagrant environments on the system for the currently logged in user, based on a cache memory.
Syntax: $ vagrant global-status
Options: (--prune)
➔ halt:
Vagrant halt command shuts down the running vagrant machine.
Syntax: $ vagrant halt [name|id]
Options: (-f) OR (--force)
➔ help:
Vagrant help command shows the help for a subcommand.
Syntax: $ vagrant <subcommand> --help
Options: (-h) OR (--help)
➔ init:
This initializes the current directory to be a Vagrant environment by creating an initial Vagrantfile if one does not already exist.
Syntax: $ vagrant init [name [url]]
Options: (--box-version), (--force), (--minimal), (--output FILE) and (--template FILE)
➔ login:
Vagrant login command packages a running vagrant environment into a box. Logging in is not a requirement to use Vagrant.
Syntax: $ vagrant login
Options: (--check), (--logout) and (--token)
➔ package:
Vagrant package command packages a currently running VirtualBox or Hyper-V environment into a re-usable box.
Syntax: vagrant package [name|id]
Options: (--base NAME), (--output NAME), (--include x,y,z) and (--vagrantfile FILE)
➔ plugin:
Vagrant plugin command manages plugins.
Syntax: vagrant plugin <subcommand>
Subcommands: expunge, install, license, list, repair, uninstall, update.
Options: (--force), (--global-only), (--local), (--local-only) and (--reinstall).
➔ port:
Vagrant port command displays the full list of guest ports mapped to the host machine portsSyntax: $ vagrant port [name|id]
Options: (--guest PORT) and (--machine-readable)
➔ powershell:
Vagrant powershell command opens a PowerShell prompt on the host into a running Vagrant guest machine.
Syntax: $ vagrant powershell
Options: (-c COMMAND) or (--command COMMAND)
➔ provision:
Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. Vagrant provision command runs any configured provisioners against the running Vagrant managed machine.
Syntax: $ vagrant provision [vm-name]
Options: (--provision-with x,y,z)
➔ push:
Vagrant push command deploys code in this environment to a configured destination.
➔ rdp:
This will start an RDP client for a remote desktop session with the guest.
Syntax: $ vagrant rdp
➔ reload:
This command is usually required for changes made in the Vagrantfile to take effect. After making any modifications to the Vagrantfile, a reload should be called.
Syntax: vagrant reload [name|id]
Options: (--provision) and (--provision-with x,y,z)
➔ resume:
Vagrant resume command resumes a Vagrant managed machine that was previously suspended.
Syntax: $ vagrant resume [name|id]
Options: (--provision) and (--provision-with x,y,z)
➔ share:
Vagrant share command initializes a Vagrant Share session. It allows us to share our Vagrant environment with anyone in the world.
Syntax: $ vagrant share
Options: (--disable-http), (--http PORT), (--https PORT), (--ssh). (--ssh-no-password) and (--ssh-once)
Vagrant Share is a Vagrant plugin that must be installed, which is not included with Vagrant system packages.
Command: $ vagrant plugin install vagrant-share
➔ snapshot:
Vagrant snapshot command is used to manage snapshots with the guest machine. Snapshots record a point-in-time state of a guest machine. You can then quickly restore to this environment
Subcommands: push, pop, save, restore, list and delete
➔ ssh:
This will SSH into a running Vagrant machine and connects to machine via SSH.
Syntax: $ vagrant ssh [name|id] [-- extra_ssh_args]
Options: (-c COMMAND OR --command COMMAND) and (-p OR --plain)
➔ ssh-config:
Vagrant ssh-config command outputs OpenSSH valid configuration to connect to the machine.
Syntax: $ vagrant ssh-config [name|id]
Options: (--host NAME)
➔ status:
Vagrant status command outputs status of the vagrant machine.
Syntax: $ vagrant status [name|id]
➔ suspend:
Vagrant suspend command suspends the guest machine, rather than fully shutting it down or destroying it.
Syntax: $ vagrant suspend [name|id]
➔ up:
Vagrant up command creates and configures guest machines according to the Vagrantfile.
Options: (name), (id), (--[no-]destroy-on-error), (--[no-]install-provider), (--[no-]parallel), (--provider x), (--[no-]provision) and (--provision-with x,y,z)
➔ upload:
Vagrant upload command uploads to machine via communicator.
➔ validate:
Vagrant validate command validates the Vagrantfile.
Syntax: $ vagrant validate
Options: (--ignore-provider)
➔ version:
Vagrant version command prints current and latest Vagrant version.
Syntax: ($ vagrant –v) OR ($ vagrant --version)
➔ winrm:
Vagrant winrm command executes commands on a machine via WinRM.
➔ winrm-config:
Vagrant winrm-config command outputs WinRM configuration to connect to the machine.
➔ docker-exec:
Syntax: $ vagrant docker-exec app -- rake db:migrate
For more details:
https://www.vagrantup.com/docs/docker/commands.html
➔ docker-logs:
Vagrant docker-logs command is used to see the logs of a running container.
Syntax: $ vagrant docker-logs
For more details:
https://www.vagrantup.com/docs/docker/commands.html
➔ docker-run:
Syntax: $ vagrant docker-run app -- rake db:migrate
For more details:
https://www.vagrantup.com/docs/docker/commands.html
Vagrant Box:
Vagrant Boxes are pre-installed virtual machines with some specific settings. There are two official vagrant providers, where we can find our virtual boxes:
SYNTAX: <username>/<box_name>
We can use terminal to execute these commands. Like I have Windows as my Operating System. So, I will use Windows PowerShell or Command Prompt to execute Vagrant Commands.
Vagrant General Used Commands:
List all sub-commands of Vagrant Box:
C:\> Vagrant box -h1. THE ADD COMMAND:
Here, the first command is 'add' command. Vagrant Add command is used to download a Vagrant Box. For e.g. we can download a tiny amount of Vagrant Box named ‘tiny-code-micro’. It’s hardly 12 MB in size, provided by olbat user.
- Go to https://app.vagrantup.com
- In search box search 'tiny-core-micro'
- Here we can see the full name is olbat/tiny-core-micro
- Use command:C:\> vagrant box add olbat/tiny-core-micro
At the first time, this takes time to download Vagrant Box but once if we download this, we can reuse it multiple times.
We can also download these Vagrant boxes from http://www.vagrantbox.es/ (here default provider is VirtualBox but if we want to use it for a different provider like VMWare, then we may use--provider=vmware) for this we need to copy URL and use that URL in command. For e.g. I am using RHEL7.3 (Guest Additions 5.1.18) to download. Just click on ‘copy’ button; then got to the terminal and use the command:
SYNTAX:
C:\> vagrant box add <box_name> <URL>
EXAMPLE:
We can see both of these downloaded boxes (olbat/tiny-core-micro from official website direct and samdoran/rhel7 by URL) by using the command vagrant box list.
2. THE LIST COMMAND:
This command lists all of the boxes available in our machine.
C:\> vagrant box list
3. THE OUTDATED COMMAND:
We can use this command to check all the boxes are outdated. When we use this command this will go to the website and check if the version of this box has changed. Here we will use --global flag to check all the boxes that we have on our machine. Without the --global flag it will just check whichever box is present in the vagrant file which we will come to in the next few minutes.
Here, we can see that ‘centos/7’ is outdated. Currently, we are using 1811.01 version whereas the latest version is 1901.01.
4. THE UPDATE COMMAND:
‘vagrant box update’ will only update the box which is available in our current Vagrantfile. But by using --box flag we can update any box. So here we will update centos/7 box:
C:\> vagrant box update --box centos/7
It will download the entire box.
5. THE PRUNE COMMAND:
We use Prune command to remove the older version of the box. Once we update any box there are two versions of the same box are available on our machine. If we don’t want an older version we can just prune it. If we provide -n flag then this command just do a dry run and it will not delete the older version of boxes.
So, here we can see that as we run this command, it removes centos/7 version 1811.01. Since we have the newer version with us. But if we run only vagrant box prune command, this will delete that older version.
6. THE REMOVE COMMAND:
We use this command to remove any version of the box. If we want completely remove a Vagrant box, then we use this command. We should be very careful before removing a box. Its better we list all the boxes then select appropriate box we want to remove.
This will completely remove centos/7 box from our machine.
We use repackage command to create a .box file which we can share with others. As I previously mentioned that boxes downloaded from URL have ‘.box’ extension. We can see the extracted version of that .box package at location:
~\.vagrant.d\boxes\olbat-VAGRANTSLASH-tiny-core-micro\0.1.0\virtualbox
Here, we get ‘.ovf’ file, which is the default extension of VirtualBox exported machine and a 'vmdk' hard disk of that VirtualBox.
First, go to the appropriate location where we want to repackage any box:
If we want then we can rename this .box file:
Now, we can share this olbat.box with anyone else. We can upload this on GitHub or Docker Hub etc. also. Then, that person needs to use below-mentioned command to get this box and to create a VirtualBox.
C:\> vagrant box add olbat <URL where we provided this box>
This will download olbat box in that user machine and we can see this box by listing in (vagrant list all) that machine.
8. THE UPDATE COMMAND:
By using this command we can update our boxes from older version to newer version.
The first line in this file tells the Vagrant environment to use version 2 or the config object. As we just used vagrant init command, it didn’t populate the config.vm.box option. So, we need to fill this manually and for an e.g., we are using here bento box "bento/centos-7.2", which we can list from https://app.vagrantup.com link.
We need to provide URL for this box using config.vm.box_url option. If we are using box downloaded in our local machine then we will use file:///<path_of_box> otherwise we will provide exact web URL.
We can provide hostname by using config.vm.hostname option.
When we spawn heavy boxes we face issues like there is some boot time out and vagrant will stop using commands if we feel that the boxes are not booting up. So, the box is heavy we can increase this boot time out to a bigger value using option config.vm.boot_timeout. The default value for this option is 300 seconds, we can increase it into a lower or higher value.
We can also control the graceful behavior of shutting down a box using option config.vm.graceful_halt_timeout. The default value for this option is 60 seconds, but we can specify it any other value also. If our box takes a long time to shut down. Once this time will pass Vagrant will forcefully shut down.
We use this command to remove any version of the box. If we want completely remove a Vagrant box, then we use this command. We should be very careful before removing a box. Its better we list all the boxes then select appropriate box we want to remove.
SYNTAX:
C:\> vagrant box remove <box_name>
EXAMPLE:
C:\> vagrant box remove centos/7
This will completely remove centos/7 box from our machine.
7. THE REPACKAGE COMMAND:
We use repackage command to create a .box file which we can share with others. As I previously mentioned that boxes downloaded from URL have ‘.box’ extension. We can see the extracted version of that .box package at location:
~\.vagrant.d\boxes\olbat-VAGRANTSLASH-tiny-core-micro\0.1.0\virtualbox
Here, we get ‘.ovf’ file, which is the default extension of VirtualBox exported machine and a 'vmdk' hard disk of that VirtualBox.
SYNTAX:
C:\> vagrant box repackage <box_name> <provider> <version>
COMMAND:
First, go to the appropriate location where we want to repackage any box:
C:\> cd ~\.projects\olbat
C:\> vagrant box repackage olbat/tiny-core-micro virtualbox 0.1.0
C:\> ls
Directory: C:\Users\Divakar Rastogi\.vagrant.d\projects\olbat
Mode LastWriteTime Length Name
-------- --------------------- --------- --------
-a----- 06-Mar-19 11:18 AM 11809191 package.box
-------- --------------------- --------- --------
-a----- 06-Mar-19 11:18 AM 11809191 package.box
If we want then we can rename this .box file:
C:\> mv package.box olbat.box
Now, we can share this olbat.box with anyone else. We can upload this on GitHub or Docker Hub etc. also. Then, that person needs to use below-mentioned command to get this box and to create a VirtualBox.
C:\> vagrant box add olbat <URL where we provided this box>
EXAMPLE:
C:\> vagrant box add olbat https://github.com/divakarrastogi/olbat/blob/master/olbat.box
This will download olbat box in that user machine and we can see this box by listing in (vagrant list all) that machine.
8. THE UPDATE COMMAND:
By using this command we can update our boxes from older version to newer version.
è Additionally I want to share that if we cancel the vagrant box add command in between it is downloading the box it will create some temporary files. So, we can clean by using '-c' or '--clean' flag to clear temporary downloaded files and '-f' or '--force' flag to override an existing box if it exists.
The Vagrant File:
We can use vagrant init command to generate a Vagrant file. As we run this command this says that 'Vagrantfile` has been placed in this directory, which is our current directory. We can list here to get our Vagrantfile. This Vagrant file helps us to customize the internal and external aspects of our Vagrant machine. This is a template file and most of the sections are well explained. We can provide each and everything in this Vagrantfile. We can use any text editor to make a change in this file:
C:\> vagrant init bento/centos-7.2
C:\> notepad.exe .\Vagrantfile
Here, the first part of the name bento is the organization name and second part is the distribution and version. Once the download process is complete we can view the contents of the box in the local cache of virtualbox. The path to the local cache is our 'home_directory.vagrant.d' > boxes > base box we just downloaded > version number > virtualbox; here we got box, metadata file vmdk virtual disk and the vagrant file. Vagrant will import the box into the environment we just created after the download is complete.
C:\> notepad.exe .\Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "base"
end
The first line in this file tells the Vagrant environment to use version 2 or the config object. As we just used vagrant init command, it didn’t populate the config.vm.box option. So, we need to fill this manually and for an e.g., we are using here bento box "bento/centos-7.2", which we can list from https://app.vagrantup.com link.
We need to provide URL for this box using config.vm.box_url option. If we are using box downloaded in our local machine then we will use file:///<path_of_box> otherwise we will provide exact web URL.
We can provide hostname by using config.vm.hostname option.
We can use config.vm.box_check_update option and mention it as 'true' to check updates. Every time when we spawn the box it will check to the website whether a newer version is available or not.
When we spawn heavy boxes we face issues like there is some boot time out and vagrant will stop using commands if we feel that the boxes are not booting up. So, the box is heavy we can increase this boot time out to a bigger value using option config.vm.boot_timeout. The default value for this option is 300 seconds, we can increase it into a lower or higher value.
We can also control the graceful behavior of shutting down a box using option config.vm.graceful_halt_timeout. The default value for this option is 60 seconds, but we can specify it any other value also. If our box takes a long time to shut down. Once this time will pass Vagrant will forcefully shut down.
Additionally, if we want to provide any message or information, we may use option config.vm.post_up_message. We can put up anything inside this message.
Now we need to run vagrant up command to bring up the box.
Now we need to run vagrant up command to bring up the box.
C:\> vagrant up
Here we can see that it has set the hostname and displayed the message which we specified in the file. we can verify hostname after going inside of the VM using command vagrant ssh:
$ hostname
Our box is running but VM doesn't have its own window. It's running in the background with no visible user interface. If we close the command window then also we can check the status of our virtual machine. In the power shell or in the command window run the command vagrant status in myfirstbox vagrant environment directory.
~\firstproject> vagrant status
Current machine states:
default running (virtualbox)
default running (virtualbox)
It shows that state for current VM is running. This works for a single vagrant VM but if we have multiple vagrant VMs then first we need to go back lower to one directory and then execute command vagrant global-status. This command can be executed from any directory.
C:\> cd ..
C:\> vagrant global-status
This vagrant global-status command can be run from any directory. It will list the status of all vagrant VMs on our machine. We can also execute commands on any one of the vagrant VM on our machine using its ID rather than having the change to its environment directory.
C:\> vagrant halt abefc0b
//here, abefc0b is the ID, returned to us form the status from the global status command that we ran previously.
//here, abefc0b is the ID, returned to us form the status from the global status command that we ran previously.
The halt command shuts down the vagrant virtual machine.
This command will delete the box completely from this environment but it will leave the Vagrantfile.
Connect to a box:
- By default, box does not include a graphical shell.
- Vagrant runs virtual machines in headless mode.
- A common method to connect remotely to the Linux operating system is SSH (Secure Socket Shell). It's a terminal interface that allows us to execute Linux commands in the remote command line shall environment.
- SSH uses cryptographic keys for authentication.
- Vagrant includes an SSH client that we can use to connect the boxes that support SSH.
Go to myfirstbox directory and run vagrant up
C:\> cd firstvagrantbox
C:\> vagrant up
Once the box is started then we can access the box. For this we need to use the command:
C:\> vagrant ssh
[vagrant@mycentos ~]$
[vagrant@mycentos ~]$
Now, we can see that it is now the CentOS box connected remotely via ssh. We can perform commands in this CentOS box. If we want to disconnect to this CentOS box, simply execute exit command. This will terminate the ssh connection and return us to the local windows.
Shut Down a Running Box:
If we want to shut down the running box, we need to run vagrant halt command. This command will shut down the guest operating system gracefully. A box that is being shut down can be restarted by using vagrant up command at any time.
However, let's suppose we want to start over an environment. We want to discard the changes made to the box and innstall the initial state of the box as it was at the first time. For this, we will use vagrant destroy command. But before this we need to log out from this VM:
C:\> Vagrant destroy
Getting started tutorials will walk you through creating your first development environment with Vagrant. This quick start guide will give you a brief ...Vagrant
ReplyDelete