Post

Linux pt 1: Quick VM Setup with Vagrant / Virtualbox (90 Days of DevOps)

Linux pt 1: Quick VM Setup with Vagrant / Virtualbox (90 Days of DevOps)

In this section of the 90 days of DevOps series, we cover Linux basics.

Linux has undergone massive growth since it was first created by Linus Torvalds decades ago as a personal project. Thanks to its open-source nature and high level of customizability, over time it has become practically the default operating system of the World Wide Web.

With that in mind, it makes sense that those involved in the process of deploying modern web apps are well served by understanding core Linux concepts.

In this section of the challenge, the goal is to get familiar with Linux basics. While I am an experienced Linux admin myself, my own goal in this series of blog posts is to complete the entire range of topics covered by the “90 Days of DevOps” challenge.

Let’s see what will be covered in the Linux section:

  • Setting up a VM
  • Basic Linux Commands
  • Managing your System, Files, and Storage
  • Text Editors: nano vs vim
  • SSH & Web Server (LAMP)
  • Automating Tasks with Bash scripts

    Note: On the off chance that a newcomer to Linux might be following along, I truly recommend eventually installing Linux directly to your own PC if at all possible. It can be an old, spare PC, or you can dual-boot a Windows PC. The more tasks you perform using Linux, the better you will grasp how it works.

Installing Virtualbox and Vagrant

Virtualbox is hypervisor software that will be run on our system to host the Linux VM we’ll be creating. Vagrant is a configuration management tool that we will use to automate the setup of our Linux VM, so that we don’t need to manually enter VM settings in our hypervisor.

This type of VM deployment is in keeping with the DevOps approach, which aims to make iteration as effortless as possible. If we needed to recreate this VM many times, the time invested automating its configuration beforehand in Vagrant would very likely pay off.

The downloads and installation instructions for both pieces of software vary by OS, and should be available at the Virtualbox and Vagrant links provided above. For the purpose of this blog post, both pieces of software will be run from a Linux workstation.

The Vagrantfile

Once both Virtualbox and Vagrant are installed, we create a new text file named Vagrantfile. Vagrantfiles use syntax from the Ruby programming language, to declare a desired VM configuration.

Below are the contents we save to our Vagrantfile :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Vagrant.configure("2") do |config|

  config.vm.box = "debian/bookworm64"

  config.vm.provider :virtualbox do |v|

   v.memory  = 8096

   v.cpus    = 4

end

end

The file is reasonably readable. It declares that we want a virtualbox VM, running the 64-bit version of the current stable/”bookworm” version of the Debian Linux OS (the VM image will come from the official public registry of Vagrant boxes). Our VM will also have 8096MB of RAM, and 4 virtual CPUs.

If we were to decide to further customize this Vagrantfile down the road, Hashicorp’s documentation of their Virtualbox provider, along with the (lengthy) official Virtualbox CLI manual, would make good references.

Creating and starting our VM

On our workstation, from within the same directory where our Vagrantfile is saved, we run the command vagrant up . This tells Vagrant to actually build the VM described in our file.

1
2
3
4
$ ls
Vagrantfile

$ vagrant up

Vagrant automatically creates our new Virtualbox VM, while printing status messages to our console:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'debian/bookworm64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'debian/bookworm64'
    default: URL: https://vagrantcloud.com/api/v2/vagrant/debian/bookworm64
==> default: Adding box 'debian/bookworm64' (v12.20240814.1) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/debian/boxes/bookworm64/versions/12.20240814.1/providers/virtualbox/unknown/vagrant.box
==> default: Successfully added box 'debian/bookworm64' (v12.20240814.1) for 'virtualbox'!
==> default: Importing base box 'debian/bookworm64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'debian/bookworm64' version '12.20240814.1' is up to date...
==> default: Setting the name of the VM: debian-bookworm64_default_1725771815785_32798
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
==> default: Machine 'default' has a post `vagrant up` message. This is a message
==> default: from the creator of the Vagrantfile, and not from Vagrant itself:
==> default: 
==> default: Vanilla Debian box. See https://app.vagrantup.com/debian for help and bug reports

Connecting to our VM

As the output above lets us know, our VM is now running. We connect to it using the vagrant ssh command:

1
$ vagrant ssh

We’re then presented with command-line access to the new VM.

1
2
3
...

vagrant@bookworm:~$ 

If we were running a Virtualbox image that included a GUI/desktop environment, we could also visit that desktop environment using the Virtualbox Manager GUI.

For now, we keep things simple and stick with a basic Linux command line.

Now that we have a working Linux terminal, we can start looking at basic Linux commands, which will be covered in the next post.

<< Back to 90 Days of DevOps posts

<<< Back to all posts

This post is licensed under CC BY-NC-SA 4.0 by the author.