1. Home
  2. Training Library
  3. Cloud Computing Fundamentals
  4. Courses
  5. Creating a Local Linux Shell Scripting Environment

Exercise 1 – Walkthrough – Part II – All Users

Start course
Overview
Difficulty
Beginner
Duration
53m
Students
213
Ratings
5/5
starstarstarstarstar
Description

In this course, you're going to learn how to set up a local Linux shell scripting environment, whether you're using Windows, MacOS, or Red Hat Enterprise Linux. You'll be guided through the installation processes of Vagrant and VirtualBox—two tools that we will make your life easier as we work our way through the Linux Shell Scripting learning path. We'll also carry out some troubleshooting of the most common issues, should you face them.

This course is part of the Linux Shell Scripting learning path. To follow along with this course, you can download all the necessary resources here.

Learning Objectives

  • Set up a local Linux environment using Vagrant and VirtualBox
  • Troubleshoot any issues/errors

Intended Audience

  • Anyone who wants to learn Linux shell scripting 
  • Linux system administrators, developers, or programmers

Prerequisites

To get the most out of this course, you should have a basic understanding of the Linux command line.

Transcript

At this point, you should have Vagrant and VirtualBox installed on your operating system. The next few steps that we're going to do include adding a box of 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 and your distribution. Examples include Nome, terminal, console, and external. 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 catalog. Run the following command on your local machine to download it. That command is Vagrant box add jasonc/centos7. By the way always follow the instructions in the project or exercise documentation because sometimes 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 Okay, 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 coursework 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 window system as Jason, my home directory could be something like C:\Users\jason. Now this might vary depending on what version of windows you're using but it's going to be something very similar. If I'm logged into a Mac system, as I am here, my home directory will be /Users/jason. If I'm logged into a Linux system as Jason, my home directory is going to be /home/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 shellclass and what we're going to do now is type, mkdir shellclass. 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 so we'll run cd, which stands for change directory shellclass. 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 to box or 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 we'll run, vagrant init jasonc/centos7 and press enter. The first time you run the Vagrant up command, Vagrant will import or clone the Vagrant Box into VirtualBox, and then start it. If Vagrant detects that the Virtual Machine already exists in VirtualBox, 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 and 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 mistype 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 init 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. And 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 so 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 block of texts that I'll provide in the exercise notes. And what that block of 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 CPU 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 for your computer, restart, and then try again. By the way there's a troubleshooting video specifically on these issues that goes into more detail. So if you're still having problems, go ahead and skip forward to the troubleshooting 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, Libre Office writer, do not use Apple pages, use a text editor. One text editor that I recommend a lot is Atom. It's spelled, A-T-O-M and it's available at atom.io, A-T-O-M.io and it works on windows, Mac, and Linux. So I'm going to go ahead and open up my Atom text editor and edit the Vagrant file here. Now, you can open up the Vagrant file with your text editor. I'm going to go to my home directory then find shellclass and then textbox01 and finally opened the Vagrant file. So this is where it becomes important to know where your home directory is, where you created your shellclass folder and what project you're working on and obviously where the Vagrant file is. So once you've located the file, you can open it with your favorite text editor. So if you did run Vagrant init with the incorrect box name, you can come down to the config.vm box line and make your change as needed. For example, I've seen some people do this, Jason/centos7 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 VirtualBox application. on windows you can double-click on the Oracle VM VirtualBox icon on your desktop. For Mac users you can start the applications VirtualBox application and Linux users will just need to find the application in their menuing 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 testbox01. 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 could confirm that it shows that the Virtual Machine is in a running state by running Vagrant status. The Vagrant status command confirm that the machine is running just like we saw it running inside the VirtualBox application as well. Now that we have a box up, let's connect to it with SSH. SSH is called Secure Shell and it's the network protocol use to connect to Linux systems. Vagrant provides a nice shortcut to SSH into the Virtual Machine, and that shortcut is Vagrant SSH. 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 SSH without a password. For reference, the password for the Vagrant account is Vagrant. The password for the root account is also Vagrant, that's V-A-G-R-A-N-T. The Vagrant user has full sudo or administrative privileges and that allows you to further configure the system if you want to. Of course, we'll be dealing more with accounts and privileges and Sudu later in this course. Now, let's exit the Virtual Machine by typing the exit command. And now we're logged out of the Linux Virtual Machine and back to our host operating system. The Vagrant all command shuts down the Virtual Machine. When you run this command, you will not lose any work you've performed inside the Virtual Machine. The Virtual Machine will still exist in VirtualBox. It will just simply be stopped. So let's do that now. Okay, the machine should be halted. Let's look at it in the VirtualBox application. You can see that the Virtual Machine still exists in VirtualBox, but it's just simply powered off. You might've noticed when we logged into the Linux system we had a prompt at redvagrant@localhost. Local hosts 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 a 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.vm box line. To set the host name, we can type config.vm.host name equals and then specify the name. So we're going to call this box testbox01, and now what we need to do is save it. I want 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, lowers the new Vagrant configuration and starts the Virtual Machine again. So let's give that a go here. Okay, let's connect to the Virtual Machine and see if the host name has changed, so we will run Vagrant SSH. Now you can see the prompt to reads Vagrant at testbox01. So our configuration worked. Let's go ahead and exit out of this Virtual Machine with exit. That will log us out and place us 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 10.9.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 start it on the next line. We'll do config.vm.network space, a quotation Mark, private underscore network. And then after the closing quotation mark we need to comma space IP, a colon space, we'll need quotation marks, 10.9.8.7 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 session and make Vagrant reload that configuration. So we'll do Vagrant, reload. Since the Virtual Machine was running, it'll go ahead and stop it and then start it with a new Vagrant configuration. Okay, our machine is reloaded. Let's test the network connectivity with the ping command. If you're on windows you can just type ping and the IP address. If you're on Mac or Linux, you're going to specify account with dash C. So we'll just do dash C3 to send three packets and we'll do 10.9.8.7, which is the IP address we assigned 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 address is 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.vm.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 of 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. We'll do that now. It'll prompt you to confirm that you actually want to delete the machine and we'll just say, Y for yes and hit enter. Now, if we look back in the VirtualBox 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 Vagrant destory, 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, period and press enter. That command changes to the parent directory which is represented by dot, dot or a 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. We'll do that with mkdir multitest and we'll change into that directory, cd multitest. And the next step is to initialize the Vagrant project which creates our Vagrant file. Vagrant init jasonc/centos7 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.9.8.5. And so what 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. So we just leave that there and it will apply to all the Virtual Machines that we create. So how do we create a Virtual Machine instance here is config.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, followed by pipe and then again the name of the Virtual Machine test one followed by a closing pipe. Now we'll use the name of that machine to perform our configuration. So we'll do test1.vm.host name and we'll give it a host name of test one. We also want to configure the network on this one. So we'll do test one.vm.network, private network. We're going to give it an IP address of 10.9.8.5. Now we can end 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 test2. So the host name is test2. We'll give this an IP address 10.9.8.6 and we'll end with an end keyword for that block of code which configured the test to the Virtual Machine. Now we just save our changes. Now let's start the Virtual Machines up by going back to our command line session and running Vagrant up. By the way if you only wanted to bring one of the systems up then you would specify it after Vagrant up. For example, if you only want it to bring up the test two system, you would run Vagrant up tests two. But if you don't specify a system or a Virtual Machine Vagrant up will bring up every machine that is defined in the Vagrant file. Okay, vagrant has brought up both of the Virtual Machines that we defined. Now let's check their status with Vagrant status. Okay, it says both of them are running. Test1 is running in VirtualBox, test2 is also running in VirtualBox. Let's go ahead and connect to the test1 Virtual Machine to confirm that it's working correctly and then we'll just quickly log out of it and we'll use Vagrant SSH. 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. So it will give a name of the VM here which is test one and hit enter. If you look at the prompt, it says Vagrant at test one. So that's the correct host we're on and it looks okay. We can get in it. Now let's log out of the system with exit. Let's do the same thing with the test two system. So we'll do Vagrant SSH test two. Again, notice the prompt it reads Vagrant at test two. So that's another confirmation that we're connected to the test two VM. Now, while you're logged into the test two 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 dash C3 10.9.8.5, which is the IP address of the test one system and hit enter. Okay, no packet loss. We can connect to the test one system over the network so that looks good. While we're connected to this VM, I want to show you one more thing. You might've noticed some messages about mounting shared folders when the Vagrant up command was executed. Well, you can access the files and 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 file on our 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 two VM. We'll run ls/Vagrant and you can see that there is the Vagrant file there. And let's look at the contents of that file with a cat command. So we'll do cat/vagrant/Vagrant file and press enter. Okay, 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 SCPS and those types of things. Okay, we're done for today so let's go ahead and exit out of this VM. By the way in the upcoming projects, you'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 the VirtualBox. 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.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
4195
Courses
61
Learning Paths
8

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics