There could be some changes to your virtual machine and you can find out that it no longer works.
Thankfully, since we have a base box configuration file and provisioning files which are all stored
separately, we can instruct Vagrant to destroy our virtual machine and then create it again using the
configurations to set it up.
This is done via the destroy sub command and then the up sub command to start it again.
So first let's destroy the environment we already have and then let's start it again.
I'll type vagrant, destroy, wait some time, and now I'll type vagrant up.
Of course, if we have data, our vagrant file provisioning manifests or application code that can also
break things.
So it's important to use a version control system to properly manage our project's code and configuration
so we can undo our changes there too.
Now, if you want to connect to the virtual machine over SSH, you can run the vagrant SSH command.
Vagrant will then connect to the VM over ssh.
Of course you can also connect to the VM directly if you are connecting from linux box.
You have a built in ssh client there.
And if you if you are connecting from windows machine, you won't have a built in a client.
So you have to download the client such as party.
You can download it from the official side party dot org or alternatively you can use sig client segment
terminal also available for Windows Now without any form of integration between the host machine and
the guest, we would have a virtual server running on top of our own operating system, which is not
particularly useful.
We need our own machine to be capable of integrating tightly with the guest as virtual machine.
Although the VM is running on our own machine, it acts and behaves like a completely different machine.
Sometimes this is what we want.
Other times it isn't.
We may want to integrate the two more tightly.
One option is port forwarding, where we can tune a port from a virtual machine to a port on the host
machine.
If, for example, we have a web server running on our own machine, we obviously don't want to map
the web server port from Vagrant onto the same port.
Otherwise, there would be a conflict.
Instead, we can map it to another port.
If we map the web server port on the VM to port 88, for example on the host, then visiting http local
host colon 880 on our own machine would show us the web service will run on the guest port.
Forwarding is done while lines in the vagrant file.
We simply provide the guest and host ports we wish to map by typing config.
Dot vmm dot network colon forwarded underscore port comma guest 80 comma host 88.
If we have other vagrant managed virtual machines on our computer which we wish to run simultaneously,
we can enable autocorrect on specific ports.
This way if a conflict is found, for example, two VMs trying to map the same port, one will try a
different port instead.
So we can do this with this option.
Autocorrect through ports below a certain range need elevated or root privileges on the host machine.
So you may be asked for your administrative password.
Another very useful form of integration is synced folders or synchronized folders.
Synced folders allow us to share a folder between the host and the guest.
By default, Vagrant shares the folder containing the Vagrant project as slash vagrant on the VM.
We can use the following in our vagrant file to sync more folders if we wish.
We can do this with a command config dot VM select folder and we have to specify the folders the path
to the folders.
So the first parameter is the path to the folder on our machine.
The second being the amount point on the beam.
If we use a relative pass on our machine, it would be relative to the project folder.
The network file system can give us better performance with synched folders than the default settings.
This won't work on Windows hosts and on Linux.
It will require root privileges, but we can enable the NFS on a percent folder basis by adding the
following to the preceding line.
We should add NFS True.
Now some words about networking.
By default, our vagrant virtual machines are only accessible from the machines we run.
Vagrant on.
If we map ports on our host, then we can share services running on the VM with our colleagues within
our network.
If we want to allow our colleagues to access our vagrant managed VMs directly, we can attach the VM
to our internal network, and VirtualBox will breach the network between our machine and the VM and
the internal network between our machine and the rest of machines in our home or office.
We can perform this with the following command config, dot VM, dot network, and then we have to specify
that it is private network and its IP should be.
For example.
1922.168.
1.100 or any IP unit right now.
And of course one of the key concepts within vagrant is provisioning.
This involves tuning a basic virtual machine with a base operating system into a server that is ready
to run for your project.
Meeting your requirements.
There are key provision and options with Vagrant, SSH, Ansible, puppet or chef, Ansible or chef
or third party tools which vagrant supports out of the box and provide specific languages for configure
and servers in an way that can be used for different operating systems.
A stage provisioning involves running a series of commands on the virtual machine over SSH when the
VM is first setup.
There are two ways we can use such provisioning.
We can either directly run a command from our vagrant file with the following line config, dot vm,
dot provision, shell and inline command followed by the command itself.
Alternatively, we can tell Vagrant to run a particular shell script.
The location of the script specify it is relative to our project root that is slash vagrant.
For example, we could type config dot vm provisioned shell followed by path parameter and the shell
script itself.
For example provisioned dot ssh in double quotes.
This shell script could contain all the commands we need to convert a base box which contains very little
to a box which supports our project and application, perhaps install web and database servers.
So let's wrap up.
We created projects with Vagrant pooled vagrant boxes to use and we also looked at the commands needed
to manage these boxes and to manage the vagrant virtual machines.
We looked at how we can configure our vagrant environment with networking synced folders and forwarded
ports, and finally we looked at how to provision software on our VM with SSH commands.