Vagrant Cheat Sheet + Get Started With Vagrant
Vagrant Cheat Sheet + Get Started With Vagrant
linuxacademy.com/blog/linux/vagrant-cheat-sheet-get-started-with-vagrant/
December 1, 2017
We’re back with another cheat sheet – this time for Vagrant! We define some common
terms, give you a rundown of the most-used commands, and even include a sample
Vagrantfile that provisions a basic CentOS 7 web server to experiment on.
Check out the cheat sheet and get a basic Vagrant tutorial inside!
Cheat Sheet
Prerequisites
Vagrant! Vagrant is easy to install, and the process should only take a few
minutes.
VirtualBox
Linux, Mac, or Windows! Vagrant can be used on any operating system, and the
commands are the same.
Basic command-line knowledge of your operating system. Since this guide is OS-
agnostic, we will use generic terms like “change into the src directory” instead
of using any specific commands; you should know how to perform the following
commands on your system:
Change directories
Open text files
Add text files
Windows users should also have PuTTY installed.
Should we run vagrant init without the --minimal flag, our Vagrantfile
generates with instructional commentary. For the sake of this guide, we chose not to
include the additional commentary to provide us with a cleaner Vagrantfile to work
from.
Basic Settings
1. Open the newly-created Vagrantfile in your chosen text editor. Currently, it
should resemble the following:
Vagrant.configure(2) do |config|
config.vm.box = "base"
end
As it stands, all this Vagrantfile does is define the Vagrant configuration version
as 2 ( Vagrant.configure(2) do |config| ) and sets the guest machine to
work from the “base” box ( config.vm.box = "base" ). Note that the config at
the start of the box configuration references directly back to the |config|
value in the version line. All our configuration settings will start with config .
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
end
3. We also want to define the hostname, myhost, for this box. We can do this by
simply adding config.vm.hostname = "myhost" to our code:
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.hostname = "myhost"
end
4. We can also configure networking settings in Vagrant. For this example, we’re
going to set an IP address within a private network range:
2/6
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.hostname = "myhost"
config.vm.network "private_network", ip: "192.168.50.10"
end
If you are already using the private IP 192.168.50.10, update the Vagrantfile to use a
different address accordingly.
Sync Folders
In Vagrant, we can sync a folder to a folder within our guest machine. Since we’re
creating a sample web server, we want our local directory to hold our website’s files.
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.hostname = "myhost"
config.vm.network "private_network", ip: "192.168.50.10"
config.vm.synced_folder "src/", "/var/www/html"
end
Set Up Provisioning
Vagrant is able to pair with a number of provisioning tools to configure your server
upon creation. The most basic uses simple shell scripting. This is what we’ll be using.
Specifically, we will be using Ruby syntax to feed a series of commands into a variable,
and then call that variable in the line that configures our provisioning.
1. Create a $samplescript variable that installs, enables, and starts the Apache.
Place this before the Vagrant configuration stanza:
$samplescript = <<SCRIPT
yum install -y httpd
systemctl enable httpd
systemctl start httpd
SCRIPT
Vagrant.configure(2) do |config|
[snip]
3/6
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.hostname = "myhost"
config.vm.network "private_network", ip: "192.168.50.10"
config.vm.synced_folder "src/", "/var/www/html"
config.vm.provision "shell", inline: $samplescript
end
Configure VirtualBox
Finally, we can use our Vagrantfile to configure VirtualBox. This is done by adding a
code block specific to VirtualBox within the main section of our Vagrantfile:
Notice how the format is similar to the set up of the Vagrant configuration line, only
instead of defining and then prepending config to the configure, we’re using vb
(for VirtualBox; note that this value does not have to be the same).
$samplescript = <<SCRIPT
yum install -y httpd
systemctl enable httpd
systemctl start httpd
SCRIPT
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.hostname = "myhost"
config.vm.network "private_network", ip: "192.168.50.10"
config.vm.synced_folder "src/", "/var/www/html"
Vagrant Up!
With our Vagrantfile finished, return to the command line and ensure you’re in the
vagrantdir directory. From here, we can start our virtual machine. Starting a virtual
machine in Vagrant is easy. Just run:
vagrant up
4/6
Wait for the provisioning process to finish. When finished, you can check to see if the
web server is working by visiting 192.168.50.10:80 in your browser.
We can also SSH into our server. This process depends on your workstation
computer.
vagrant ssh
Feel free to take this moment to look around the CentOS environment. exit back to
your workstation when done.
First, from the command line, run vagrant ssh-config to retrieve the IP address
and port of the virtual machine:
vagrant ssh-config
Next, take this information and input it into PuTTY; the default username and
password for Vagrant-provisioned virtual machines are both vagrant. From here, we
should be able to connect to your server.
Take this moment to explore the guest machine. Close the connection when finished.
Halt
To halt the machine – that is, attempt a graceful shut down, wherein the machine
turns off the same as it would if it were a physical machine, run the following
command:
vagrant halt
Suspend
A suspended machine is a machine that is turned off but not shut down; essentially, it
is saved in the exact state that it was in when the suspend was run. To suspend a
machine, run:
vagrant suspend
5/6
Resume
To start a previously-halted or suspended machine, run:
vagrant resume
Restart
You may also need to perform a restart on your machine – whether to ensure your
system settings persist through reboot or otherwise. Use the following command to
restart the machine:
vagrant restart
Destroy
If you’re permanently done with your Vagrant environment and want to fully remove
it from your system, use:
vagrant destroy
Conclusion
Vagrant is a powerful and highly-useful tool for ensuring your development systems
run the same regardless of what computer they are running on. Feel free to adapt
and expand the example Vagrantfile as much or as little as you wish and be sure to
download the cheat sheet for a quick-reference if you get stuck! Keep practicing, and
you’ll be a Vagrant expert in no time.
6/6