DEV Community

Beatriz Rodrigues
Beatriz Rodrigues

Posted on

🧪How to Build a Linux Lab Using Vagrant

Hello, folks! 👋
I'm excited to share a tool I recently discovered that's been a game-changer for setting up Linux study labs: Vagrant.

At its core, Vagrant automates the creation and management of virtual machines. But here's a crucial point: it doesn't create the VM on its own. It needs a provider to do that (like VirtualBox, VMware, Hyper-V, Docker, etc.).

For this example, I'll be using VirtualBox, and all commands were executed in PowerShell on Windows.


Installing the Tools

First things first, download and install these two:


Preparing Your Environment

Once everything's installed, open PowerShell. You can use the Win + S shortcut, type "powershell," and run it as an administrator.

Choose where you want to create your virtual machine. In my case, I'm using my second drive (D:), so I navigated to the folder with:

cd D:\labs
Enter fullscreen mode Exit fullscreen mode

Creating Your Machine

You have two paths here. If you want to get straight to it, you can use:

vagrant init centos/7
Enter fullscreen mode Exit fullscreen mode

This command will automatically create the configuration file (Vagrantfile) with the CentOS 7 box pre-configured. However, if you prefer to set things up manually (define memory, CPU, static IP, etc.), you can run:

vagrant init
Enter fullscreen mode Exit fullscreen mode

This will generate an empty Vagrantfile that you can then customize to your liking.

I won't delve into Vagrantfile configuration in this post, as the goal is practicality. But if you're looking for something more tailored, a quick search will definitely help!


Bringing Up Your VM

Now, simply run:

vagrant up
Enter fullscreen mode Exit fullscreen mode

And to access your machine:

vagrant ssh
Enter fullscreen mode Exit fullscreen mode

And just like that, you're inside your VM! 😎


Some Distros You Can Try

vagrant init ubuntu/jammy64
vagrant init debian/bookworm64
vagrant init generic/rocky9
Enter fullscreen mode Exit fullscreen mode

You can find many more options here: https://app.vagrantup.com/boxes/search


Managing Your VM

Beyond vagrant up and vagrant ssh, here are some other useful commands for managing your machine:

vagrant halt       # gracefully shuts down the machine
vagrant suspend    # suspends the VM and saves its current state
vagrant resume     # resumes the VM from where it was suspended
vagrant reload     # restarts the VM, applying changes from the Vagrantfile
vagrant destroy    # completely destroys the VM, removing everything
Enter fullscreen mode Exit fullscreen mode

Final Tips

While vagrant ssh works fine, I personally prefer using PuTTY or MobaXterm to access the VM via SSH. I find them more stable and convenient than PowerShell.

If you have any questions, feel free to drop them in the comments!
Happy studying and happy testing!

Top comments (0)