5  RHEL 10 Lab with Vagrant

There are a variety of ways to stand up a lab environment to practice for Red Hat exams. One of the more effective ways that I’ve found is to stand up a reproducible lab environment with a tool like Vagrant.

5.1 Why Vagrant?

What Vagrant allows us to do is define one (or a few) virtual machines (VMs) with code and use the vagrant command-line utility to manage them in a local virtual environment. We can spin them up, reprovision, or destroy them as many times as we like. This means we don’t have to be afraid to break these instances; we can treat them as a throwaway sandbox. We also don’t have to worry about rolling back changes we may have made to the machine to get to a known good state. We can just destroy the instance and start over with a fresh box. This is a very important psychological safety net that allows us the freedom to learn without fear of making mistakes. Restoring our lab is minimal effort in the form of just a couple of vagrant commands (vagrant destroy and vagrant up).

5.1.1 Labs as Code

Vagrant has the added benefit of allowing me to ship chapter lab environments and practice exams in the form of Vagrantfile definitions and provisioning scripts (coming soon!).

5.1.2 Vagrant as an introduction to IaC

This also serves an introduction to patterns you will see in Infrastructure as Code (IaC) type tools like Ansible and HashiCorp’s Terraform. Servers are “cattle not pets” is something you might hear infrastructure engineers say. More and more we’re seeing the Linux System Administrator skillset morph into an “Engineering” skillset that can be scaled to a few or many hundreds or thousands of instances running in virtual environments. It makes sense that we should use some of the same tools and patterns to manage our local sandbox that we use everywhere else.

5.2 Workstation Prerequisites

This chapter assumes you have a Linux workstation that can run local VMs.

Important

If you still need to choose or install a desktop Linux system, see the Linux Workstation Learning Platform chapter first.

Ideally your workstation should meet the following requirements:

  • CPU contains virtualization extensions (Intel VT or AMD-v) and they’re enabled in the BIOS.
  • Minimum of 16GB RAM in order to run Linux plus a few VM’s.
  • x86_64 architecture is recommended for running the Vagrant RHEL boxes.
Note

ARM architecture may be workable (e.g. M-series macOS hardware), but hasn’t been tested. If you’re working through this book on ARM-based hardware, feel free to contribute to the discussion with any issues or feedback.

5.3 Install Vagrant

If you’re running Fedora, HashiCorp’s docs provide the following one-liner that can be pasted into your terminal and run to both install the hashicorp.repo file and install Vagrant.

wget -O- https://rpm.releases.hashicorp.com/fedora/hashicorp.repo | sudo tee /etc/yum.repos.d/hashicorp.repo
sudo yum list available | grep hashicorp
sudo dnf -y install vagrant
Caution

It’s a good idea to read and understand any command like this that is copy and paste out of docs you find online. Especially if it’s downloading something and piping it to another command with | as this one is doing. We’ll cover pipes in more detail later.

If your workstation isn’t running Fedora, then follow the documented Vagrant Install for your Linux distro.

5.4 Enable Virtualization Support

There are multiple virtualization providers that Vagrant supports, but the two common ones are libvirt and VirtualBox (default). libvirt requires installing the vagrant-libvirt plugin and VirtualBox requires installing Oracle’s VirtualBox. My recommendation is to use libvirt since most Linux distributions have some kind of out-of-the box Virtualization support using libvirt + KVM.

Choose either: install libvirt or install VirtualBox.

5.4.2 [Option 2] Install VirtualBox

Alternatively, you may install VirtualBox. This is the default provider that Vagrant supports out of the box (no special plugin needed as in vagrant-libvirt). If you choose to go this route, then I recommend not installing libvirt alongside it. This is known to conflict with VirtualBox as both hypervisors compete for the CPU’s virtualization extensions. Ideally pick one or the other as you’re preferred hypervisor for standing up your RHCSA lab VMs.

If you install VirtualBox on one of the rpm-family distros (i.e. Oracle Linux, RHEL, Fedora, or openSUSE) I recommend installing Oracle’s published virtualbox.repo file and installing VirtualBox using the system package manager (yum, dnf, or zypper). That way if there are updates available they will automatically become available whenever repositories are refreshed as in a system update (e.g. dnf update)

Install VirtualBox on Fedora:

wget -O- https://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo | sudo tee /etc/yum.repos.d/virtualbox.repo
sudo dnf -y install VirtualBox-7.2
Important

Be sure to verify that the version of VirtualBox you install is supported by Vagrant.

5.4.3 Install Vagrant Plugins

Install the vagrant-libvirt plugin:

vagrant plugin install vagrant-libvirt

And install the vagrant-registration plugin:

vagrant plugin install vagrant-registration
Caution

Although you may be able to install the vagrant-libvirt and vagrant-registration plugins using dnf (e.g. dnf install vagrant-registration); this is not recommended due to compatibility issues that may arise as a result of dependency and version mismatch between the vagrant packages available in repos compared to vagrant’s own plugin manager.

To list vagrant plugins:

vagrant plugin list
vagrant-libvirt (0.12.2, global)
vagrant-registration (1.3.4, global)

5.5 Stand up a RHEL 10 VM using Vagrant

Next let’s stand up a RHEL 10 VM using Vagrant. I recommend creating a directory to use for RHCSA lab stuff.

mkdir ~/rhcsa-lab && cd ~/rhcsa-lab

Next fetch the pre-baked Vagrantfile for RHEL 10 from the vagrant-rhel-boxes project:

wget https://raw.githubusercontent.com/kraker/vagrant-rhel-boxes/refs/heads/main/Vagrantfile

Or copy and paste the code below into a new Vagrantfile.

Review the contents of Vagrantfile (e.g. cat Vagrantfile):

Vagrant.configure("2") do |config|
  config.vm.box = "kraker/rhel-10"
  config.vm.hostname = "rhel10"

  config.registration.auto_attach = false

  if ENV['RHSM_ORG'] && ENV['RHSM_ACTIVATIONKEY']
    config.registration.org           = ENV['RHSM_ORG']
    config.registration.activationkey = ENV['RHSM_ACTIVATIONKEY']
  elsif ENV['RHSM_USERNAME'] && ENV['RHSM_PASSWORD']
    config.registration.username = ENV['RHSM_USERNAME']
    config.registration.password = ENV['RHSM_PASSWORD']
  end

  cpus = 2
  memory = 2048

  config.vm.provider  do |libvirt|
    libvirt.cpus = cpus
    libvirt.memory = memory
    libvirt.video_type = "virtio"
  end

  config.vm.provider  do |vb|
    vb.cpus = cpus
    vb.memory = memory
    vb.gui = false
  end

  config.vm.synced_folder ".", "/vagrant",  true
end

Stand-up the rhel10 VM using your chosen provider:

vagrant up --provider=libvirt  # or --provider=virtualbox

The first time you run vagrant up the vagrant-registration plugin will automatically prompt you if you want to register the system. Go ahead and press Enter for the default, “yes”.

Note

You may see [fog][WARNING] Unrecognized arguments: libvirt_ip_command when using the libvirt provider. This is a known vagrant-libvirt/Fog compatibility warning. If vagrant status and vagrant ssh continue to work, it can be safely ignored.

Next you will be prompted to enter your username and password. Use the same username and password you used to create your Red Hat Developer’s Account.

...
==> default: Registering box with vagrant-registration...
    default: Would you like to register the system now (default: yes)? [y|n]y
    default: username: akraker
    default: password:
==> default: Registration successful.
==> default: Waiting for machine to boot. This may take a few minutes...
...

For non-interactive username/password registration, the Vagrantfile above also accepts RHSM_USERNAME + RHSM_PASSWORD environment variables that can be set in your shell.

export RHSM_USERNAME="your_redhat_username"
export RHSM_PASSWORD="your_redhat_password"

Although this isn’t necessarily the most secure practice, you may also add the two lines above to your ~/.bashrc so that the environment variables are persistent across shell sessions. Only do this if you’re reasonably certain your workstation is secure.

Obviously don’t use plaintext username/password on shared systems and or anything which might be exposed publicly.

Next run vagrant status and you should see the machine is running:

vagrant status
[fog][WARNING] Unrecognized arguments: libvirt_ip_command
Current machine states:

default                   running (libvirt)

The Libvirt domain is running. To stop this machine, you can run
`vagrant halt`. To destroy the machine, you can run `vagrant destroy`.

SSH into the machine with vagrant ssh:

vagrant ssh
[fog][WARNING] Unrecognized arguments: libvirt_ip_command
Register this system with Red Hat Insights: rhc connect

Example:
# rhc connect --activation-key <key> --organization <org>

The rhc client and Red Hat Insights will enable analytics and additional
management capabilities on your system.
View your connected systems at https://console.redhat.com/insights

You can learn more about how to register your system 
using rhc at https://red.ht/registration

Verify that the system is registered by running sudo dnf repolist to refresh Red Hat repos.

sudo dnf repolist
Updating Subscription Management repositories.
repo id                           repo name
rhel-10-for-x86_64-appstream-rpms Red Hat Enterprise Linux 10 for x86_64 - AppStream (RPMs)
rhel-10-for-x86_64-baseos-rpms    Red Hat Enterprise Linux 10 for x86_64 - BaseOS (RPMs)

Note that by default vagrant sets up vagrant as the login user:

whoami
vagrant

And gives this user passwordless sudoers privileges by default:

sudo -l
User vagrant may run the following commands on rhel10:
    (ALL) NOPASSWD: ALL
Note

To support standing up reproducible lab environments on RHEL I’m publishing RHEL boxes to Vagrant Cloud. See the kraker namespace for a list of available boxes.

Don’t see what you need? Please open a request!

5.5.1 Vagrant CLI quickstart

From a directory with a Vagrantfile and a box defined such as the one we created above.

Spin up or start a box:

vagrant up  # Defaults to the virtualbox provider if running the first time
# or
vagrant up --provider=libvirt

To check the status of a running box:

vagrant status

To shutdown a box:

vagrant halt

To destroy a box and start over:

vagrant destroy

Connect to a box with SSH:

vagrant ssh

See the Vagrant CLI docs for a complete reference.

Tip

You can also use vagrant --help and vagrant COMMAND --help as Vagrant CLI references.

5.6 LAB: Vagrant Exercise

To familiarize yourself further with what all Vagrant can do; working through the quick start tutorials is recommended as an exercise.