At this point you should have vagrant and virtual box installed on your operating system.
The next steps that we're going to do include adding a box a vagrant setting up a vagrant project and
bringing up those boxes logging into them and so on.
So the first thing we need to do is start a command line session.
Now if you're a Windows user what you need to do is start the command prompt.
Click on the Start button and in the search box type the words command prompt and then in a list of
results just double click on the command prompt and that will start your command line session for you.
For you Mac users start the Terminal application which is located in the slash applications slash Utilities
folder for Linux users go ahead and start your favorite terminal emulator probably using the menu system
in your distribution.
Examples include gnome terminal console and xterm as you can see here.
I'm working on a Mac but the commands we're going to run work on Windows Linux or Mac so no matter what
operating system you're on you can follow these exact commands.
So now that we have a command line session open we're ready to add a box to vagrant.
And as you probably remember from a previous lesson a box and vagrant speak is an operating system image.
And the vagrant box add command will download and store that box on your local system.
You only need to download a box once as this image will be cloned when you create a new virtual machine
with vagrant using that box's name.
I created a box specifically for this class and uploaded it to the public vagrant box catalogue.
Run the following command on your local machine to download it.
That command is vagrant box at Jason C. forward slashes Santos 7.
By the way I always follow the instructions in the project or exercise documentation because sometimes
that these links change but I'll try to stay as close to that as possible.
But again if you see something slightly different on your screen just know that the document that you're
following is the source of truth.
So I hit enter and the box will start to download.
OK the box has successfully been added to our local system.
So our next step is to create a folder or directory to keep all of our course work in.
And I just want to point out really quickly that when you open the command prompt on Windows or the
terminal on Mac or Linux typically you're placed into your home directory.
For example if I'm logged into a Windows system as Jason my home directory could be something like C
colon backslash users backslash Jason.
Now this might vary depending on what version of Windows you're using but it's going to be something
very similar.
I'm logged into a Mac system as I am here.
My home directory will be forward slash users forward slash.
Jason if I'm logged into a Linux system as Jason my home directory is going to be for Slash home Ford
slash Jason.
So this just gives you an idea of where you're at on the system so you know where to find the files
that you're about to create and work with during this course.
So again like I said we're going to create a folder or a directory to keep our coursework in we're going
to name that folder show class and what we're going to do now is type make der Shao class that command
works on Windows Mac and Linux so we're good here.
So next we want to move into that folder we just created a well run CD which stands for change directory
show class vagrant has this concept of projects and a vagrant project must consist of a folder and a
vagrant configuration file called a vagrant file that's placed in that folder.
So let's start out by creating a test box oh one folder.
Next we'll move into that folder with CD and now to create the vagrant configuration file you'll want
to run the vagrant init command followed by the box name so run vagrant and that Jason see forge slash
Cintas 7 and press enter.
The first time you run the vagrant up command vagrant will import or clone the vagrant box into virtual
box and then start it.
If vagrant detects that the virtual machine already exists in virtual box it will just go ahead and
simply start it by default.
When the virtual machine is started it is started in headless mode meaning that there is no user interface
for the machine visible on your local host machine.
It will be running in the background but you won't necessarily see anything change.
Let's bring up your first Linux virtual machine with vagrant by running vagrant up pressing enter.
I want to point out a couple of common issues that some people run into when they run the vagrant up
command.
Sometimes I've seen some issues where someone has accidentally mis type the box name when they did the
vagrant init.
That can lead to an error that states that the box couldn't be found and so vagrant will not be able
to clone that box and so on.
So that's a common issue.
So make sure with your vagrant a command you spelled everything correctly and I'm going to show you
in a second how to fix that.
If you didn't.
Another issue that you may run into when running vagrant up is that you'll get an error reporting something
about timed out while waiting for the machine to boot.
It could be a couple of different reasons for this.
The most likely reason is that the virtual machine started without its network interface enabled.
I've mainly seen this on older Windows systems I'm not sure what it is about the host operating system
that sometimes doesn't enable the virtual network interface to be enabled on start but you can quickly
fix this by editing the vagrant file and adding a block of text that I'll provide in the exercise notes.
And with that block a configuration does is it just forces the cable to be plugged in and that virtual
network device to be turned on.
A third possibility is that VTX or the virtualization settings for your C.P you are not enabled on the
host computer.
And then what you need to do in that case is to go into the BIOS and enable those through your computer
restart and then try again.
By the way there was a trouble shooting video specifically on these issues that goes into more detail.
So if you're still having problems go ahead and skip forward to the trouble shooting video.
Watch that.
Fix your problem and then come back to this exercise so quickly how we're going to fix these issues
or how we're actually going to provide further configuration is to edit the vagrant file to do that
you want to open it with your favorite text editor and I'm saying text editor for a reason do not use
a word processor do not use Microsoft Word Libris Office Writer do not use Apple pages.
Use a text editor.
One text editor that I recommend a lot is Adam spelled 80 O M and it's available at Adam dot I O O M
IO and it works on windows of Mac and Linux so I'm going to go ahead and open up my Adam text editor
and edit the vagrant file here.
Now you can open up the vagrant file with your text editor
to go to my home directory then find shelf class and then test the box one.
And finally open the vagrant file.
So this is where it becomes important to know where your home directory is where you created your shell
class folder and where what project you're working on and obviously where the vagrant file list.
So once you've located the file you can open it with your favorite text editor.
So if you did run vagrant a net with the incorrect box name you can come down to the config that the
box line and make your changes needed.
For example I've seen some people do this Jason Ford slash sento seven or some other variation like
that and then you can fix it here.
Save your vagrant file and then run vagrant up again and you should be good to go.
Let's confirm that our first virtual machine is running by starting the virtual box application.
On Windows you can double click on the Oracle VM virtual box icon on your desktop for Mac users you
can start the applications virtual box application and Linux users will just need to find the application
in their menu system.
Now you can confirm that you see a virtual machine running and it will start with the name of your vagrant
project folder.
So you here you can see that it starts with test to box 0 1.
Let's get back to the terminal real quick.
You can also use the vagrant status command to check the status of the virtual machine and you can confirm
that it shows that the virtual machine is in a running state by running vagrant status.
The vagrant status command confirmed that the machine is running just like we saw it running inside
the virtual box application as well.
Now that we have a box up let's connect to it with S-sh as it's aitches called secure shell and it's
the network protocol used to connect to Linux systems.
Vigor provides a nice shortcut to S-sh into the virtual machine and that shortcut is vagrant S-sh.
Now you are connected to the Linux virtual machine as the vagrant user.
This default vagrant account is used to connect to the Linux system for your convenience the vagrant
application takes care of the details that allow you to connect to the box over S-sh without a password.
For reference the password for the vagrant account is vagrant.
The password for the root account is also vagrant.
That's a V A G R A N T the vagrant user has full sudo or administrative privileges and then allows you
to further configure the system if you want to.
Of course we'll be dealing more with accounts and privileges ensue later in this course.
Now let's exit the virtual machine by typing the exit command and now we're log out of the Linux virtual
machine and back to our host operating system.
The vagrant Hok command shuts down the virtual machine when you run this command you will not lose any
work you performed inside the virtual machine the virtual machine will still exist in virtual box it
will just simply be stopped.
So lets do that now.
OK the machines should be halted.
Let's look at it in the virtual box application.
You can see that the virtual machine still exists in virtual box but it's just simply powered off.
You might have noticed only logged into the Linux system.
We had a prompt that read vagrant app local host local host is the name of the virtual machine inside
the virtual machine itself.
It's what Linux thinks its name is.
We can have vagrant configured that name for us and to do that we need to put that configuration in
the vagrant file.
So open up your favorite text editor navigate to the location of the vagrant file and then load that
file up and we're going to put a line of configuration in there.
So the configuration in the vagrant file really starts at this vagrant configure line and it goes all
the way down to the bottom of the file here at the end keyword.
What we're going to do is start placing our configuration right after this config that VM box line to
set the hostname we can type config VM dot hostname equals and then specify the name.
So we're going to call this box test box.
So we want it now and we need to do is save it to go to file and save.
And let's return to the command prompt.
At this point you could run vagrant halt followed by vagrant up to activate this change however vagrant
provides a shortcut vagrant reload which restarts the virtual machine load the new vigorish configuration
and starts the virtual machine again.
So let's give that a go here
OK.
Let's connect to the virtual machine and see if the hostname is changed or run vagrant.
S-sh.
Now you can see the prompt reads vagrant at test the box so once our configuration worked.
Let's go ahead and exit out of this virtual machine with exit that will log out and places back on our
host system.
Let's move back over to the vagrant file.
During this course you're going to create virtual machines that will be able to communicate with each
other as well as your local desktop or laptop.
Let's give this virtual machine the IP address of ten nine dot 8.7 and to do that what we need to do
is insert some configuration into the vagrant file.
So continuing here I'll just started on the next line will do config.
VM network space a quotation mark private underscore network.
And then after the closing quotation mark we need a comma space I.P a colon space Leweni quotation marks
ten nine eight seven and then we'll go ahead and save our file a common issue I see people have here
is that they simply do something like leave out this comma.
So again make sure that you're following the syntax and what's in the project instructions.
Now let's move back to our command line section and make vagrant reload that configuration so we'll
do vagrant reload.
Since the virtual machine was running it will go ahead and stop it and then restart it with the new
vagrant configuration
OK our machine is reloaded.
Let's test the network connectivity with the ping command.
If you're on Windows you can just typing in the IP address.
If you a Mac or Linux you're going to specify a count with that.
So we'll just do that.
See three dozen three packets and we'll do 10 9 8 7 which is the IP address we assign the virtual machine
let's hit enter and see what happens.
We were able to successfully ping the virtual machine as we had no packet loss.
Now the ping command is just one simple way to test network connectivity.
So if you see replies and you can just safely assume the IP addresses reachable and that the host is
up if you see timeout messages then the system is not answering your ping requests.
In the real world this doesn't necessarily mean the system is down.
It just means it's not answering your ping requests which could be for a variety of reasons.
However for our purposes here if you get a timeout message then you can safely assume that this system
is down or there's something wrong with the configuration.
The first thing to try is just simply reboot the VM by running vagrant reload if the ping command fails
again.
Double check the contents of the vagrant file and pay very special close attention to the config that
the network line.
If you've made a typing mistake no big deal just fix your changes save the vagrant file restart the
virtual machine and try again.
The final step is to reboot the host operating system or your physical computer come back and try it
again.
If you're done with the virtual machine or you want to start over with a fresh copy of the virtual machine
run vagrant destroy.
Well do that now.
It'll prompt you to confirm that you actually want to delete the machine and will just say why for yes
and hit enter.
Now if we look back in the virtual box application that machine is gone it's been destroyed it's been
removed.
It's like it never existed.
So if we had any work that was saved inside to the local disk that work is gone because we totally destroyed
that virtual machine.
So when you run into story be sure that you're done with the contents inside of that virtual machine.
Let's create another vagrant project but this time let's define multiple virtual machines in that one
project.
First let's return to our shell class directory and the easiest way to do that is CD space.
Period.
And press enter.
That command changes to the parent directory which is represented by dot dot or period period.
By the way dot represents this directory dot dot represents the parent directory or the directory above
your current location in the file system.
Now let's go ahead and create a vagrant project folder and change into that folder.
Will do that with make sure multi test and will change into that directory.
CD multi test and the next step is to initialize the vagrant project which creates our vagrant file
vagrant and knit Jason.
Jason see Ford slash Sentosa 7 and press enter.
Now open up the vagrant file with your favorite text editor.
Now what we want to do is to define two virtual machines.
Let's give the first one the name of test one and give it an IP address of 10 that nind at a dot five.
And so we're going to do is leave any of the common configuration amongst all the machines that we're
creating at the top of the file.
So the box we're going to use for each of our virtual machines is going to be this box we just leave
that there and it will apply to all the virtual machines that we create.
So how we create a virtual machine instance here is config dot VM define.
And then what we're going to do is place the virtual machine name in quotation marks.
So we'll call this test one next.
We need the word do fall by the pipe.
And then again the name of the virtual machine.
Test one followed by closing pite.
Now we use the name of that machine to perform our configuration.
So Will do test one that VM hostname and we'll give it a hostname of test 1.
We also want to configure the network on this one so we'll do test one dot VM dot net work
private network.
We're going to give it an IP address of 10 9 8 and 5.
Now we can in this block of code with end and that is our first virtual machine definition.
Let's go ahead and configure the second virtual machine.
Very similar process.
To test to
the host name to test to
will give us an IP address of 10 that Nonda 8.6 and we'll end with it and key word for that block of
code which configures the test to virtual machine.
Now we just save our changes know what started the virtual machines up by going back to our command
line section and running vagrant up by the way if you only wanted to bring one of the systems up then
you would specify it after having grown up.
For example if you only wanted to bring up the test two system you would run vagrant tests too.
But if you don't specify a system or virtual machine vagrant up will bring up every machine that is
defined in the vagrant file.
Ok big run has brought up both of the virtual machines that we defined.
Now let's check the status with vagrant status OK.
It says both of them are running tests one is running in Virtual Box test 2 is also running in virtual
box.
Let's go ahead and connect to the test to one virtual machine to confirm that it's working correctly
and then we'll just quickly log out of it.
We'll use vagrant S-sh and this time we have to specify a name of a virtual machine because we've defined
more than one virtual machines in our configuration.
So vagrant won't be able to tell which box we're trying to connect to unless we give it a name that
will give a name of the VM here which is Test 1 and hit enter.
If you look at the prompters vagrant at Test 1 so that's the correct TOSed we're on that looks OK we
can get in it.
Now let's log out of the system with exit.
Let's do the same thing with the test 2 system so we'll do vagrant S-sh test 2.
Again notice the prompt it reads vagrant at Test 2 so that's another confirmation that we're connected
to the test to VM.
Now while you're logged into the test to VM Let's see if we can ping the test one VM to prove that the
two virtual machines can communicate with each other over the network.
So we can do this with ping to see three ten nine eight dot five which is the IP address of the test
one system and hit enter OK.
No packet loss we can connect to the test one system over the network.
So that looks good.
While were connected to this VM I want to show you one more thing you might have noticed some messages
about mounting shared folders when the vagrant up command was executed.
Well you can access the files in the vagrant project directory that resides on your local machine inside
the virtual machine.
The vagrant project directory is mounted or shared via the forward slash vagrant directory the only
fallen or local directory is the vagrant file and you can look at that file from within the VM.
So let's run the following commands inside the test to VM will run LS for flash a vagrant and you can
see that there is a vagrant file there.
And let's look at the contents of that file with a cat command.
Or do cat vagrant vagrant file and press enter.
OK so now you can see how you can access files that reside on your local machine while you're logged
into the virtual machine.
This is also a convenient way to get files inside the virtual machine without having to do SEPs and
those types of things.
OK we're done for today so let's go ahead and exit out of this VM.
By the way in the upcoming project she'll be working a lot more with vagrant virtual machines and IP
addresses and so on.
So feel free to explore the Linux system if you'd like or keep playing around with virtual box when
you're ready to stop or take a break.
Halt the virtual machine or virtual machines that you're working with and then remember that you can
always pick up where you left off.
As long as you don't destroy the virtual machine.
Since I'm done I'm just going to halt these virtual machines with a vagrant halt command.
Again remember if you only want to operate on one of the virtual machines specify that virtual machine
name at the end of the vagrant command.